aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/components/TableBox.tsx
blob: adefe90cdd88ec397b383d203d0306a20e714ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { action, computed } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { emptyFunction, returnFalse, setupMoveUpEvents } from '../../../../../Utils';

interface TableBoxProps {
    pairs: { [key: string]: any }[];
    selectAxes: (axes: string[]) => void;
    axes: string[];
}

@observer
export class TableBox extends React.Component<TableBoxProps> {
    @computed get columns() {
        return this.props.pairs.length ? Array.from(Object.keys(this.props.pairs[0])) : [];
    }
    render() {
        return (
            <div className="table-container">
                <table className="table">
                    <thead>
                        <tr className="table-row">
                            {this.columns.map(col => (
                                <th
                                    style={{ color: this.props.axes.slice().reverse().lastElement() === col ? 'green' : this.props.axes.lastElement() === col ? 'red' : undefined, fontWeight: this.props.axes.includes(col) ? 'bolder' : 'normal' }}
                                    onPointerDown={e =>
                                        setupMoveUpEvents(
                                            {},
                                            e,
                                            returnFalse,
                                            emptyFunction,
                                            action(e => {
                                                const newAxes = this.props.axes;
                                                if (newAxes.includes(col)) {
                                                    newAxes.splice(newAxes.indexOf(col), 1);
                                                } else if (newAxes.length >= 1) {
                                                    newAxes[1] = col;
                                                } else {
                                                    newAxes[0] = col;
                                                }
                                                this.props.selectAxes(newAxes);
                                            })
                                        )
                                    }>
                                    {col}
                                </th>
                            ))}
                        </tr>
                    </thead>
                    <tbody>
                        {this.props.pairs?.map(p => {
                            return (
                                <tr className="table-row">
                                    {this.columns.map(col => (
                                        <td>{p[col]}</td>
                                    ))}
                                </tr>
                            );
                        })}
                    </tbody>
                </table>
            </div>
        );
    }
}