Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): add filterOnClose prop for Column #47451

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions components/table/__tests__/Table.filter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2855,4 +2855,23 @@ describe('Table.filter', () => {
// User opens filter Dropdown.
fireEvent.click(container.querySelector('.ant-dropdown-trigger.ant-table-filter-trigger')!);
});

it('should not fire change event when dropdown dismisses if filterOnClose is false', () => {
const handleChange = jest.fn();
const { container } = render(
createTable({
onChange: handleChange,
columns: [
{
...column,
filterOnClose: false,
},
],
}),
);
fireEvent.click(container.querySelector('.ant-dropdown-trigger')!);
fireEvent.click(container.querySelectorAll('.ant-dropdown-menu-item')[0]);
fireEvent.click(container.querySelector('.ant-dropdown-trigger')!);
expect(handleChange).not.toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions components/table/demo/head.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## zh-CN

对某一列数据进行筛选,使用列的 `filters` 属性来指定需要筛选菜单的列,`onFilter` 用于筛选当前数据,`filterMultiple` 用于指定多选和单选。
对某一列数据进行筛选,使用列的 `filters` 属性来指定需要筛选菜单的列,`onFilter` 用于筛选当前数据,`filterMultiple` 用于指定多选和单选,`filterOnClose` 用于指定是否在筛选菜单关闭时触发筛选

使用 `defaultFilteredValue` 属性,设置列的默认筛选项。

Expand All @@ -14,7 +14,7 @@

## en-US

Use `filters` to generate filter menu in columns, `onFilter` to determine filtered result, and `filterMultiple` to indicate whether it's multiple or single selection.
Use `filters` to generate filter menu in columns, `onFilter` to determine filtered result, and `filterMultiple` to indicate whether it's multiple or single selection, `filterOnClose` to specify whether to trigger filter when the filter menu closes.

Use `defaultFilteredValue` to make a column filtered by default.

Expand Down
5 changes: 3 additions & 2 deletions components/table/hooks/useFilter/FilterDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export interface FilterDropdownProps<RecordType> {
dropdownPrefixCls: string;
column: ColumnType<RecordType>;
filterState?: FilterState<RecordType>;
filterOnClose: boolean;
filterMultiple: boolean;
filterMode?: 'menu' | 'tree';
filterSearch?: FilterSearchType<ColumnFilterItem | TreeColumnFilterItem>;
Expand All @@ -147,6 +148,7 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {
column,
dropdownPrefixCls,
columnKey,
filterOnClose,
filterMultiple,
filterMode = 'menu',
filterSearch = false,
Expand Down Expand Up @@ -306,8 +308,7 @@ function FilterDropdown<RecordType>(props: FilterDropdownProps<RecordType>) {

triggerVisible(newVisible);

// Default will filter when closed
if (!newVisible && !column.filterDropdown) {
if (!newVisible && !column.filterDropdown && filterOnClose) {
onConfirm();
}
}
Expand Down
8 changes: 7 additions & 1 deletion components/table/hooks/useFilter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ function injectFilter<RecordType>(
): ColumnsType<RecordType> {
return columns.map((column, index) => {
const columnPos = getColumnPos(index, pos);
const { filterMultiple = true, filterMode, filterSearch } = column as ColumnType<RecordType>;
const {
filterOnClose = true,
filterMultiple = true,
filterMode,
filterSearch,
} = column as ColumnType<RecordType>;

let newColumn: ColumnsType<RecordType>[number] = column;

Expand All @@ -98,6 +103,7 @@ function injectFilter<RecordType>(
column={newColumn}
columnKey={columnKey}
filterState={filterState}
filterOnClose={filterOnClose}
filterMultiple={filterMultiple}
filterMode={filterMode}
filterSearch={filterSearch}
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 @@ -192,6 +192,7 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| filtered | Whether the `dataSource` is filtered | boolean | false | |
| filteredValue | Controlled filtered value, filter icon will highlight | string\[] | - | |
| filterIcon | Customized filter icon | ReactNode \| (filtered: boolean) => ReactNode | - | |
| filterOnClose | Whether to trigger filter when the filter menu closes | boolean | true | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

版本号没写

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

阿巴阿巴🤦‍♀️

| filterMultiple | Whether multiple filters can be selected | boolean | true | |
| filterMode | To specify the filter interface | 'menu' \| 'tree' | 'menu' | 4.17.0 |
| filterSearch | Whether to be searchable for filter menu | boolean \| function(input, record):boolean | false | boolean:4.17.0 function:4.19.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 @@ -193,6 +193,7 @@ const columns = [
| filtered | 标识数据是否经过过滤,筛选图标会高亮 | boolean | false | |
| filteredValue | 筛选的受控属性,外界可用此控制列的筛选状态,值为已筛选的 value 数组 | string\[] | - | |
| filterIcon | 自定义 filter 图标。 | ReactNode \| (filtered: boolean) => ReactNode | false | |
| filterOnClose | 是否在筛选菜单关闭时触发筛选 | boolean | true | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| filterMultiple | 是否多选 | boolean | true | |
| filterMode | 指定筛选菜单的用户界面 | 'menu' \| 'tree' | 'menu' | 4.17.0 |
| filterSearch | 筛选菜单项是否可搜索 | boolean \| function(input, record):boolean | false | boolean:4.17.0 function:4.19.0 |
Expand Down
1 change: 1 addition & 0 deletions components/table/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export interface ColumnType<RecordType> extends Omit<RcColumnType<RecordType>, '
filtered?: boolean;
filters?: ColumnFilterItem[];
filterDropdown?: React.ReactNode | ((props: FilterDropdownProps) => React.ReactNode);
filterOnClose?: boolean;
filterMultiple?: boolean;
filteredValue?: FilterValue | null;
defaultFilteredValue?: FilterValue | null;
Expand Down