fix: 🐛 Table unchanged filter should not trigger onChange

ref https://github.com/ant-design/ant-design/issues/22819#issuecomment-607171024
This commit is contained in:
afc163 2020-04-01 18:52:37 +08:00 committed by 偏右
parent aca2656721
commit ddb0ac46ba
2 changed files with 46 additions and 32 deletions

View File

@ -11,12 +11,7 @@ import ConfigProvider from '../../config-provider';
const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => {} } }; const nativeEvent = { nativeEvent: { stopImmediatePropagation: () => {} } };
function getDropdownWrapper(wrapper) { function getDropdownWrapper(wrapper) {
return mount( return mount(wrapper.find('Trigger').instance().getComponent());
wrapper
.find('Trigger')
.instance()
.getComponent(),
);
} }
describe('Table.filter', () => { describe('Table.filter', () => {
@ -213,7 +208,10 @@ describe('Table.filter', () => {
expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toBeFalsy(); expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toBeFalsy();
wrapper.find('FilterDropdown').find('input[type="checkbox"]').first().simulate('click'); wrapper.find('FilterDropdown').find('input[type="checkbox"]').first().simulate('click');
wrapper.find('FilterDropdown').find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click'); wrapper
.find('FilterDropdown')
.find('.ant-table-filter-dropdown-btns .ant-btn-primary')
.simulate('click');
expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toEqual(['boy']); expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toEqual(['boy']);
wrapper.setProps({ dataSource: [...data, { key: 999, name: 'Chris' }] }); wrapper.setProps({ dataSource: [...data, { key: 999, name: 'Chris' }] });
expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toEqual(['boy']); expect(wrapper.find('FilterDropdown').props().filterState.filteredKeys).toEqual(['boy']);
@ -342,7 +340,10 @@ describe('Table.filter', () => {
const wrapper = mount(createTable({ onChange: handleChange })); const wrapper = mount(createTable({ onChange: handleChange }));
wrapper.find('.ant-dropdown-trigger').first().simulate('click'); wrapper.find('.ant-dropdown-trigger').first().simulate('click');
wrapper.find('FilterDropdown').find('MenuItem').first().simulate('click'); wrapper.find('FilterDropdown').find('MenuItem').first().simulate('click');
wrapper.find('FilterDropdown').find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click'); wrapper
.find('FilterDropdown')
.find('.ant-table-filter-dropdown-btns .ant-btn-primary')
.simulate('click');
expect(handleChange).toHaveBeenCalledWith( expect(handleChange).toHaveBeenCalledWith(
{}, {},
{ name: ['boy'] }, { name: ['boy'] },
@ -358,12 +359,15 @@ describe('Table.filter', () => {
const wrapper = mount(createTable({ pagination: { onChange: onPaginationChange } })); const wrapper = mount(createTable({ pagination: { onChange: onPaginationChange } }));
wrapper.find('.ant-dropdown-trigger').first().simulate('click'); wrapper.find('.ant-dropdown-trigger').first().simulate('click');
wrapper.find('FilterDropdown').find('MenuItem').first().simulate('click'); wrapper.find('FilterDropdown').find('MenuItem').first().simulate('click');
wrapper.find('FilterDropdown').find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click'); wrapper
.find('FilterDropdown')
.find('.ant-table-filter-dropdown-btns .ant-btn-primary')
.simulate('click');
expect(onPaginationChange).toHaveBeenCalledWith(1, 10); expect(onPaginationChange).toHaveBeenCalledWith(1, 10);
}); });
it('should not fire change event on close filterDropdown without changing anything', () => { it('should not fire change event when close filterDropdown without changing anything', () => {
const handleChange = jest.fn(); const handleChange = jest.fn();
const wrapper = mount(createTable({ onChange: handleChange })); const wrapper = mount(createTable({ onChange: handleChange }));
@ -373,6 +377,26 @@ describe('Table.filter', () => {
expect(handleChange).not.toHaveBeenCalled(); expect(handleChange).not.toHaveBeenCalled();
}); });
it('should not fire change event when close a filtered filterDropdown without changing anything', () => {
const handleChange = jest.fn();
const wrapper = mount(
createTable({
onChange: handleChange,
columns: [
{
...column,
defaultFilteredValue: ['boy', 'designer'],
},
],
}),
);
wrapper.find('.ant-dropdown-trigger').first().simulate('click');
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click');
expect(handleChange).not.toHaveBeenCalled();
});
it('three levels menu', () => { it('three levels menu', () => {
const filters = [ const filters = [
{ text: 'Upper', value: 'Upper' }, { text: 'Upper', value: 'Upper' },
@ -410,29 +434,17 @@ describe('Table.filter', () => {
let dropdownWrapper = getDropdownWrapper(wrapper); let dropdownWrapper = getDropdownWrapper(wrapper);
expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']); expect(renderedNames(wrapper)).toEqual(['Jack', 'Lucy', 'Tom', 'Jerry']);
// select // select
dropdownWrapper dropdownWrapper.find('.ant-dropdown-menu-submenu-title').at(0).simulate('mouseEnter');
.find('.ant-dropdown-menu-submenu-title')
.at(0)
.simulate('mouseEnter');
jest.runAllTimers(); jest.runAllTimers();
dropdownWrapper = getDropdownWrapper(wrapper); dropdownWrapper = getDropdownWrapper(wrapper);
dropdownWrapper dropdownWrapper.find('.ant-dropdown-menu-submenu-title').at(1).simulate('mouseEnter');
.find('.ant-dropdown-menu-submenu-title')
.at(1)
.simulate('mouseEnter');
jest.runAllTimers(); jest.runAllTimers();
dropdownWrapper = getDropdownWrapper(wrapper); dropdownWrapper = getDropdownWrapper(wrapper);
dropdownWrapper dropdownWrapper.find('MenuItem').last().simulate('click');
.find('MenuItem')
.last()
.simulate('click');
dropdownWrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click'); dropdownWrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').simulate('click');
wrapper.update(); wrapper.update();
expect(renderedNames(wrapper)).toEqual(['Jack']); expect(renderedNames(wrapper)).toEqual(['Jack']);
dropdownWrapper dropdownWrapper.find('MenuItem').last().simulate('click');
.find('MenuItem')
.last()
.simulate('click');
jest.useRealTimers(); jest.useRealTimers();
}); });
@ -1060,12 +1072,9 @@ describe('Table.filter', () => {
expect(wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').text()).toEqual( expect(wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-primary').text()).toEqual(
'Bamboo', 'Bamboo',
); );
expect( expect(wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').last().text()).toEqual(
wrapper 'Reset',
.find('.ant-table-filter-dropdown-btns .ant-btn-link') );
.last()
.text(),
).toEqual('Reset');
}); });
it('filtered should work', () => { it('filtered should work', () => {

View File

@ -1,5 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import isEqual from 'lodash/isEqual';
import FilterFilled from '@ant-design/icons/FilterFilled'; import FilterFilled from '@ant-design/icons/FilterFilled';
import Button from '../../../button'; import Button from '../../../button';
import Menu from '../../../menu'; import Menu from '../../../menu';
@ -131,6 +132,10 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
return null; return null;
} }
if (isEqual(mergedKeys, filterState?.filteredKeys)) {
return null;
}
triggerFilter({ triggerFilter({
column, column,
key: columnKey, key: columnKey,