diff --git a/components/table/Table.tsx b/components/table/Table.tsx index f409d30c5c6d..1e9a1ca6d3df 100644 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -330,6 +330,19 @@ function InternalTable( 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); diff --git a/components/table/__tests__/Table.pagination.test.js b/components/table/__tests__/Table.pagination.test.js index b2ca15c5e4c1..bbb4ad298bd3 100644 --- a/components/table/__tests__/Table.pagination.test.js +++ b/components/table/__tests__/Table.pagination.test.js @@ -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); + }); });