|
1 | | -/* eslint react/prop-types: 0 */ |
2 | | -import { Component } from 'react'; |
| 1 | +import React, { Component } from 'react'; |
3 | 2 | import PropTypes from 'prop-types';
|
4 | 3 | import { filters } from './filter';
|
5 | 4 | import { LIKE } from './comparison';
|
6 | 5 |
|
7 | | -export default class FilterWrapper extends Component { |
8 | | - static propTypes = { |
9 | | - store: PropTypes.object.isRequired, |
10 | | - columns: PropTypes.array.isRequired, |
11 | | - baseElement: PropTypes.func.isRequired, |
12 | | - onRemoteFilterChange: PropTypes.func.isRequired, |
13 | | - // refactoring later |
14 | | - _: PropTypes.object.isRequired |
15 | | - } |
16 | | - |
17 | | - constructor(props) { |
18 | | - super(props); |
19 | | - this.state = { currFilters: {}, isDataChanged: false }; |
20 | | - this.onFilter = this.onFilter.bind(this); |
21 | | - } |
22 | | - |
23 | | - componentWillReceiveProps(nextProps) { |
24 | | - // consider to use lodash.isEqual |
25 | | - if (JSON.stringify(this.state.currFilters) !== JSON.stringify(nextProps.store.filters)) { |
26 | | - this.setState(() => ({ isDataChanged: true, currFilters: nextProps.store.filters })); |
27 | | - } else { |
28 | | - this.setState(() => ({ isDataChanged: false })); |
| 6 | +export default (Base, { |
| 7 | + _, |
| 8 | + remoteResolver |
| 9 | +}) => |
| 10 | + class FilterWrapper extends remoteResolver(Component) { |
| 11 | + static propTypes = { |
| 12 | + store: PropTypes.object.isRequired, |
| 13 | + columns: PropTypes.array.isRequired |
29 | 14 | }
|
30 | | - } |
31 | | - |
32 | | - onFilter(column, filterVal, filterType) { |
33 | | - const { store, columns, _, onRemoteFilterChange } = this.props; |
34 | | - const currFilters = Object.assign({}, this.state.currFilters); |
35 | | - const { dataField, filter } = column; |
36 | 15 |
|
37 | | - if (!_.isDefined(filterVal) || filterVal === '') { |
38 | | - delete currFilters[dataField]; |
39 | | - } else { |
40 | | - const { comparator = LIKE } = filter.props; |
41 | | - currFilters[dataField] = { filterVal, filterType, comparator }; |
| 16 | + constructor(props) { |
| 17 | + super(props); |
| 18 | + this.state = { currFilters: {}, isDataChanged: false }; |
| 19 | + this.onFilter = this.onFilter.bind(this); |
42 | 20 | }
|
43 | | - store.filters = currFilters; |
44 | 21 |
|
45 | | - if (this.isRemote() || this.isPaginationRemote()) { |
46 | | - onRemoteFilterChange(this.isPaginationRemote()); |
47 | | - // when remote filtering is enable, dont set currFilters state |
48 | | - // in the componentWillReceiveProps, it's the key point that we can know the filter is changed |
49 | | - return; |
| 22 | + componentWillReceiveProps(nextProps) { |
| 23 | + // consider to use lodash.isEqual |
| 24 | + if (JSON.stringify(this.state.currFilters) !== JSON.stringify(nextProps.store.filters)) { |
| 25 | + this.setState(() => ({ isDataChanged: true, currFilters: nextProps.store.filters })); |
| 26 | + } else { |
| 27 | + this.setState(() => ({ isDataChanged: false })); |
| 28 | + } |
50 | 29 | }
|
51 | 30 |
|
52 | | - store.filteredData = filters(store, columns, _)(currFilters); |
53 | | - this.setState(() => ({ currFilters, isDataChanged: true })); |
54 | | - } |
| 31 | + onFilter(column, filterVal, filterType) { |
| 32 | + const { store, columns } = this.props; |
| 33 | + const currFilters = Object.assign({}, this.state.currFilters); |
| 34 | + const { dataField, filter } = column; |
55 | 35 |
|
56 | | - // refactoring later |
57 | | - isRemote() { |
58 | | - const { remote } = this.props; |
59 | | - return remote === true || (typeof remote === 'object' && remote.filter); |
60 | | - } |
| 36 | + if (!_.isDefined(filterVal) || filterVal === '') { |
| 37 | + delete currFilters[dataField]; |
| 38 | + } else { |
| 39 | + const { comparator = LIKE } = filter.props; |
| 40 | + currFilters[dataField] = { filterVal, filterType, comparator }; |
| 41 | + } |
| 42 | + store.filters = currFilters; |
61 | 43 |
|
62 | | - // refactoring later |
63 | | - isPaginationRemote() { |
64 | | - const { remote } = this.props; |
65 | | - return remote === true || (typeof remote === 'object' && remote.pagination); |
66 | | - } |
| 44 | + if (this.isRemoteFiltering() || this.isRemotePagination()) { |
| 45 | + this.handleRemoteFilterChange(); |
| 46 | + // when remote filtering is enable, dont set currFilters state |
| 47 | + // in the componentWillReceiveProps, |
| 48 | + // it's the key point that we can know the filter is changed |
| 49 | + return; |
| 50 | + } |
67 | 51 |
|
68 | | - render() { |
69 | | - return this.props.baseElement({ |
70 | | - ...this.props, |
71 | | - key: 'table', |
72 | | - onFilter: this.onFilter, |
73 | | - isDataChanged: this.state.isDataChanged |
74 | | - }); |
75 | | - } |
76 | | -} |
| 52 | + store.filteredData = filters(store, columns, _)(currFilters); |
| 53 | + this.setState(() => ({ currFilters, isDataChanged: true })); |
| 54 | + } |
| 55 | + |
| 56 | + render() { |
| 57 | + return ( |
| 58 | + <Base |
| 59 | + { ...this.props } |
| 60 | + data={ this.props.store.data } |
| 61 | + onFilter={ this.onFilter } |
| 62 | + isDataChanged={ this.state.isDataChanged } |
| 63 | + /> |
| 64 | + ); |
| 65 | + } |
| 66 | + }; |
0 commit comments