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: add table onChange an action param #24697

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 26 additions & 10 deletions components/table/Table.tsx
Expand Up @@ -76,6 +76,7 @@ export interface TableProps<RecordType>
filters: Record<string, Key[] | null>,
sorter: SorterResult<RecordType> | SorterResult<RecordType>[],
extra: TableCurrentDataSource<RecordType>,
action: 'paginate' | 'sort' | 'filter',
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
) => void;
rowSelection?: TableRowSelection<RecordType>;

Expand Down Expand Up @@ -178,7 +179,11 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
// ============================ Events =============================
const changeEventInfo: Partial<ChangeEventInfo<RecordType>> = {};

const triggerOnChange = (info: Partial<ChangeEventInfo<RecordType>>, reset: boolean = false) => {
const triggerOnChange = (
info: Partial<ChangeEventInfo<RecordType>>,
action: 'paginate' | 'sort' | 'filter',
reset: boolean = false,
) => {
const changeInfo = {
...changeEventInfo,
...info,
Expand All @@ -205,12 +210,18 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
}

if (onChange) {
onChange(changeInfo.pagination!, changeInfo.filters!, changeInfo.sorter!, {
currentDataSource: getFilterData(
getSortData(rawData, changeInfo.sorterStates!, childrenColumnName),
changeInfo.filterStates!,
),
});
onChange(
changeInfo.pagination!,
changeInfo.filters!,
changeInfo.sorter!,
{
currentDataSource: getFilterData(
getSortData(rawData, changeInfo.sorterStates!, childrenColumnName),
changeInfo.filterStates!,
),
},
Copy link
Member

@afc163 afc163 Jun 6, 2020

Choose a reason for hiding this comment

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

Please add action into 4th argument, instead of adding extra argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought if someone is using the extra argument, moving its position can possible break people apps...
Should i move?

Copy link
Member

Choose a reason for hiding this comment

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

This is for extra argument, to avoid long arguments.

onChange(pagination, filters, sorter, { currentDataSource, xxx, yyy, zzz })

instead of

onChange(pagination, filters, sorter, xxx, yyy, zzz)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, got it

action,
);
}
};

Expand All @@ -231,6 +242,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
sorter,
sorterStates,
},
'sort',
false,
);
};
Expand Down Expand Up @@ -260,6 +272,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
filters,
filterStates,
},
'filter',
true,
);
};
Expand Down Expand Up @@ -288,9 +301,12 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {

// ========================== Pagination ==========================
const onPaginationChange = (current: number, pageSize: number) => {
triggerOnChange({
pagination: { ...changeEventInfo.pagination, current, pageSize },
});
triggerOnChange(
{
pagination: { ...changeEventInfo.pagination, current, pageSize },
},
'paginate',
);
};

const [mergedPagination, resetPagination] = usePagination(
Expand Down
5 changes: 5 additions & 0 deletions components/table/__tests__/Table.filter.test.js
Expand Up @@ -363,6 +363,7 @@ describe('Table.filter', () => {
{
currentDataSource: [],
},
'filter',
);
});

Expand Down Expand Up @@ -942,6 +943,7 @@ describe('Table.filter', () => {
{
currentDataSource: [],
},
'filter',
);
expect(wrapper.find('.ant-pagination-item')).toHaveLength(0);
});
Expand Down Expand Up @@ -973,6 +975,7 @@ describe('Table.filter', () => {
{
currentDataSource: [],
},
'filter',
);
});

Expand Down Expand Up @@ -1045,6 +1048,7 @@ describe('Table.filter', () => {
},
}),
expect.anything(),
'sort',
);

// Filter it
Expand All @@ -1065,6 +1069,7 @@ describe('Table.filter', () => {
},
}),
expect.anything(),
'filter',
);
});

Expand Down
2 changes: 2 additions & 0 deletions components/table/__tests__/Table.pagination.test.js
Expand Up @@ -132,6 +132,7 @@ describe('Table.pagination', () => {
{ key: 3, name: 'Jerry' },
],
},
'paginate',
);

expect(handlePaginationChange).toHaveBeenCalledWith(2, 2);
Expand Down Expand Up @@ -245,6 +246,7 @@ describe('Table.pagination', () => {
{ key: 3, name: 'Jerry' },
],
},
'paginate',
);
expect(onPaginationChange).toHaveBeenCalledWith(2, 10);

Expand Down
4 changes: 3 additions & 1 deletion components/table/index.en-US.md
Expand Up @@ -78,7 +78,7 @@ const columns = [
| size | Size of table | `default` \| `middle` \| `small` | `default` |
| summary | Summary content | (currentData) => ReactNode | - |
| title | Table title renderer | Function(currentPageData) | - |
| onChange | Callback executed when pagination, filters or sorter is changed | Function(pagination, filters, sorter, extra: { currentDataSource: [] }) | - |
| onChange | Callback executed when pagination, filters or sorter is changed | Function(pagination, filters, sorter, extra: { currentDataSource: [] }), action: `paginate` \| `sort` \| `filter` | - |
| onHeaderRow | Set props on per header row | Function(column, index) | - |
| onRow | Set props on per row | Function(record, index) | - |
| getPopupContainer | the render container of dropdowns in table | (triggerNode) => HTMLElement | `() => TableHtmlElement` |
Expand Down Expand Up @@ -289,6 +289,8 @@ Table total page count usually reduce after filter data, we defaultly return to

You may need to keep current page after filtering when fetch data from remote service, please check [this demo](https://codesandbox.io/s/yuanchengjiazaishuju-ant-design-demo-7y2uf) as workaround.

Also you can use the action param to determine when return to first page.

### Why Table pagination show size changer?

In order to improve user experience, Pagination show size changer by default when `total >= 50` since `4.1.0`. You can set `showSizeChanger=false` to disable this feature.
Expand Down
4 changes: 3 additions & 1 deletion components/table/index.zh-CN.md
Expand Up @@ -83,7 +83,7 @@ const columns = [
| size | 表格大小 | `default` \| `middle` \| `small` | default |
| summary | 总结栏 | (currentData) => ReactNode | - |
| title | 表格标题 | Function(currentPageData) | - |
| onChange | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter, extra: { currentDataSource: [] }) | - |
| onChange | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter, extra: { currentDataSource: [] }), action: `paginate` \| `sort` \| `filter` | - |
| onHeaderRow | 设置头部行属性 | Function(column, index) | - |
| onRow | 设置行属性 | Function(record, index) | - |
| getPopupContainer | 设置表格内各类浮层的渲染节点,如筛选菜单 | (triggerNode) => HTMLElement | `() => TableHtmlElement` |
Expand Down Expand Up @@ -293,6 +293,8 @@ Table 移除了在 v3 中废弃的 `onRowClick`、`onRowDoubleClick`、`onRowMou

如果你在使用远程分页,很可能需要保持当前页面,你可以参照这个 [受控例子](https://codesandbox.io/s/yuanchengjiazaishuju-ant-design-demo-7y2uf) 控制当前页面不变。

您還可以使用操作參數來確定何時返回首頁。
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved

### 表格分页为何会出现 size 切换器?

自 `4.1.0` 起,Pagination 在 `total` 大于 50 条时会默认显示 size 切换器以提升用户交互体验。如果你不需要该功能,可以通过设置 `showSizeChanger` 为 `false` 来关闭。
Expand Down