/
table.tsx
151 lines (138 loc) · 3.52 KB
/
table.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
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import React, { ReactElement, ReactNode, useState } from 'react';
import { caretDownIcon, caretUpIcon } from '../icon';
export const TABLE_CLASS = 'jp-sortable-table';
/**
* A namespace for Table.
*/
export namespace Table {
/**
* The state which will be restored from layout tracker.
*/
export interface ISortState {
sortKey?: string | null;
sortDirection: -1 | 1;
}
/**
* The initialization options for the table.
*/
export interface IOptions<T> extends Partial<ISortState> {
rows: IRow<T>[];
columns: IColumn<T>[];
onRowClick?: React.MouseEventHandler<HTMLTableRowElement>;
blankIndicator: () => ReactNode;
}
/**
* Table row with data to display.
*/
export interface IRow<T> {
data: T;
key: string;
}
/**
* Column definition.
*/
export interface IColumn<T> {
id: string;
label: string;
renderCell(data: T): ReactNode;
sort(a: T, b: T): number | undefined;
isAvailable?(): boolean;
isHidden?: boolean;
}
}
/**
* Sortable table component for small datasets.
*
* For large datasets use `DataGrid` from `@lumino/datagrid`.
*/
export function Table<T>(props: Table.IOptions<T>) {
const [sortState, setSortState] = useState<Table.ISortState>({
sortKey: props.sortKey,
sortDirection: props.sortDirection || 1
});
const sort = (key: string) => {
if (key === sortState.sortKey) {
setSortState({
sortKey: key,
sortDirection: (sortState.sortDirection * -1) as -1 | 1
});
} else {
setSortState({ sortKey: key, sortDirection: 1 });
}
};
let rows = props.rows;
const sortedColumn = props.columns.filter(
column => column.id === sortState.sortKey
)[0];
if (sortedColumn) {
const sorter = sortedColumn.sort.bind(sortedColumn);
rows = props.rows.sort(
(a, b) => sorter(a.data, b.data) * sortState.sortDirection
);
}
const visibleColumns = props.columns.filter(
column =>
(column.isAvailable ? column.isAvailable() : true) && !column.isHidden
);
const elements = rows.map(row => {
const cells = visibleColumns.map(column => (
<td key={column.id + '-' + row.key}>{column.renderCell(row.data)}</td>
));
return (
<tr
key={row.key}
data-key={row.key}
onClick={props.onRowClick}
className={'jp-sortable-table-tr'}
>
{cells}
</tr>
);
});
const columnsHeaders = visibleColumns.map(column => (
<SortableTH
label={column.label}
id={column.id}
state={sortState}
key={column.id}
onSort={() => {
sort(column.id);
}}
/>
));
return (
<table className={TABLE_CLASS}>
<thead>
<tr className={'jp-sortable-table-tr'}>{columnsHeaders}</tr>
</thead>
<tbody>{elements}</tbody>
</table>
);
}
function SortableTH(props: {
id: string;
label: string;
state: Table.ISortState;
onSort: () => void;
}): ReactElement {
const isSortKey = props.id === props.state.sortKey;
const sortIcon =
!isSortKey || props.state.sortDirection === 1 ? caretUpIcon : caretDownIcon;
return (
<th
key={props.id}
onClick={() => props.onSort()}
className={isSortKey ? 'jp-sorted-header' : undefined}
data-id={props.id}
>
<div className="jp-sortable-table-th-wrapper">
<label>{props.label}</label>
<sortIcon.react tag="span" className="jp-sort-icon" />
</div>
</th>
);
}