Skip to content

Commit

Permalink
fix: table column filter reset is not working (ant-design#35226)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalazero committed May 5, 2022
1 parent 5a6b3cc commit dd862dd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
49 changes: 49 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Expand Up @@ -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();
});
});
18 changes: 12 additions & 6 deletions components/table/hooks/useFilter/FilterDropdown.tsx
Expand Up @@ -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}>
Expand Down

0 comments on commit dd862dd

Please sign in to comment.