From 575d2932fa43c90d0f669f2f574bf19823bc8a49 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Fri, 6 May 2022 13:37:47 +0200 Subject: [PATCH 1/4] make base components support resource override via props --- .../src/controller/create/CreateBase.tsx | 22 ++++++++++++++----- .../ra-core/src/controller/edit/EditBase.tsx | 22 ++++++++++++++----- .../ra-core/src/controller/show/ShowBase.tsx | 22 ++++++++++++++----- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/packages/ra-core/src/controller/create/CreateBase.tsx b/packages/ra-core/src/controller/create/CreateBase.tsx index 32cd088e2d6..eee6e593ac3 100644 --- a/packages/ra-core/src/controller/create/CreateBase.tsx +++ b/packages/ra-core/src/controller/create/CreateBase.tsx @@ -6,6 +6,7 @@ import { } from './useCreateController'; import { CreateContextProvider } from './CreateContextProvider'; import { RaRecord } from '../../types'; +import { ResourceContextProvider } from '../../core'; /** * Call useCreateController and put the value in a CreateContext @@ -39,8 +40,19 @@ import { RaRecord } from '../../types'; export const CreateBase = ({ children, ...props -}: CreateControllerProps & { children: ReactNode }) => ( - - {children} - -); +}: CreateControllerProps & { children: ReactNode }) => { + const controllerProps = useCreateController(props); + const body = ( + + {children} + + ); + return props.resource ? ( + // support resource override via props + + {body} + + ) : ( + body + ); +}; diff --git a/packages/ra-core/src/controller/edit/EditBase.tsx b/packages/ra-core/src/controller/edit/EditBase.tsx index e35ec541949..8f76073a94f 100644 --- a/packages/ra-core/src/controller/edit/EditBase.tsx +++ b/packages/ra-core/src/controller/edit/EditBase.tsx @@ -4,6 +4,7 @@ import { ReactNode } from 'react'; import { RaRecord } from '../../types'; import { useEditController, EditControllerProps } from './useEditController'; import { EditContextProvider } from './EditContextProvider'; +import { ResourceContextProvider } from '../../core'; /** * Call useEditController and put the value in a EditContext @@ -37,8 +38,19 @@ import { EditContextProvider } from './EditContextProvider'; export const EditBase = ({ children, ...props -}: { children: ReactNode } & EditControllerProps) => ( - (props)}> - {children} - -); +}: { children: ReactNode } & EditControllerProps) => { + const controllerProps = useEditController(props); + const body = ( + + {children} + + ); + return props.resource ? ( + // support resource override via props + + {body} + + ) : ( + body + ); +}; diff --git a/packages/ra-core/src/controller/show/ShowBase.tsx b/packages/ra-core/src/controller/show/ShowBase.tsx index f7a96603a83..65839dcd1f8 100644 --- a/packages/ra-core/src/controller/show/ShowBase.tsx +++ b/packages/ra-core/src/controller/show/ShowBase.tsx @@ -4,6 +4,7 @@ import { ReactElement } from 'react'; import { RaRecord } from '../../types'; import { useShowController, ShowControllerProps } from './useShowController'; import { ShowContextProvider } from './ShowContextProvider'; +import { ResourceContextProvider } from '../../core'; /** * Call useShowController and put the value in a ShowContext @@ -37,8 +38,19 @@ import { ShowContextProvider } from './ShowContextProvider'; export const ShowBase = ({ children, ...props -}: { children: ReactElement } & ShowControllerProps) => ( - (props)}> - {children} - -); +}: { children: ReactElement } & ShowControllerProps) => { + const controllerProps = useShowController(props); + const body = ( + + {children} + + ); + return props.resource ? ( + // support resource override via props + + {body} + + ) : ( + body + ); +}; From c8aaff0c5d00441b41aa403807da0de63b45d9db Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Fri, 6 May 2022 15:23:57 +0200 Subject: [PATCH 2/4] Make Create, Edit and Show comp use their Base counterpart --- .../ra-ui-materialui/src/detail/Create.tsx | 26 +++++-------------- packages/ra-ui-materialui/src/detail/Edit.tsx | 26 +++++-------------- packages/ra-ui-materialui/src/detail/Show.tsx | 14 +++++----- 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/packages/ra-ui-materialui/src/detail/Create.tsx b/packages/ra-ui-materialui/src/detail/Create.tsx index 15948c34374..baf8699c73b 100644 --- a/packages/ra-ui-materialui/src/detail/Create.tsx +++ b/packages/ra-ui-materialui/src/detail/Create.tsx @@ -1,16 +1,11 @@ import * as React from 'react'; import { ReactElement } from 'react'; import PropTypes from 'prop-types'; -import { - CreateContextProvider, - RaRecord, - ResourceContextProvider, - useCheckMinimumRequiredProps, - useCreateController, -} from 'ra-core'; +import { RaRecord, useCheckMinimumRequiredProps } from 'ra-core'; import { CreateProps } from '../types'; import { CreateView } from './CreateView'; +import { CreateBase } from 'ra-core'; /** * Page component for the Create view @@ -59,19 +54,10 @@ export const Create = ( props: CreateProps & { children: ReactElement } ): ReactElement => { useCheckMinimumRequiredProps('Create', ['children'], props); - const controllerProps = useCreateController(props); - const body = ( - - - - ); - return props.resource ? ( - // support resource override via props - - {body} - - ) : ( - body + return ( + + + ); }; diff --git a/packages/ra-ui-materialui/src/detail/Edit.tsx b/packages/ra-ui-materialui/src/detail/Edit.tsx index 5f478a4f98a..aa75b0e26c5 100644 --- a/packages/ra-ui-materialui/src/detail/Edit.tsx +++ b/packages/ra-ui-materialui/src/detail/Edit.tsx @@ -1,15 +1,10 @@ import * as React from 'react'; import { ReactNode } from 'react'; import PropTypes from 'prop-types'; -import { - EditContextProvider, - ResourceContextProvider, - useCheckMinimumRequiredProps, - useEditController, - RaRecord, -} from 'ra-core'; +import { useCheckMinimumRequiredProps, RaRecord } from 'ra-core'; import { EditProps } from '../types'; import { EditView } from './EditView'; +import { EditBase } from 'ra-core'; /** * Page component for the Edit view @@ -60,19 +55,10 @@ export const Edit = ( props: EditProps & { children: ReactNode } ) => { useCheckMinimumRequiredProps('Edit', ['children'], props); - const controllerProps = useEditController(props); - const body = ( - - - - ); - return props.resource ? ( - // support resource override via props - - {body} - - ) : ( - body + return ( + + + ); }; diff --git a/packages/ra-ui-materialui/src/detail/Show.tsx b/packages/ra-ui-materialui/src/detail/Show.tsx index c78673841e1..e71bd60553a 100644 --- a/packages/ra-ui-materialui/src/detail/Show.tsx +++ b/packages/ra-ui-materialui/src/detail/Show.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { ReactElement } from 'react'; import PropTypes from 'prop-types'; -import { ShowBase, ResourceContextProvider, RaRecord } from 'ra-core'; +import { ShowBase, RaRecord } from 'ra-core'; import { ShowProps } from '../types'; import { ShowView } from './ShowView'; @@ -64,11 +64,13 @@ export const Show = ({ queryOptions, ...rest }: ShowProps): ReactElement => ( - - id={id} queryOptions={queryOptions}> - - - + + id={id} + queryOptions={queryOptions} + resource={resource} + > + + ); Show.propTypes = { From c34683e8863f0f65c9a43c2bfa9d7d79cce70358 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Fri, 6 May 2022 16:14:31 +0200 Subject: [PATCH 3/4] split props used by Base and by View --- .../ra-ui-materialui/src/detail/Create.tsx | 18 +++++++++++++-- packages/ra-ui-materialui/src/detail/Edit.tsx | 22 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/ra-ui-materialui/src/detail/Create.tsx b/packages/ra-ui-materialui/src/detail/Create.tsx index baf8699c73b..544cad1e011 100644 --- a/packages/ra-ui-materialui/src/detail/Create.tsx +++ b/packages/ra-ui-materialui/src/detail/Create.tsx @@ -54,9 +54,23 @@ export const Create = ( props: CreateProps & { children: ReactElement } ): ReactElement => { useCheckMinimumRequiredProps('Create', ['children'], props); + const { + resource, + record, + redirect, + transform, + mutationOptions, + ...rest + } = props; return ( - - + + ); }; diff --git a/packages/ra-ui-materialui/src/detail/Edit.tsx b/packages/ra-ui-materialui/src/detail/Edit.tsx index aa75b0e26c5..de44e3ae0bd 100644 --- a/packages/ra-ui-materialui/src/detail/Edit.tsx +++ b/packages/ra-ui-materialui/src/detail/Edit.tsx @@ -55,9 +55,27 @@ export const Edit = ( props: EditProps & { children: ReactNode } ) => { useCheckMinimumRequiredProps('Edit', ['children'], props); + const { + resource, + id, + mutationMode, + mutationOptions, + queryOptions, + redirect, + transform, + ...rest + } = props; return ( - - + + ); }; From 4e81b47da22acc78c9b473fc2d8f9a81816b68f1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Wed, 11 May 2022 10:19:56 +0200 Subject: [PATCH 4/4] code review changes --- .../ra-core/src/controller/create/useCreateController.ts | 2 ++ packages/ra-ui-materialui/src/detail/Create.tsx | 8 ++++++-- packages/ra-ui-materialui/src/detail/Edit.tsx | 2 ++ packages/ra-ui-materialui/src/types.ts | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/ra-core/src/controller/create/useCreateController.ts b/packages/ra-core/src/controller/create/useCreateController.ts index 2b765fa82c7..0ccd13a9bbc 100644 --- a/packages/ra-core/src/controller/create/useCreateController.ts +++ b/packages/ra-core/src/controller/create/useCreateController.ts @@ -157,6 +157,8 @@ export const useCreateController = ( export interface CreateControllerProps { disableAuthentication?: boolean; + hasEdit?: boolean; + hasShow?: boolean; record?: Partial; redirect?: RedirectionSideEffect; resource?: string; diff --git a/packages/ra-ui-materialui/src/detail/Create.tsx b/packages/ra-ui-materialui/src/detail/Create.tsx index 544cad1e011..1c6e1e41a26 100644 --- a/packages/ra-ui-materialui/src/detail/Create.tsx +++ b/packages/ra-ui-materialui/src/detail/Create.tsx @@ -60,6 +60,9 @@ export const Create = ( redirect, transform, mutationOptions, + disableAuthentication, + hasEdit, + hasShow, ...rest } = props; return ( @@ -69,6 +72,9 @@ export const Create = ( redirect={redirect} transform={transform} mutationOptions={mutationOptions} + disableAuthentication={disableAuthentication} + hasEdit={hasEdit} + hasShow={hasShow} > @@ -81,7 +87,6 @@ Create.propTypes = { children: PropTypes.element, className: PropTypes.string, disableAuthentication: PropTypes.bool, - hasCreate: PropTypes.bool, hasEdit: PropTypes.bool, hasShow: PropTypes.bool, redirect: PropTypes.oneOfType([ @@ -92,7 +97,6 @@ Create.propTypes = { resource: PropTypes.string, title: PropTypes.node, record: PropTypes.object, - hasList: PropTypes.bool, mutationOptions: PropTypes.object, transform: PropTypes.func, sx: PropTypes.any, diff --git a/packages/ra-ui-materialui/src/detail/Edit.tsx b/packages/ra-ui-materialui/src/detail/Edit.tsx index de44e3ae0bd..8164782ff4f 100644 --- a/packages/ra-ui-materialui/src/detail/Edit.tsx +++ b/packages/ra-ui-materialui/src/detail/Edit.tsx @@ -63,6 +63,7 @@ export const Edit = ( queryOptions, redirect, transform, + disableAuthentication, ...rest } = props; return ( @@ -74,6 +75,7 @@ export const Edit = ( queryOptions={queryOptions} redirect={redirect} transform={transform} + disableAuthentication={disableAuthentication} > diff --git a/packages/ra-ui-materialui/src/types.ts b/packages/ra-ui-materialui/src/types.ts index 8146aed10f3..261667ad66a 100644 --- a/packages/ra-ui-materialui/src/types.ts +++ b/packages/ra-ui-materialui/src/types.ts @@ -16,6 +16,7 @@ export interface EditProps { aside?: ReactElement; className?: string; component?: ElementType; + disableAuthentication?: boolean; id?: Identifier; mutationMode?: MutationMode; queryOptions?: UseQueryOptions; @@ -36,6 +37,9 @@ export interface CreateProps { aside?: ReactElement; className?: string; component?: ElementType; + disableAuthentication?: boolean; + hasEdit?: boolean; + hasShow?: boolean; record?: Partial; redirect?: RedirectionSideEffect; resource?: string;