@@ -57,12 +57,27 @@ const reducer = (state, { type, specialType, validate, arrayValidator, initialVa
5757 }
5858} ;
5959
60+ const createFieldProps = ( name , formOptions ) => {
61+ const { value, blur, change, focus, ...meta } = formOptions . getFieldState ( name ) || { } ;
62+
63+ return {
64+ meta,
65+ input : { name, value }
66+ } ;
67+ } ;
68+
6069const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, ...props } ) => {
6170 const { validatorMapper, formOptions } = useContext ( RendererContext ) ;
6271
72+ const { validate : resolvePropsValidate , ...resolvedProps } = resolveProps
73+ ? resolveProps ( props , createFieldProps ( name , formOptions ) , formOptions ) || { }
74+ : { } ;
75+
76+ const finalValidate = resolvePropsValidate || validate ;
77+
6378 const [ { type, initialValue, validate : stateValidate , arrayValidator } , dispatch ] = useReducer (
6479 reducer ,
65- { props, validate, component, validatorMapper } ,
80+ { props : { ... props , ... resolvedProps } , validate : finalValidate , component, validatorMapper } ,
6681 init
6782 ) ;
6883
@@ -71,6 +86,7 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
7186 const enhancedProps = {
7287 type,
7388 ...props ,
89+ ...resolvedProps ,
7490 ...( initialValue ? { initialValue } : { } ) ,
7591 ...( stateValidate ? { validate : stateValidate } : { } )
7692 } ;
@@ -92,8 +108,8 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
92108 if ( mounted . current ) {
93109 dispatch ( {
94110 type : 'setValidators' ,
95- validate : calculateValidate ( props , validate , component , validatorMapper ) ,
96- arrayValidator : calculateArrayValidator ( props , validate , component , validatorMapper )
111+ validate : calculateValidate ( enhancedProps , finalValidate , component , validatorMapper ) ,
112+ arrayValidator : calculateArrayValidator ( enhancedProps , finalValidate , component , validatorMapper )
97113 } ) ;
98114 }
99115 /**
@@ -102,20 +118,20 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
102118 * Using stringify is acceptable here since the array is usually very small.
103119 * If we notice performance hit, we can implement custom hook with a deep equal functionality.
104120 */
105- } , [ validate ? JSON . stringify ( validate ) : false , component , props . dataType ] ) ;
121+ } , [ finalValidate ? JSON . stringify ( finalValidate ) : false , component , enhancedProps . dataType ] ) ;
106122
107123 /** Re-convert initialValue when changed */
108124 useEffect ( ( ) => {
109125 if ( mounted . current ) {
110- const newInitialValue = calculateInitialValue ( props ) ;
126+ const newInitialValue = calculateInitialValue ( enhancedProps ) ;
111127 if ( ! isEqual ( initialValue , newInitialValue ) ) {
112128 dispatch ( {
113129 type : 'setInitialValue' ,
114130 initialValue : newInitialValue
115131 } ) ;
116132 }
117133 }
118- } , [ props . initialValue , props . dataType ] ) ;
134+ } , [ enhancedProps . initialValue , enhancedProps . dataType ] ) ;
119135
120136 useEffect ( ( ) => {
121137 /**
@@ -160,14 +176,22 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
160176 [ ]
161177 ) ;
162178
163- const { initialValue : _initialValue , clearOnUnmount, dataType, clearedValue, isEqual : _isEqual , ...cleanProps } = props ;
179+ const {
180+ initialValue : _initialValue ,
181+ clearOnUnmount,
182+ dataType,
183+ clearedValue,
184+ isEqual : _isEqual ,
185+ validate : _validate ,
186+ type : _type ,
187+ ...cleanProps
188+ } = enhancedProps ;
164189
165190 /**
166191 * construct component props necessary that would live in field provider
167192 */
168193 return {
169194 ...cleanProps ,
170- ...( resolveProps ? resolveProps ( cleanProps , fieldProps , formOptions ) : { } ) ,
171195 ...fieldProps ,
172196 ...( arrayValidator ? { arrayValidator } : { } ) ,
173197 input : {
0 commit comments