feat: add filterResetToDefaultFilteredValue api (#34355)

* feat: add filterResetToDefaultFilteredValue api

* feat: fix lint

* feat: update test case

* feat: update doc
This commit is contained in:
黑雨 2022-03-23 19:29:05 +08:00 committed by GitHub
parent 6df508d594
commit d4acc661da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 4 deletions

View File

@ -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', () => {
@ -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);
});
});
@ -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');
});
});

View File

@ -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>) {
@ -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 = !!(
@ -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 }) => {

View File

@ -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 |

View File

@ -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 |

View File

@ -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[];