import React , { Children , cloneElement , ReactElement } from 'react' ; import PropTypes from 'prop-types' ; import { useInput , useReferenceInputController , InputProps , warning as warningLog , ListContextProvider , ReferenceInputValue , UseInputValue , ResourceContextProvider , } from 'ra-core' ; import sanitizeInputRestProps from './sanitizeInputRestProps' ; import ReferenceError from './ReferenceError' ; example example example example example example const ReferenceInput = ( props ) => { const { format , onBlur , onChange , onFocus , parse , validate , ... rest } = props ; const inputProps = useInput ( { format , onBlur , onChange , onFocus , parse , validate , ... rest , } ) ; return ( < ReferenceInputView { ... inputProps } { ... rest } { ... useReferenceInputController ( { ... rest , ... inputProps } ) } />); } ; ReferenceInput . propTypes = { allowEmpty : PropTypes . bool , basePath : PropTypes . string , children : PropTypes . element . isRequired , className : PropTypes . string , classes : PropTypes . object , filter : PropTypes . object , filterToQuery : PropTypes . func . isRequired , label : PropTypes . string , onChange : PropTypes . func , perPage : PropTypes . number , record : PropTypes . object , reference : PropTypes . string . isRequired , resource : PropTypes . string , sort : PropTypes . shape ( { field : PropTypes . string , order : PropTypes . oneOf ( [ 'ASC' , 'DESC' ] ) , } ) , source : PropTypes . string , } ; ReferenceInput . defaultProps = { filter : { } , filterToQuery : searchText => ( searchText ? { q : searchText } : { } ) , perPage : 25 , sort : { field : 'id' , order : 'DESC' } , } ; export interface ReferenceInputProps extends InputProps { allowEmpty ? : boolean ; basePath ? : string ; children : ReactElement ; classes ? : any ; className ? : string ; filterToQuery ? : ( filter : string ) => any ; label ? : string ; perPage ? : number ; reference : string ; referenceSource ? : ( resource : string , source : string ) => string ; resource ? : string ; enableGetChoices ? : ( filters : any ) => boolean ; [ key ] : any ; } const sanitizeRestProps = ( { dataStatus = null , enableGetChoices = null , filter = null , filterToQuery = null , onChange = null , perPage = null , reference = null , referenceRecord = null , referenceSource = null , sort = null , validation = null , ... rest } ) => sanitizeInputRestProps ( rest ) ; export interface ReferenceInputViewProps extends ReferenceInputValue , ReferenceInputProps , Omit < UseInputValue , 'id' > { } export const ReferenceInputView = ( props ) => { const { allowEmpty , basePath , children , choices , classes , className , error , helperText , id , input , isRequired , label , meta , possibleValues , resource , reference , setFilter , setPagination , setSort , source , warning , ... rest } = props ; if ( Children . count ( children ) !== 1 ) { throw new Error ( '<ReferenceInput> only accepts a single child' ) ; } if ( error ) { return < ReferenceError label = { label } error = { error } / > ; } const finalMeta = warning ? { ... meta , error : warning , } : meta ; warningLog ( helperText !== undefined && helperText !== false , "<ReferenceInput> doesn't accept a helperText prop. Set the helperText prop on the child component instead" ) ; const disabledHelperText = helperText === false ? { helperText } : { } ; return ( < ResourceContextProvider value = { reference } > < ListContextProvider value = { possibleValues } > { cloneElement ( children , { allowEmpty , classes , className , input , isRequired , label , resource , meta : finalMeta , source , choices , basePath , setFilter , setPagination , setSort , translateChoice : false , ... disabledHelperText , ... sanitizeRestProps ( rest ) , } ) } < /ListContextProvider> < /ResourceContextProvider>); } ; export default ReferenceInput ;