From cdf0ce4b9585b67aa343e0ad22cb56490939e228 Mon Sep 17 00:00:00 2001 From: Gildas Garcia Date: Tue, 17 Sep 2019 14:04:57 +0200 Subject: [PATCH 1/3] Migrate ReferenceArrayInput to use hooks --- .../ra-core/src/controller/input/index.ts | 2 + .../src/input/ReferenceArrayInput.js | 53 +++++++++++++------ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/packages/ra-core/src/controller/input/index.ts b/packages/ra-core/src/controller/input/index.ts index 2d84189d5da..7f533494999 100644 --- a/packages/ra-core/src/controller/input/index.ts +++ b/packages/ra-core/src/controller/input/index.ts @@ -1,9 +1,11 @@ import ReferenceArrayInputController from './ReferenceArrayInputController'; import ReferenceInputController from './ReferenceInputController'; import useReferenceInputController from './useReferenceInputController'; +import useReferenceArrayInputController from './useReferenceArrayInputController'; export { ReferenceArrayInputController, ReferenceInputController, useReferenceInputController, + useReferenceArrayInputController, }; diff --git a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js index fd9e7619b94..b96fa80f21b 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js +++ b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js @@ -1,7 +1,10 @@ import React from 'react'; import PropTypes from 'prop-types'; -import compose from 'recompose/compose'; -import { addField, translate, ReferenceArrayInputController } from 'ra-core'; +import { + useReferenceArrayInputController, + useInput, + useTranslate, +} from 'ra-core'; import LinearProgress from '../layout/LinearProgress'; import Labeled from '../input/Labeled'; @@ -209,22 +212,43 @@ ReferenceArrayInputView.propTypes = { * * */ -export const ReferenceArrayInput = ({ children, ...props }) => { +export const ReferenceArrayInput = ({ + children, + id: idOverride, + onBlur, + onChange, + onFocus, + validate, + ...props +}) => { if (React.Children.count(children) !== 1) { throw new Error( ' only accepts a single child (like )' ); } + const { id, input, isRequired, meta } = useInput({ + id: idOverride, + onBlur, + onChange, + onFocus, + validate, + }); + + const controllerProps = useReferenceArrayInputController(); + + const translate = useTranslate(); + return ( - - {controllerProps => ( - - )} - + ); }; @@ -257,9 +281,4 @@ ReferenceArrayInput.defaultProps = { sort: { field: 'id', order: 'DESC' }, }; -const EnhancedReferenceArrayInput = compose( - addField, - translate -)(ReferenceArrayInput); - -export default EnhancedReferenceArrayInput; +export default ReferenceArrayInput; From c49f7308cbde85390b9a9a1b1164eb899b0cc08b Mon Sep 17 00:00:00 2001 From: Gildas Garcia Date: Tue, 17 Sep 2019 14:27:07 +0200 Subject: [PATCH 2/3] Review --- packages/ra-ui-materialui/src/input/ReferenceArrayInput.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js index b96fa80f21b..3aac1ee8f02 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js +++ b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js @@ -246,8 +246,9 @@ export const ReferenceArrayInput = ({ isRequired={isRequired} meta={meta} translate={translate} + children={children} {...props} - {...{ children, ...controllerProps }} + {...controllerProps} /> ); }; From 552d1be80a5e07b13f38125d6cde4ead8cf89940 Mon Sep 17 00:00:00 2001 From: Gildas Garcia Date: Tue, 17 Sep 2019 15:07:17 +0200 Subject: [PATCH 3/3] Fix component --- .../ra-ui-materialui/src/input/ReferenceArrayInput.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js index 3aac1ee8f02..b809d4fb24d 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js +++ b/packages/ra-ui-materialui/src/input/ReferenceArrayInput.js @@ -232,10 +232,14 @@ export const ReferenceArrayInput = ({ onBlur, onChange, onFocus, + source: props.source, validate, }); - const controllerProps = useReferenceArrayInputController(); + const controllerProps = useReferenceArrayInputController({ + ...props, + input, + }); const translate = useTranslate(); @@ -260,18 +264,15 @@ ReferenceArrayInput.propTypes = { className: PropTypes.string, filter: PropTypes.object, filterToQuery: PropTypes.func.isRequired, - input: PropTypes.object.isRequired, label: PropTypes.string, - meta: PropTypes.object, perPage: PropTypes.number, reference: PropTypes.string.isRequired, - resource: PropTypes.string.isRequired, + resource: PropTypes.string, sort: PropTypes.shape({ field: PropTypes.string, order: PropTypes.oneOf(['ASC', 'DESC']), }), source: PropTypes.string, - translate: PropTypes.func.isRequired, }; ReferenceArrayInput.defaultProps = {