Skip to content

Commit

Permalink
Merge pull request #3540 from marmelab/referenceinput-useinput
Browse files Browse the repository at this point in the history
[RFR] Migrate ReferenceInput to use useInput
  • Loading branch information
fzaninotto authored Aug 19, 2019
2 parents 8cdd8ce + 990642b commit ce889b1
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions packages/ra-ui-materialui/src/input/ReferenceInput.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { addField, useReferenceInputController } from 'ra-core';
import { useInput, useReferenceInputController } from 'ra-core';

import LinearProgress from '../layout/LinearProgress';
import Labeled from './Labeled';
Expand Down Expand Up @@ -85,9 +85,28 @@ import ReferenceError from './ReferenceError';
* <SelectInput optionText="title" />
* </ReferenceInput>
*/
export const ReferenceInput = props => (
<ReferenceInputView {...props} {...useReferenceInputController(props)} />
);
export const ReferenceInput = ({
onBlur,
onChange,
onFocus,
validate,
...props
}) => {
const inputProps = useInput({
onBlur,
onChange,
onFocus,
validate,
...props,
});
return (
<ReferenceInputView
{...inputProps}
{...props}
{...useReferenceInputController({ ...props, ...inputProps })}
/>
);
};

ReferenceInput.propTypes = {
allowEmpty: PropTypes.bool.isRequired,
Expand All @@ -97,14 +116,12 @@ ReferenceInput.propTypes = {
classes: PropTypes.object,
filter: PropTypes.object,
filterToQuery: PropTypes.func.isRequired,
input: PropTypes.object.isRequired,
label: PropTypes.string,
meta: PropTypes.object,
onChange: PropTypes.func,
perPage: PropTypes.number,
record: PropTypes.object,
reference: PropTypes.string.isRequired,
resource: PropTypes.string.isRequired,
resource: PropTypes.string,
sort: PropTypes.shape({
field: PropTypes.string,
order: PropTypes.oneOf(['ASC', 'DESC']),
Expand All @@ -120,8 +137,6 @@ ReferenceInput.defaultProps = {
sort: { field: 'id', order: 'DESC' },
};

const EnhancedReferenceInput = addField(ReferenceInput);

const sanitizeRestProps = ({
allowEmpty,
basePath,
Expand Down Expand Up @@ -248,4 +263,4 @@ ReferenceInputView.propTypes = {
warning: PropTypes.string,
};

export default EnhancedReferenceInput;
export default ReferenceInput;

0 comments on commit ce889b1

Please sign in to comment.