-
Notifications
You must be signed in to change notification settings - Fork 182
/
smart-table.tsx
63 lines (54 loc) · 1.61 KB
/
smart-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
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import Table, {TableAttrs} from './table';
import Selection, {SelectionItem} from './selection';
const {
selection: __selection__,
onSelect: __onSelect__,
...restPropTypes
} = Table.propTypes ?? {};
export interface SmartTableProps<T extends SelectionItem> extends
Omit<TableAttrs<T>, 'selection' | 'onSelect'> {
onSelectionChange: (selection: Selection<T>) => void
selection?: Selection<T>
}
class SmartTable<T extends SelectionItem> extends PureComponent<SmartTableProps<T>> {
static propTypes = {
onSelectionChange: PropTypes.func,
isItemSelectable: PropTypes.func,
...restPropTypes
};
static defaultProps = {
onSelectionChange: () => {}
};
state = {
selection: new Selection({
data: this.props.data,
isItemSelectable: this.props.isItemSelectable
})
};
UNSAFE_componentWillReceiveProps(nextProps: SmartTableProps<T>) {
const {data, isItemSelectable, selection} = nextProps;
if (this.props.remoteSelection && this.props.selection !== selection) {
this.setState({selection});
} else if (this.props.data !== data || this.props.isItemSelectable !== isItemSelectable) {
this.setState({
selection: new Selection({data, isItemSelectable})
});
}
}
onSelect = (selection: Selection<T>) => {
this.setState({selection});
this.props.onSelectionChange(selection);
};
render() {
return (
<Table
{...this.props}
selection={this.state.selection}
onSelect={this.onSelect}
/>
);
}
}
export default SmartTable;