Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/Table/Hooks/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export default function usePagination(
}
);

// A changed pageSizes prop can orphan the pageSize persisted by Pagination's mount effect
const { pageSize, pageSizes } = mergedPagination;
if (pageSizes && pageSize !== undefined && !pageSizes.includes(pageSize)) {
mergedPagination.pageSize = pageSizes[0];
}

// Reset `current` if data length or pageSize changed
const pages: number[] = mergedPagination.pageSizes!;
let maxPage: number;
Expand Down
8 changes: 8 additions & 0 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ function InternalTable<RecordType extends object = any>(
);
}
}

// pageSize not in pageSizes: fall back to the first size instead of rendering empty
const fallbackPageSize: number = pageSizes[0] ?? DEFAULT_PAGE_SIZE;
mergedPagination.pageSize = fallbackPageSize;
return mergedData.slice(
(currentPage - 1) * fallbackPageSize,
currentPage * fallbackPageSize
);
} else {
if (mergedData.length < total!) {
if (mergedData.length > pageSize) {
Expand Down
22 changes: 22 additions & 0 deletions src/components/Table/Tests/Table.pagination.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import Enzyme, { mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import Table from '../index';
Expand Down Expand Up @@ -77,6 +78,27 @@ describe('Table.table-pagination', () => {
expect(renderedNames(wrapper)).toEqual(['Lola']);
});

it('keeps rendering rows when a dynamic pageSizes prop changes value', () => {
// act() flushes Pagination's mount effect, which persists the initial size
let wrapper;
act(() => {
wrapper = mount(
createTable({
dataSource: longData,
pagination: { pageSizes: [10] },
})
);
});
wrapper.update();
expect(renderedNames(wrapper)).toHaveLength(10);

act(() => {
wrapper.setProps({ pagination: { pageSizes: [9] } });
});
wrapper.update();
expect(renderedNames(wrapper)).toHaveLength(9);
});

it('should not crash when trigger onChange in render', () => {
function App() {
const [page, setPage] = React.useState({
Expand Down
Loading