-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
table.tsx
208 lines (176 loc) · 6.77 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import "./table.scss";
import React from "react";
import { orderBy } from "lodash";
import { observer } from "mobx-react";
import { autobind, cssNames, noop } from "../../utils";
import { TableRow, TableRowElem, TableRowProps } from "./table-row";
import { TableHead, TableHeadElem, TableHeadProps } from "./table-head";
import { TableCellElem } from "./table-cell";
import { VirtualList } from "../virtual-list";
import { createPageParam } from "../../navigation";
import { ItemObject } from "../../item.store";
import { getSortParams, setSortParams } from "./table.storage";
import { computed } from "mobx";
export type TableSortBy = string;
export type TableOrderBy = "asc" | "desc" | string;
export type TableSortParams = { sortBy: TableSortBy; orderBy: TableOrderBy };
export type TableSortCallback<D = any> = (data: D) => string | number | (string | number)[];
export type TableSortCallbacks = { [columnId: string]: TableSortCallback };
export interface TableProps extends React.DOMAttributes<HTMLDivElement> {
tableId?: string;
items?: ItemObject[]; // Raw items data
className?: string;
autoSize?: boolean; // Setup auto-sizing for all columns (flex: 1 0)
selectable?: boolean; // Highlight rows on hover
scrollable?: boolean; // Use scrollbar if content is bigger than parent's height
storageKey?: string; // Keep some data in localStorage & restore on page reload, e.g sorting params
/**
* Define sortable callbacks for every column in <TableHead><TableCell sortBy="someCol"><TableHead>
* @sortItem argument in the callback is an object, provided in <TableRow sortItem={someColDataItem}/>
*/
sortable?: TableSortCallbacks;
sortSyncWithUrl?: boolean; // sorting state is managed globally from url params
sortByDefault?: Partial<TableSortParams>; // default sorting params
onSort?: (params: TableSortParams) => void; // callback on sort change, default: global sync with url
noItems?: React.ReactNode; // Show no items state table list is empty
selectedItemId?: string; // Allows to scroll list to selected item
virtual?: boolean; // Use virtual list component to render only visible rows
rowPadding?: string;
rowLineHeight?: string;
customRowHeights?: (item: object, lineHeight: number, paddings: number) => number;
getTableRow?: (uid: string) => React.ReactElement<TableRowProps>;
}
export const sortByUrlParam = createPageParam({
name: "sort",
isSystem: true,
});
export const orderByUrlParam = createPageParam({
name: "order",
isSystem: true,
});
@observer
export class Table extends React.Component<TableProps> {
static defaultProps: TableProps = {
scrollable: true,
autoSize: true,
rowPadding: "8px",
rowLineHeight: "17px",
sortSyncWithUrl: true,
};
componentDidMount() {
const { sortable, tableId } = this.props;
if (sortable && !tableId) {
console.error("[Table]: sorted table requires props.tableId to be specified");
}
}
@computed get sortParams() {
return Object.assign({}, this.props.sortByDefault, getSortParams(this.props.tableId));
}
renderHead() {
const { sortable, children } = this.props;
const content = React.Children.toArray(children) as (TableRowElem | TableHeadElem)[];
const headElem: React.ReactElement<TableHeadProps> = content.find(elem => elem.type === TableHead);
if (headElem) {
if (sortable) {
const columns = React.Children.toArray(headElem.props.children) as TableCellElem[];
return React.cloneElement(headElem, {
children: columns.map(elem => {
if (elem.props.checkbox) {
return elem;
}
const title = elem.props.title || (
// copy cell content to title if it's a string
// usable if part of TableCell's content is hidden when there is not enough space
typeof elem.props.children === "string" ? elem.props.children : undefined
);
return React.cloneElement(elem, {
title,
_sort: this.sort,
_sorting: this.sortParams,
_nowrap: headElem.props.nowrap,
});
})
});
}
return headElem;
}
}
getSorted(items: any[]) {
const { sortBy, orderBy: order } = this.sortParams;
const sortingCallback = this.props.sortable[sortBy] || noop;
return orderBy(items, sortingCallback, order as any);
}
@autobind()
protected onSort({ sortBy, orderBy }: TableSortParams) {
setSortParams(this.props.tableId, { sortBy, orderBy });
const { sortSyncWithUrl, onSort } = this.props;
if (sortSyncWithUrl) {
sortByUrlParam.set(sortBy);
orderByUrlParam.set(orderBy);
}
if (onSort) {
onSort({ sortBy, orderBy });
}
}
@autobind()
sort(colName: TableSortBy) {
const { sortBy, orderBy } = this.sortParams;
const sameColumn = sortBy == colName;
const newSortBy: TableSortBy = colName;
const newOrderBy: TableOrderBy = (!orderBy || !sameColumn || orderBy === "desc") ? "asc" : "desc";
this.onSort({
sortBy: String(newSortBy),
orderBy: newOrderBy,
});
}
renderRows() {
const { sortable, noItems, children, virtual, customRowHeights, rowLineHeight, rowPadding, items, getTableRow, selectedItemId, className } = this.props;
const content = React.Children.toArray(children) as (TableRowElem | TableHeadElem)[];
let rows: React.ReactElement<TableRowProps>[] = content.filter(elem => elem.type === TableRow);
let sortedItems = rows.length ? rows.map(row => row.props.sortItem) : [...items];
if (sortable) {
sortedItems = this.getSorted(sortedItems);
if (rows.length) {
rows = sortedItems.map(item => rows.find(row => {
return item == row.props.sortItem;
}));
}
}
if (!rows.length && !items.length && noItems) {
return noItems;
}
if (virtual) {
const lineHeight = parseFloat(rowLineHeight);
const padding = parseFloat(rowPadding);
let rowHeights: number[] = Array(items.length).fill(lineHeight + padding * 2);
if (customRowHeights) {
rowHeights = sortedItems.map(item => {
return customRowHeights(item, lineHeight, padding * 2);
});
}
return (
<VirtualList
items={sortedItems}
rowHeights={rowHeights}
getRow={getTableRow}
selectedItemId={selectedItemId}
className={className}
/>
);
}
return rows;
}
render() {
const { selectable, scrollable, sortable, autoSize, virtual } = this.props;
let { className } = this.props;
className = cssNames("Table flex column", className, {
selectable, scrollable, sortable, autoSize, virtual,
});
return (
<div className={className}>
{this.renderHead()}
{this.renderRows()}
</div>
);
}
}