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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ import Column from './Column';
import ColumnGroup from './ColumnGroup';
import devWarning from '../_util/devWarning';
import useBreakpoint from '../grid/hooks/useBreakpoint';
import { tuple } from '../_util/type';

export { ColumnsType, TablePaginationConfig };

const EMPTY_LIST: any[] = [];

export const TableActions = tuple('paginate', 'sort', 'filter');
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved

interface ChangeEventInfo<RecordType> {
pagination: {
current?: number;
Expand Down Expand Up @@ -76,6 +79,7 @@ export interface TableProps<RecordType>
filters: Record<string, Key[] | null>,
sorter: SorterResult<RecordType> | SorterResult<RecordType>[],
extra: TableCurrentDataSource<RecordType>,
action: typeof TableActions[number],
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
) => void;
rowSelection?: TableRowSelection<RecordType>;

Expand Down Expand Up @@ -178,7 +182,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: typeof TableActions[number],
yoyo837 marked this conversation as resolved.
Show resolved Hide resolved
reset: boolean = false,
) => {
const changeInfo = {
...changeEventInfo,
...info,
Expand All @@ -205,12 +213,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

@NetoBraghetto NetoBraghetto Jun 8, 2020

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 +245,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
sorter,
sorterStates,
},
'sort',
false,
);
};
Expand Down Expand Up @@ -260,6 +275,7 @@ function Table<RecordType extends object = any>(props: TableProps<RecordType>) {
filters,
filterStates,
},
'filter',
true,
);
};
Expand Down Expand Up @@ -288,9 +304,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion components/table/index.zh-CN.md
Original file line number Diff line number Diff line change
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