From af0c1c92e9f8eaa56ecca1a2aa4dd00bbf03fe4e Mon Sep 17 00:00:00 2001 From: Alan Schio <6757777+schirrel@users.noreply.github.com> Date: Fri, 8 Jul 2022 13:38:16 -0300 Subject: [PATCH 1/4] feat(PaginationProps): Update pagination props --- .../ra-ui-materialui/src/list/pagination/Pagination.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/ra-ui-materialui/src/list/pagination/Pagination.tsx b/packages/ra-ui-materialui/src/list/pagination/Pagination.tsx index f79520c6f5..50e3d8df36 100644 --- a/packages/ra-ui-materialui/src/list/pagination/Pagination.tsx +++ b/packages/ra-ui-materialui/src/list/pagination/Pagination.tsx @@ -13,6 +13,7 @@ import { useListPaginationContext, sanitizeListRestProps, ComponentPropType, + ListPaginationContextValue, } from 'ra-core'; import DefaultPaginationActions from './PaginationActions'; @@ -122,8 +123,9 @@ Pagination.defaultProps = { limit: , rowsPerPageOptions: [5, 10, 25], }; - -export interface PaginationProps extends TablePaginationBaseProps { +export interface PaginationProps + extends TablePaginationBaseProps, + Partial { rowsPerPageOptions?: number[]; actions?: FC; limit?: ReactElement; From 971829707ffcd0f0856103b4ae9e067a505d8b95 Mon Sep 17 00:00:00 2001 From: Alan Schio <6757777+schirrel@users.noreply.github.com> Date: Fri, 8 Jul 2022 13:40:23 -0300 Subject: [PATCH 2/4] test(Pagination): test to work outside of a ListContext --- .../src/list/pagination/Pagination.spec.tsx | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx b/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx index ee8d244234..52d60c5345 100644 --- a/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx +++ b/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx @@ -207,4 +207,33 @@ describe('', () => { ).not.toBeNull(); }); }); + + it('should work outside of a ListContext', () => { + render( + + null} + isLoading={false} + setPerPage={() => {}} + hasNextPage={undefined} + hasPreviousPage={undefined} + perPage={1} + total={2} + page={1} + rowsPerPageOptions={[1]} + /> + + ); + const nextButton = screen.queryByLabelText( + 'ra.navigation.next' + ) as HTMLButtonElement; + expect(nextButton).not.toBeNull(); + expect(nextButton.disabled).toBe(false); + const prevButton = screen.queryByLabelText( + 'ra.navigation.previous' + ) as HTMLButtonElement; + expect(prevButton).not.toBeNull(); + expect(prevButton.disabled).toBe(true); + }); }); From e0436d8dc28a0c47381d0a385ed0aa38e0e345a3 Mon Sep 17 00:00:00 2001 From: Alan Schio <6757777+schirrel@users.noreply.github.com> Date: Fri, 8 Jul 2022 13:44:16 -0300 Subject: [PATCH 3/4] fix(useListPaginationContext): cannot be used outside a ListContext --- .../controller/useListPaginationContext.ts | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/ra-core/src/controller/useListPaginationContext.ts b/packages/ra-core/src/controller/useListPaginationContext.ts index 94afa300ea..be1168635d 100644 --- a/packages/ra-core/src/controller/useListPaginationContext.ts +++ b/packages/ra-core/src/controller/useListPaginationContext.ts @@ -24,25 +24,44 @@ import ListPaginationContext, { */ const useListPaginationContext = (props?: any): ListPaginationContextValue => { const context = useContext(ListPaginationContext); - if (!context.setPage) { - /** - * The element isn't inside a - * - * This may only happen when using Datagrid / SimpleList / SingleFieldList components - * outside of a List / ReferenceManyField / ReferenceArrayField - - * which isn't documented but tolerated. - * To avoid breakage in that case, fallback to props - * - * @deprecated - to be removed in 4.0 - */ - if (process.env.NODE_ENV !== 'production') { - console.log( - "List components must be used inside a . Relying on props rather than context to get List data and callbacks is deprecated and won't be supported in the next major version of react-admin." - ); - } - return props; - } - return context; + return useMemo( + () => + defaults( + {}, + props != null ? extractListPaginationContextProps(props) : {}, + context + ), + [context, props] + ); }; +/** + * Extract only the list controller props + * + * @param {Object} props Props passed to the useListContext hook + * + * @returns {ListControllerResult} List controller props + */ +const extractListPaginationContextProps = ({ + isLoading, + page, + perPage, + setPage, + setPerPage, + hasPreviousPage, + hasNextPage, + total, + resource, +}) => ({ + isLoading, + page, + perPage, + setPage, + setPerPage, + hasPreviousPage, + hasNextPage, + total, + resource, +}); + export default useListPaginationContext; From 67a4c6b28c2f38d073eaebbdf00507e5bf446126 Mon Sep 17 00:00:00 2001 From: Alan Schio Date: Fri, 8 Jul 2022 14:22:57 -0300 Subject: [PATCH 4/4] feat cannot be used outside a ListContext --- packages/ra-core/src/controller/index.ts | 5 ++++- .../controller/useListPaginationContext.ts | 11 ++++------ .../src/list/pagination/Pagination.spec.tsx | 20 ++++--------------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/packages/ra-core/src/controller/index.ts b/packages/ra-core/src/controller/index.ts index d2a8f71f59..9d3ec219e9 100644 --- a/packages/ra-core/src/controller/index.ts +++ b/packages/ra-core/src/controller/index.ts @@ -1,7 +1,9 @@ import ListController from './ListController'; import ListContext from './ListContext'; import ListFilterContext from './ListFilterContext'; -import ListPaginationContext from './ListPaginationContext'; +import ListPaginationContext, { + ListPaginationContextValue, +} from './ListPaginationContext'; import ListSortContext from './ListSortContext'; import ListBase from './ListBase'; import useRecordSelection from './useRecordSelection'; @@ -40,6 +42,7 @@ export { ListContext, ListFilterContext, ListPaginationContext, + ListPaginationContextValue, ListSortContext, ListContextProvider, useCheckMinimumRequiredProps, diff --git a/packages/ra-core/src/controller/useListPaginationContext.ts b/packages/ra-core/src/controller/useListPaginationContext.ts index be1168635d..916bfc90fe 100644 --- a/packages/ra-core/src/controller/useListPaginationContext.ts +++ b/packages/ra-core/src/controller/useListPaginationContext.ts @@ -1,4 +1,5 @@ -import { useContext } from 'react'; +import { useContext, useMemo } from 'react'; +import defaults from 'lodash/defaults'; import ListPaginationContext, { ListPaginationContextValue, @@ -43,23 +44,19 @@ const useListPaginationContext = (props?: any): ListPaginationContextValue => { * @returns {ListControllerResult} List controller props */ const extractListPaginationContextProps = ({ - isLoading, + loading, page, perPage, setPage, setPerPage, - hasPreviousPage, - hasNextPage, total, resource, }) => ({ - isLoading, + loading, page, perPage, setPage, setPerPage, - hasPreviousPage, - hasNextPage, total, resource, }); diff --git a/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx b/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx index 52d60c5345..17d2a7c823 100644 --- a/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx +++ b/packages/ra-ui-materialui/src/list/pagination/Pagination.spec.tsx @@ -207,33 +207,21 @@ describe('', () => { ).not.toBeNull(); }); }); - + it('should work outside of a ListContext', () => { - render( + const { queryByText } = render( null} - isLoading={false} + loading={false} setPerPage={() => {}} - hasNextPage={undefined} - hasPreviousPage={undefined} perPage={1} total={2} page={1} - rowsPerPageOptions={[1]} /> ); - const nextButton = screen.queryByLabelText( - 'ra.navigation.next' - ) as HTMLButtonElement; - expect(nextButton).not.toBeNull(); - expect(nextButton.disabled).toBe(false); - const prevButton = screen.queryByLabelText( - 'ra.navigation.previous' - ) as HTMLButtonElement; - expect(prevButton).not.toBeNull(); - expect(prevButton.disabled).toBe(true); + expect(queryByText('ra.navigation.page_rows_per_page')).not.toBeNull(); }); });