From 0a641d48f198d68c035c58b8428b2e9520aba265 Mon Sep 17 00:00:00 2001 From: fzaninotto Date: Mon, 9 Nov 2020 13:28:21 +0100 Subject: [PATCH] Change RecordContext in Reference elements --- .../field/useReferenceArrayFieldController.ts | 2 +- .../src/field/ReferenceArrayField.tsx | 20 +++++-- .../src/field/ReferenceField.tsx | 23 ++++---- .../src/field/ReferenceManyField.tsx | 9 ++- .../src/input/ReferenceArrayInput.tsx | 56 ++++++++++--------- .../src/input/ReferenceInput.tsx | 46 ++++++++------- .../src/list/datagrid/Datagrid.tsx | 1 - 7 files changed, 91 insertions(+), 66 deletions(-) diff --git a/packages/ra-core/src/controller/field/useReferenceArrayFieldController.ts b/packages/ra-core/src/controller/field/useReferenceArrayFieldController.ts index ac95d0bcc3a..643388f6092 100644 --- a/packages/ra-core/src/controller/field/useReferenceArrayFieldController.ts +++ b/packages/ra-core/src/controller/field/useReferenceArrayFieldController.ts @@ -216,7 +216,7 @@ const useReferenceArrayFieldController = ( onUnselectItems, page, perPage, - resource, + resource: reference, selectedIds, setFilters, setPage, diff --git a/packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx b/packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx index 4d9ef1512fb..5da928cb5ab 100644 --- a/packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx +++ b/packages/ra-ui-materialui/src/field/ReferenceArrayField.tsx @@ -10,6 +10,7 @@ import { useReferenceArrayFieldController, SortPayload, FilterPayload, + ResourceContextProvider, } from 'ra-core'; import { fieldPropTypes, PublicFieldProps, InjectedFieldProps } from './types'; @@ -103,9 +104,11 @@ const ReferenceArrayField: FC = props => { source, }); return ( - - - + + + + + ); }; @@ -160,7 +163,14 @@ export interface ReferenceArrayFieldViewProps } export const ReferenceArrayFieldView: FC = props => { - const { children, pagination, className, reference, ...rest } = props; + const { + children, + pagination, + className, + resource, + reference, + ...rest + } = props; const classes = useStyles(props); const { loaded } = useListContext(props); @@ -173,7 +183,7 @@ export const ReferenceArrayFieldView: FC = props = {cloneElement(Children.only(children), { ...sanitizeFieldRestProps(rest), className, - resource: reference, + resource, })}{' '} {pagination && props.total !== undefined && diff --git a/packages/ra-ui-materialui/src/field/ReferenceField.tsx b/packages/ra-ui-materialui/src/field/ReferenceField.tsx index 14a0cf9fd49..1ce3bc29731 100644 --- a/packages/ra-ui-materialui/src/field/ReferenceField.tsx +++ b/packages/ra-ui-materialui/src/field/ReferenceField.tsx @@ -10,6 +10,7 @@ import { UseReferenceProps, getResourceLinkPath, LinkToType, + ResourceContextProvider, } from 'ra-core'; import LinearProgress from '../layout/LinearProgress'; @@ -82,16 +83,18 @@ const ReferenceField: FC = ({ }); return ( - - {children} - + + + {children} + + ); }; diff --git a/packages/ra-ui-materialui/src/field/ReferenceManyField.tsx b/packages/ra-ui-materialui/src/field/ReferenceManyField.tsx index d9d19ff0302..90bc1438e95 100644 --- a/packages/ra-ui-materialui/src/field/ReferenceManyField.tsx +++ b/packages/ra-ui-materialui/src/field/ReferenceManyField.tsx @@ -6,6 +6,7 @@ import { useReferenceManyFieldController, ListContextProvider, ListControllerProps, + ResourceContextProvider, } from 'ra-core'; import { PublicFieldProps, fieldPropTypes, InjectedFieldProps } from './types'; @@ -92,9 +93,11 @@ export const ReferenceManyField: FC = props => { }); return ( - - - + + + + + ); }; diff --git a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.tsx b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.tsx index 791ce2c39f9..5f7f62eabb0 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.tsx +++ b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.tsx @@ -9,6 +9,7 @@ import { SortPayload, PaginationPayload, Translate, + ResourceContextProvider, } from 'ra-core'; import sanitizeInputRestProps from './sanitizeInputRestProps'; @@ -232,6 +233,7 @@ export const ReferenceArrayInputView = ({ meta, onChange, options, + reference, resource, setFilter, setPagination, @@ -264,31 +266,35 @@ export const ReferenceArrayInputView = ({ return ; } - return React.cloneElement(children, { - allowEmpty, - basePath, - choices, - className, - error, - input, - isRequired, - label: translatedLabel, - meta: { - ...meta, - helperText: warning || false, - }, - onChange, - options, - resource, - setFilter, - setPagination, - setSort, - source, - translateChoice: false, - limitChoicesToValue: true, - ...sanitizeRestProps(rest), - ...children.props, - }); + return ( + + {React.cloneElement(children, { + allowEmpty, + basePath, + choices, + className, + error, + input, + isRequired, + label: translatedLabel, + meta: { + ...meta, + helperText: warning || false, + }, + onChange, + options, + resource, + setFilter, + setPagination, + setSort, + source, + translateChoice: false, + limitChoicesToValue: true, + ...sanitizeRestProps(rest), + ...children.props, + })} + + ); }; ReferenceArrayInputView.propTypes = { diff --git a/packages/ra-ui-materialui/src/input/ReferenceInput.tsx b/packages/ra-ui-materialui/src/input/ReferenceInput.tsx index eaec9e0887e..ae2653a632a 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceInput.tsx +++ b/packages/ra-ui-materialui/src/input/ReferenceInput.tsx @@ -13,6 +13,7 @@ import { ListContextProvider, ReferenceInputValue, UseInputValue, + ResourceContextProvider, } from 'ra-core'; import sanitizeInputRestProps from './sanitizeInputRestProps'; @@ -206,6 +207,7 @@ export const ReferenceInputView: FunctionComponent = ({ meta, possibleValues, resource, + reference, setFilter, setPagination, setSort, @@ -260,27 +262,29 @@ export const ReferenceInputView: FunctionComponent = ({ const disabledHelperText = helperText === false ? { helperText } : {}; return ( - - {cloneElement(children, { - allowEmpty, - classes, - className, - input, - isRequired, - label, - resource, - meta: finalMeta, - source, - choices, - basePath, - setFilter, - setPagination, - setSort, - translateChoice: false, - ...disabledHelperText, - ...sanitizeRestProps(rest), - })} - + + + {cloneElement(children, { + allowEmpty, + classes, + className, + input, + isRequired, + label, + resource, + meta: finalMeta, + source, + choices, + basePath, + setFilter, + setPagination, + setSort, + translateChoice: false, + ...disabledHelperText, + ...sanitizeRestProps(rest), + })} + + ); }; diff --git a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx index 1f5124c9696..be0ac217b5a 100644 --- a/packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx +++ b/packages/ra-ui-materialui/src/list/datagrid/Datagrid.tsx @@ -14,7 +14,6 @@ import { useVersion, Identifier, Record, - useResourceContext, } from 'ra-core'; import { Checkbox,