Skip to content

Commit

Permalink
feat: add filterResetToDefaultFilteredValue api (ant-design#34355)
Browse files Browse the repository at this point in the history
* feat: add filterResetToDefaultFilteredValue api

* feat: fix lint

* feat: update test case

* feat: update doc
  • Loading branch information
heiyu4585 authored Mar 23, 2022
1 parent 6df508d commit d4acc66
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
67 changes: 65 additions & 2 deletions components/table/__tests__/Table.filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,11 @@ describe('Table.filter', () => {
expect(wrapper.find('.ant-tree-checkbox').at(0).hasClass('ant-tree-checkbox-checked')).toBe(
true,
);
expect(wrapper.find('.ant-table-filter-dropdown-checkall .ant-checkbox').hasClass('ant-checkbox-indeterminate')).toBe(true);
expect(
wrapper
.find('.ant-table-filter-dropdown-checkall .ant-checkbox')
.hasClass('ant-checkbox-indeterminate'),
).toBe(true);
});

it('select-all checkbox should change when all items are selected', () => {
Expand All @@ -1991,7 +1995,11 @@ describe('Table.filter', () => {
});
wrapper.find('.ant-tree-node-content-wrapper').at(0).simulate('click');
wrapper.find('.ant-tree-node-content-wrapper').at(1).simulate('click');
expect(wrapper.find('.ant-table-filter-dropdown-checkall .ant-checkbox').hasClass('ant-checkbox-checked')).toBe(true);
expect(
wrapper
.find('.ant-table-filter-dropdown-checkall .ant-checkbox')
.hasClass('ant-checkbox-checked'),
).toBe(true);
});
});

Expand Down Expand Up @@ -2189,4 +2197,59 @@ describe('Table.filter', () => {
expect(wrapper.find('Dropdown').first().props().visible).toBe(res2);
});
});

it('filterDropDown should support filterResetToDefaultFilteredValue', () => {
jest.useFakeTimers();
jest.spyOn(console, 'error').mockImplementation(() => undefined);

const columnFilter = {
...column,
filterMode: 'tree',
filterSearch: true,
defaultFilteredValue: ['girl'],
};

let wrapper = mount(
createTable({
columns: [columnFilter],
}),
);
wrapper.find('span.ant-dropdown-trigger').simulate('click', nativeEvent);
act(() => {
jest.runAllTimers();
wrapper.update();
});
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(1);
wrapper
.find(Checkbox)
.find('input')
.simulate('change', { target: { checked: true } });
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(5);
wrapper.find('button.ant-btn-link').simulate('click', nativeEvent);
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(0);

wrapper = mount(
createTable({
columns: [
{
...columnFilter,
filterResetToDefaultFilteredValue: true,
},
],
}),
);
wrapper.find('span.ant-dropdown-trigger').simulate('click', nativeEvent);
act(() => {
jest.runAllTimers();
wrapper.update();
});
wrapper
.find(Checkbox)
.find('input')
.simulate('change', { target: { checked: true } });
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(5);
wrapper.find('button.ant-btn-link').simulate('click', nativeEvent);
expect(wrapper.find('.ant-tree-checkbox-checked').length).toBe(1);
expect(wrapper.find('.ant-tree-checkbox-checked+span').text()).toBe('Girl');
});
});
16 changes: 14 additions & 2 deletions components/table/hooks/useFilter/FilterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export interface FilterDropdownProps<RecordType> {
triggerFilter: (filterState: FilterState<RecordType>) => void;
locale: TableLocale;
getPopupContainer?: GetPopupContainer;
filterResetToDefaultFilteredValue?: boolean;
}

function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
Expand All @@ -130,7 +131,12 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
getPopupContainer,
} = props;

const { filterDropdownVisible, onFilterDropdownVisibleChange } = column;
const {
filterDropdownVisible,
onFilterDropdownVisibleChange,
filterResetToDefaultFilteredValue,
defaultFilteredValue,
} = column;
const [visible, setVisible] = React.useState(false);

const filtered: boolean = !!(
Expand Down Expand Up @@ -231,8 +237,14 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
if (closeDropdown) {
triggerVisible(false);
}

setSearchValue('');
setFilteredKeysSync([]);

if (filterResetToDefaultFilteredValue) {
setFilteredKeysSync((defaultFilteredValue || []).map(key => String(key)));
} else {
setFilteredKeysSync([]);
}
};

const doFilter = ({ closeDropdown } = { closeDropdown: true }) => {
Expand Down
1 change: 1 addition & 0 deletions components/table/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| colSpan | Span of this column's title | number | - | |
| dataIndex | Display field of the data record, support nest path by string array | string \| string\[] | - | |
| defaultFilteredValue | Default filtered values | string\[] | - | |
| filterResetToDefaultFilteredValue | click the reset button, whether to restore the default filter | boolean | false | |
| defaultSortOrder | Default order of sorted values | `ascend` \| `descend` | - | |
| editable | Whether column can be edited | boolean | false | |
| ellipsis | The ellipsis cell content, not working with sorter and filters for now.<br />tableLayout would be `fixed` when `ellipsis` is `true` or `{ showTitle?: boolean }` | boolean \| {showTitle?: boolean } | false | showTitle: 4.3.0 |
Expand Down
1 change: 1 addition & 0 deletions components/table/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const columns = [
| colSpan | 表头列合并,设置为 0 时,不渲染 | number | - | |
| dataIndex | 列数据在数据项中对应的路径,支持通过数组查询嵌套路径 | string \| string\[] | - | |
| defaultFilteredValue | 默认筛选值 | string\[] | - | |
| filterResetToDefaultFilteredValue | 点击重置按钮的时候,是否恢复默认筛选值 | boolean | false | |
| defaultSortOrder | 默认排序顺序 | `ascend` \| `descend` | - | |
| editable | 是否可编辑 | boolean | false | |
| ellipsis | 超过宽度将自动省略,暂不支持和排序筛选一起使用。<br />设置为 `true``{ showTitle?: boolean }` 时,表格布局将变成 `tableLayout="fixed"`| boolean \| { showTitle?: boolean } | false | showTitle: 4.3.0 |
Expand Down
1 change: 1 addition & 0 deletions components/table/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface ColumnType<RecordType> extends RcColumnType<RecordType> {
onFilter?: (value: string | number | boolean, record: RecordType) => boolean;
filterDropdownVisible?: boolean;
onFilterDropdownVisibleChange?: (visible: boolean) => void;
filterResetToDefaultFilteredValue?: boolean;

// Responsive
responsive?: Breakpoint[];
Expand Down

0 comments on commit d4acc66

Please sign in to comment.