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

Fix pagination state when perPage changes #7213

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ describe('<ReferenceArrayInputController />', () => {
});
});

it('should props compatible with the ListContext', async () => {
it('should props be compatible with the ListContext', async () => {
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
const children = ({
setPage,
setPerPage,
Expand Down Expand Up @@ -869,7 +869,7 @@ describe('<ReferenceArrayInputController />', () => {
},
payload: {
pagination: {
page: 2,
page: 1,
perPage: 50,
},
sort: {
Expand Down
34 changes: 34 additions & 0 deletions packages/ra-core/src/controller/usePaginationState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,38 @@ describe('usePaginationState', () => {
perPage: 100,
});
});

it('should reset the current page to 1 when perPage state changes', () => {
const { hookValue, childrenMock } = renderHook(() =>
usePaginationState()
);
expect(hookValue.pagination).toEqual({ page: 1, perPage: 25 });

act(() => hookValue.setPerPage(100));
WiXSL marked this conversation as resolved.
Show resolved Hide resolved

expect(childrenMock).toBeCalledTimes(2);

act(() => hookValue.setPage(2));

expect(childrenMock.mock.calls[1][0].pagination).toEqual({
page: 1,
perPage: 100,
});
});

it('should reset the current page to initial page when perPage state changes', () => {
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
const { hookValue, childrenMock } = renderHook(() =>
usePaginationState({ page: 2, perPage: 25 })
);
expect(hookValue.pagination).toEqual({ page: 2, perPage: 25 });

act(() => hookValue.setPerPage(100));

expect(childrenMock).toBeCalledTimes(2);

expect(childrenMock.mock.calls[1][0].pagination).toEqual({
page: 2,
perPage: 100,
});
});
});
6 changes: 5 additions & 1 deletion packages/ra-core/src/controller/usePaginationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export default (
});
const isFirstRender = useRef(true);

const setPerPage = useCallback(perPage => setPagination({ perPage }), []);
const setPerPage = useCallback(
perPage =>
setPagination({ perPage, page: initialPagination.page || 1 }),
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
[initialPagination.page]
);
const setPage = useCallback(page => setPagination({ page }), []);

useEffect(() => {
Expand Down