Skip to content

Commit cf66cfe

Browse files
committed
[pagination] Preserve layout for out-of-range pages
Change-Id: I5a8878cfe3d843f444ad7d2b2c754b31e5badd78
1 parent b91d210 commit cf66cfe

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

packages/mui-material/src/usePagination/usePagination.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default function usePagination(props = {}) {
8383
}
8484

8585
const body =
86-
count > 0 && boundaryCount === 0 && siblingCount === 0
86+
count > 0 && page >= 1 && page <= count && boundaryCount === 0 && siblingCount === 0
8787
? [page]
8888
: [
8989
...startPages,

packages/mui-material/src/usePagination/usePagination.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,43 @@ describe('usePagination', () => {
170170
expect(items[1]).to.have.property('disabled', true);
171171
});
172172

173+
it('does not render a stale uncontrolled page when count decreases', () => {
174+
const result = React.createRef();
175+
176+
function TestCase({ count }) {
177+
const hookResult = usePagination({
178+
count,
179+
defaultPage: 11,
180+
boundaryCount: 0,
181+
siblingCount: 0,
182+
});
183+
React.useEffect(() => {
184+
result.current = hookResult;
185+
}, [hookResult]);
186+
return null;
187+
}
188+
189+
const { rerender } = render(<TestCase count={11} />);
190+
expect(serialize(result.current.items)).to.deep.equal(['previous', 11, 'next']);
191+
192+
rerender(<TestCase count={5} />);
193+
expect(serialize(result.current.items)).to.deep.equal([
194+
'previous',
195+
'start-ellipsis',
196+
4,
197+
5,
198+
'next',
199+
]);
200+
});
201+
202+
it('does not render an out-of-range controlled page', () => {
203+
const items = renderHook(() =>
204+
usePagination({ count: 5, page: 0, boundaryCount: 0, siblingCount: 0 }),
205+
).result.current.items;
206+
207+
expect(serialize(items)).to.deep.equal(['previous', 1, 2, 'end-ellipsis', 'next']);
208+
});
209+
173210
it('should support boundaryCount={0}', () => {
174211
const items = renderHook(() =>
175212
usePagination({ count: 11, page: 6, boundaryCount: 0, siblingCount: 1 }),

0 commit comments

Comments
 (0)