Skip to content

Commit

Permalink
fix: Table onChange did not trigger when current reset to first page a…
Browse files Browse the repository at this point in the history
  • Loading branch information
2724635499 committed Dec 31, 2021
1 parent adbe180 commit a025cf2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ function InternalTable<RecordType extends object = any>(
onPaginationChange,
);

React.useEffect(() => {
if (
mergedPagination &&
pagination &&
mergedPagination.current &&
pagination.current &&
mergedPagination.current !== pagination.current &&
mergedPagination.onChange
) {
mergedPagination.onChange(mergedPagination.current!, mergedPagination?.pageSize!);
}
}, [pagination]);

changeEventInfo.pagination =
pagination === false ? {} : getPaginationParam(pagination, mergedPagination);

Expand Down
57 changes: 57 additions & 0 deletions components/table/__tests__/Table.pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,61 @@ describe('Table.pagination', () => {
'ant-pagination ant-table-pagination ant-table-pagination-right pagination',
);
});

// https://github.com/ant-design/ant-design/issues/33374
// https://codesandbox.io/s/festive-edison-6uq3e?file=/src/App.js
it('should called onChange when page number is changed by change of total ', () => {
const onChange = jest.fn();
const wrapper = mount(
createTable({
pagination: {
current: 2,
pageSize: 3,
total: 4,
onChange,
},
}),
);
wrapper.setProps({
dataSource: data.slice(0, 3),
pagination: {
current: 2,
pageSize: 3,
total: 3,
onChange,
},
});
expect(onChange).toHaveBeenCalledWith(1, 3);
});

// https://github.com/ant-design/ant-design/issues/33487
// https://codesandbox.io/s/focused-chatterjee-y60q6?file=/src/App.js
it('not should called onChange repeat when page total is 0 or null or undefined and dataSource is array and the length of dataSource not greater than 0 ', () => {
const onChange = jest.fn();
const paginationProp = {
onChange,
};
const wrapper = mount(
createTable({
dataSource: [],
pagination: { ...paginationProp },
}),
);
expect(onChange).toHaveBeenCalledTimes(0);
wrapper.setProps({
dataSource: [],
pagination: { ...paginationProp, total: 0 },
});
expect(onChange).toHaveBeenCalledTimes(0);
wrapper.setProps({
dataSource: [],
pagination: { ...paginationProp, total: null },
});
expect(onChange).toHaveBeenCalledTimes(0);
wrapper.setProps({
dataSource: [],
pagination: { ...paginationProp, total: undefined },
});
expect(onChange).toHaveBeenCalledTimes(0);
});
});

0 comments on commit a025cf2

Please sign in to comment.