/
basic.tsx
154 lines (148 loc) · 3.62 KB
/
basic.tsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from 'react';
import {useStyletron} from 'baseui';
import {
Unstable_StatefulDataTable,
BooleanColumn,
CategoricalColumn,
CustomColumn,
NumericalColumn,
StringColumn,
COLUMNS,
NUMERICAL_FORMATS,
} from 'baseui/data-table';
// https://gist.github.com/6174/6062387
function pseudoRandomString(rowIdx: any, columnIdx: any) {
return (
(0.88 * rowIdx)
.toString(36)
.replace('.', '')
.substring(2) +
(0.99 * columnIdx).toString(36).replace('.', '')
).slice(0, 10);
}
function makeRowsFromColumns(columns: any, rowCount: number) {
const rows = [];
for (let i = 0; i < rowCount; i++) {
rows.push({
id: i,
data: columns.map((column: any, j: number) => {
switch (column.kind) {
case COLUMNS.CATEGORICAL:
switch (i % 5) {
case 4:
return 'A';
case 3:
return 'B';
case 2:
return 'C';
case 1:
return 'D';
case 0:
default:
return 'F';
}
case COLUMNS.NUMERICAL:
return i % 2 ? i - 1 : i + 3;
case COLUMNS.BOOLEAN:
return i % 2 === 0;
case COLUMNS.STRING:
return pseudoRandomString(i, j);
case COLUMNS.CUSTOM:
switch (i % 5) {
case 4:
return {color: 'red'};
case 3:
return {color: 'green'};
case 2:
return {color: 'blue'};
case 1:
return {color: 'purple'};
case 0:
default:
return {color: 'yellow'};
}
default:
return 'default' + pseudoRandomString(i, j);
}
}),
});
}
return rows;
}
type RowDataT = [
string,
string,
number,
number,
number,
{color: string},
boolean,
string,
];
const columns = [
CategoricalColumn({
title: 'categorical',
mapDataToValue: (data: RowDataT) => data[0],
}),
StringColumn({
title: 'string',
mapDataToValue: (data: RowDataT) => data[1],
}),
NumericalColumn({
title: 'three',
mapDataToValue: (data: RowDataT) => data[2],
}),
NumericalColumn({
title: 'neg std',
highlight: (n: number) => n < 0,
mapDataToValue: (data: RowDataT) => data[3],
}),
NumericalColumn({
title: 'accounting',
format: NUMERICAL_FORMATS.ACCOUNTING,
mapDataToValue: (data: RowDataT) => data[4],
}),
CustomColumn<{color: string}, {}>({
title: 'custom color',
mapDataToValue: (data: RowDataT) => data[5],
renderCell: function Cell(props: any) {
const [css] = useStyletron();
return (
<div
className={css({
alignItems: 'center',
fontFamily: '"Comic Sans MS", cursive, sans-serif',
display: 'flex',
})}
>
<div
className={css({
backgroundColor: props.value.color,
height: '12px',
marginRight: '24px',
width: '12px',
})}
/>
<div>{props.value.color}</div>
</div>
);
},
}),
BooleanColumn({
title: 'boolean',
mapDataToValue: (data: RowDataT) => data[6],
}),
CategoricalColumn({
title: 'second category',
mapDataToValue: (data: RowDataT) => data[7],
}),
];
const rows = makeRowsFromColumns(columns, 2000);
export default () => {
const [css] = useStyletron();
return (
<div className={css({height: '800px'})}>
<Unstable_StatefulDataTable columns={columns} rows={rows} />
</div>
);
};