fix: Table onChange return cached data (#20858)

This commit is contained in:
二货机器人 2020-01-13 15:04:27 +08:00 committed by GitHub
parent e3b0104c53
commit f470f3e1e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 17 deletions

View File

@ -1104,4 +1104,71 @@ describe('Table.filter', () => {
mount(<TestTable />); mount(<TestTable />);
}); });
// https://github.com/ant-design/ant-design/issues/20854
it('Not cache for onChange state', () => {
const onChange = jest.fn();
const wrapper = mount(
<Table
columns={[
{
title: 'Name',
dataIndex: 'name',
sorter: true,
},
{
title: 'Gender',
dataIndex: 'gender',
filters: [
{ text: 'Male', value: 'male' },
{ text: 'Female', value: 'female' },
],
},
]}
dataSource={[]}
onChange={onChange}
/>,
);
// Sort it
wrapper.find('.ant-table-column-sorters').simulate('click');
expect(onChange).toHaveBeenCalledWith(
expect.anything(),
{
gender: null,
},
expect.objectContaining({
column: {
dataIndex: 'name',
sorter: true,
title: 'Name',
},
}),
expect.anything(),
);
// Filter it
onChange.mockReset();
wrapper.find('span.ant-dropdown-trigger').simulate('click', nativeEvent);
wrapper
.find('.ant-dropdown-menu-item')
.first()
.simulate('click');
wrapper.find('.ant-table-filter-dropdown-link.confirm').simulate('click');
expect(onChange).toHaveBeenCalledWith(
expect.anything(),
{
gender: ['male'],
},
expect.objectContaining({
column: {
dataIndex: 'name',
sorter: true,
title: 'Name',
},
}),
expect.anything(),
);
});
}); });

View File

@ -208,18 +208,16 @@ function useFilter<RecordType>({
onFilterChange(generateFilterInfo(newFilterStates), newFilterStates); onFilterChange(generateFilterInfo(newFilterStates), newFilterStates);
}; };
const transformColumns = React.useMemo(() => { const transformColumns = (innerColumns: ColumnsType<RecordType>) =>
return (innerColumns: ColumnsType<RecordType>) => injectFilter(
injectFilter( prefixCls,
prefixCls, dropdownPrefixCls,
dropdownPrefixCls, innerColumns,
innerColumns, mergedFilterStates,
mergedFilterStates, triggerFilter,
triggerFilter, getPopupContainer,
getPopupContainer, tableLocale,
tableLocale, );
);
}, [mergedFilterStates]);
return [transformColumns, mergedFilterStates, getFilters]; return [transformColumns, mergedFilterStates, getFilters];
} }

View File

@ -362,11 +362,8 @@ export default function useFilterSorter<RecordType>({
onSorterChange(generateSorterInfo(newSorterStates), newSorterStates); onSorterChange(generateSorterInfo(newSorterStates), newSorterStates);
} }
const transformColumns = React.useCallback( const transformColumns = (innerColumns: ColumnsType<RecordType>) =>
(innerColumns: ColumnsType<RecordType>) => injectSorter(prefixCls, innerColumns, mergedSorterStates, triggerSorter, sortDirections);
injectSorter(prefixCls, innerColumns, mergedSorterStates, triggerSorter, sortDirections),
[mergedSorterStates],
);
const getSorters = () => { const getSorters = () => {
return generateSorterInfo(mergedSorterStates); return generateSorterInfo(mergedSorterStates);