1 | import React, {PureComponent} from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 |
|
4 | import Table from './table';
|
5 | import Selection from './selection';
|
6 |
|
7 |
|
8 | const {
|
9 | selection: __selection__,
|
10 | onSelect: __onSelect__,
|
11 | ...restPropTypes
|
12 | } = Table.propTypes;
|
13 |
|
14 |
|
15 |
|
16 | class SmartTable extends PureComponent {
|
17 | static propTypes = {
|
18 | onSelectionChange: PropTypes.func,
|
19 | isItemSelectable: PropTypes.func,
|
20 | ...restPropTypes
|
21 | };
|
22 |
|
23 | static defaultProps = {
|
24 | onSelectionChange: () => {}
|
25 | };
|
26 |
|
27 | state = {
|
28 | selection: new Selection({
|
29 | data: this.props.data,
|
30 | isItemSelectable: this.props.isItemSelectable
|
31 | })
|
32 | };
|
33 |
|
34 | componentWillReceiveProps(nextProps) {
|
35 | const {data, isItemSelectable} = nextProps;
|
36 | if (this.props.data !== data || this.props.isItemSelectable !== isItemSelectable) {
|
37 | const selection = new Selection({data, isItemSelectable});
|
38 | this.setState({selection});
|
39 | }
|
40 | }
|
41 |
|
42 | onSelect = selection => {
|
43 | this.setState({selection});
|
44 | this.props.onSelectionChange(selection);
|
45 | };
|
46 |
|
47 | render() {
|
48 | return (
|
49 | <Table
|
50 | {...this.props}
|
51 | selection={this.state.selection}
|
52 | onSelect={this.onSelect}
|
53 | />
|
54 | );
|
55 | }
|
56 | }
|
57 |
|
58 | export default SmartTable;
|