fix: table column filter reset is not working (#35226) (#35386)

This commit is contained in:
lalalazero 2022-05-05 12:17:15 +08:00 committed by GitHub
parent e039b10b2f
commit 6c0f67e407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 6 deletions

View File

@ -2305,4 +2305,53 @@ describe('Table.filter', () => {
);
expect(errorSpy).not.toBeCalled();
});
it('can reset if filterResetToDefaultFilteredValue and filter is changing', () => {
const wrapper = mount(
createTable({
columns: [
{
...column,
filters: [
{ text: 'Jack', value: 'Jack' },
{ text: 'Lucy', value: 'Lucy' },
],
defaultFilteredValue: ['Jack'],
filterResetToDefaultFilteredValue: true,
},
],
}),
);
expect(wrapper.find('tbody tr').length).toBe(1);
expect(wrapper.find('tbody tr').text()).toBe('Jack');
// open filter
wrapper.find('span.ant-dropdown-trigger').first().simulate('click');
expect(
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').props().disabled,
).toBeTruthy();
expect(wrapper.find('li.ant-dropdown-menu-item').at(0).text()).toBe('Jack');
expect(wrapper.find('li.ant-dropdown-menu-item').at(1).text()).toBe('Lucy');
// deselect default
wrapper.find('li.ant-dropdown-menu-item').at(0).simulate('click');
expect(
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').props().disabled,
).toBeFalsy();
// select other one
wrapper.find('li.ant-dropdown-menu-item').at(1).simulate('click');
expect(
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').props().disabled,
).toBeFalsy();
// deselect other one
wrapper.find('li.ant-dropdown-menu-item').at(1).simulate('click');
expect(
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').props().disabled,
).toBeFalsy();
// select default
wrapper.find('li.ant-dropdown-menu-item').at(0).simulate('click');
expect(
wrapper.find('.ant-table-filter-dropdown-btns .ant-btn-link').props().disabled,
).toBeTruthy();
});
});

View File

@ -407,16 +407,22 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
);
};
const getResetDisabled = () => {
if (filterResetToDefaultFilteredValue) {
return isEqual(
(defaultFilteredValue || []).map(key => String(key)),
selectedKeys,
);
}
return selectedKeys.length === 0;
};
dropdownContent = (
<>
{getFilterComponent()}
<div className={`${prefixCls}-dropdown-btns`}>
<Button
type="link"
size="small"
disabled={selectedKeys.length === 0}
onClick={() => onReset()}
>
<Button type="link" size="small" disabled={getResetDisabled()} onClick={() => onReset()}>
{locale.filterReset}
</Button>
<Button type="primary" size="small" onClick={onConfirm}>