Skip to content

Commit

Permalink
fix: Table filterDropdown confirm closeDropdown (#30457)
Browse files Browse the repository at this point in the history
* fix: Table filterDropdown confirm closeDropdown

close #30454

* add test case

* improve demo
  • Loading branch information
afc163 committed May 10, 2021
1 parent b2a35f0 commit dd14752
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
43 changes: 43 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@ describe('Table.filter', () => {

expect(wrapper.find('.ant-table-filter-column')).toHaveLength(3);
});

it('should pagination.current be 1 after filtering', () => {
const onChange = jest.fn();
const columns = [
Expand Down Expand Up @@ -1562,4 +1563,46 @@ describe('Table.filter', () => {
wrapper.find('.ant-btn-primary').first().simulate('click');
expect(onChange.mock.calls[1][0].current).toBe(1);
});

// https://github.com/ant-design/ant-design/issues/30454
it('should not trigger onFilterDropdownVisibleChange when call confirm({ closeDropdown: false })', () => {
const onFilterDropdownVisibleChange = jest.fn();
const wrapper = mount(
createTable({
columns: [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
filteredValue: name,
filterDropdown: ({ confirm }) => (
<>
<button id="confirm-and-close" type="button" onClick={() => confirm()}>
confirm
</button>
<button
id="confirm-only"
type="button"
onClick={() => confirm({ closeDropdown: false })}
>
confirm
</button>
</>
),
onFilterDropdownVisibleChange,
},
],
}),
);

wrapper.find('.ant-dropdown-trigger').first().simulate('click');
expect(onFilterDropdownVisibleChange).toHaveBeenCalledTimes(1);

wrapper.find('#confirm-only').simulate('click');
expect(onFilterDropdownVisibleChange).toHaveBeenCalledTimes(1);

wrapper.find('#confirm-and-close').simulate('click');
expect(onFilterDropdownVisibleChange).toHaveBeenCalledTimes(2);
expect(onFilterDropdownVisibleChange).toHaveBeenLastCalledWith(false);
});
});
2 changes: 1 addition & 1 deletion components/table/demo/custom-filter-panel.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class App extends React.Component {
value={selectedKeys[0]}
onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
onPressEnter={() => this.handleSearch(selectedKeys, confirm, dataIndex)}
style={{ width: 188, marginBottom: 8, display: 'block' }}
style={{ marginBottom: 8, display: 'block' }}
/>
<Space>
<Button
Expand Down
15 changes: 5 additions & 10 deletions components/table/hooks/useFilter/FilterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import Checkbox from '../../../checkbox';
import Radio from '../../../radio';
import Dropdown from '../../../dropdown';
import Empty from '../../../empty';
import {
ColumnType,
ColumnFilterItem,
Key,
TableLocale,
GetPopupContainer,
FilterConfirmProps,
} from '../../interface';
import { ColumnType, ColumnFilterItem, Key, TableLocale, GetPopupContainer } from '../../interface';
import FilterDropdownMenuWrapper from './FilterWrapper';
import { FilterState } from '.';
import useSyncState from '../../../_util/hooks/useSyncState';
Expand Down Expand Up @@ -192,8 +185,10 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
internalTriggerFilter([]);
};

const doFilter = (param: FilterConfirmProps = { closeDropdown: true }) => {
triggerVisible(!param.closeDropdown);
const doFilter = ({ closeDropdown } = { closeDropdown: true }) => {
if (closeDropdown) {
triggerVisible(false);
}
internalTriggerFilter(getFilteredKeysSync());
};

Expand Down

0 comments on commit dd14752

Please sign in to comment.