From f84dfbbaa9735240cfdd94975f1dbaf3bbc8fee8 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Wed, 8 Feb 2017 14:43:00 +0800 Subject: [PATCH] Fix table filter in JSX (#4765) Fix #4759 --- components/table/Table.tsx | 11 +++-- .../table/__tests__/Table.filter.test.js | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/components/table/Table.tsx b/components/table/Table.tsx index 817eb8a693..abf7146801 100755 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -194,6 +194,7 @@ export default class Table extends React.Component, any> { } componentWillReceiveProps(nextProps) { + this.columns = nextProps.columns || normalizeColumns(nextProps.children); if ('pagination' in nextProps || 'pagination' in this.props) { this.setState(previousState => { const newPagination = assign({}, defaultPagination, previousState.pagination, nextProps.pagination); @@ -222,17 +223,17 @@ export default class Table extends React.Component, any> { this.CheckboxPropsCache = {}; } - if (this.getSortOrderColumns(nextProps.columns).length > 0) { - const sortState = this.getSortStateFromColumns(nextProps.columns); + if (this.getSortOrderColumns(this.columns).length > 0) { + const sortState = this.getSortStateFromColumns(this.columns); if (sortState.sortColumn !== this.state.sortColumn || sortState.sortOrder !== this.state.sortOrder) { this.setState(sortState); } } - const filteredValueColumns = this.getFilteredValueColumns(nextProps.columns); + const filteredValueColumns = this.getFilteredValueColumns(this.columns); if (filteredValueColumns.length > 0) { - const filtersFromColumns = this.getFiltersFromColumns(nextProps.columns); + const filtersFromColumns = this.getFiltersFromColumns(this.columns); const newFilters = assign({}, this.state.filters); Object.keys(filtersFromColumns).forEach(key => { newFilters[key] = filtersFromColumns[key]; @@ -241,8 +242,6 @@ export default class Table extends React.Component, any> { this.setState({ filters: newFilters }); } } - - this.columns = nextProps.columns || normalizeColumns(nextProps.children); } setSelectedRowKeys(selectedRowKeys, { selectWay, record, checked, changeRowKeys }: any) { diff --git a/components/table/__tests__/Table.filter.test.js b/components/table/__tests__/Table.filter.test.js index 55b6cb9c30..02b9a14fb9 100644 --- a/components/table/__tests__/Table.filter.test.js +++ b/components/table/__tests__/Table.filter.test.js @@ -194,4 +194,46 @@ describe('Table.filter', () => { dropdownWrapper.find('.confirm').simulate('click'); expect(renderedNames(wrapper)).toEqual(['Jack']); }); + + it('works with JSX in controlled mode', () => { + const { Column } = Table; + + class App extends React.Component { + state = { + filters: {}, + } + + handleChange = (pagination, filters) => { + this.setState({ filters }); + } + + render() { + return ( + + +
+ ); + } + } + + const wrapper = mount(); + const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent()); + + dropdownWrapper.find('MenuItem').first().simulate('click'); + dropdownWrapper.find('.confirm').simulate('click'); + expect(renderedNames(wrapper)).toEqual(['Jack']); + + dropdownWrapper.find('.clear').simulate('click'); + expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']); + }); });