@@ -57,12 +57,27 @@ const reducer = (state, { type, specialType, validate, arrayValidator, initialVa
57
57
}
58
58
} ;
59
59
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
+
60
69
const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, ...props } ) => {
61
70
const { validatorMapper, formOptions } = useContext ( RendererContext ) ;
62
71
72
+ const { validate : resolvePropsValidate , ...resolvedProps } = resolveProps
73
+ ? resolveProps ( props , createFieldProps ( name , formOptions ) , formOptions ) || { }
74
+ : { } ;
75
+
76
+ const finalValidate = resolvePropsValidate || validate ;
77
+
63
78
const [ { type, initialValue, validate : stateValidate , arrayValidator } , dispatch ] = useReducer (
64
79
reducer ,
65
- { props, validate, component, validatorMapper } ,
80
+ { props : { ... props , ... resolvedProps } , validate : finalValidate , component, validatorMapper } ,
66
81
init
67
82
) ;
68
83
@@ -71,6 +86,7 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
71
86
const enhancedProps = {
72
87
type,
73
88
...props ,
89
+ ...resolvedProps ,
74
90
...( initialValue ? { initialValue } : { } ) ,
75
91
...( stateValidate ? { validate : stateValidate } : { } )
76
92
} ;
@@ -92,8 +108,8 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
92
108
if ( mounted . current ) {
93
109
dispatch ( {
94
110
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 )
97
113
} ) ;
98
114
}
99
115
/**
@@ -102,20 +118,20 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
102
118
* Using stringify is acceptable here since the array is usually very small.
103
119
* If we notice performance hit, we can implement custom hook with a deep equal functionality.
104
120
*/
105
- } , [ validate ? JSON . stringify ( validate ) : false , component , props . dataType ] ) ;
121
+ } , [ finalValidate ? JSON . stringify ( finalValidate ) : false , component , enhancedProps . dataType ] ) ;
106
122
107
123
/** Re-convert initialValue when changed */
108
124
useEffect ( ( ) => {
109
125
if ( mounted . current ) {
110
- const newInitialValue = calculateInitialValue ( props ) ;
126
+ const newInitialValue = calculateInitialValue ( enhancedProps ) ;
111
127
if ( ! isEqual ( initialValue , newInitialValue ) ) {
112
128
dispatch ( {
113
129
type : 'setInitialValue' ,
114
130
initialValue : newInitialValue
115
131
} ) ;
116
132
}
117
133
}
118
- } , [ props . initialValue , props . dataType ] ) ;
134
+ } , [ enhancedProps . initialValue , enhancedProps . dataType ] ) ;
119
135
120
136
useEffect ( ( ) => {
121
137
/**
@@ -160,14 +176,22 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
160
176
[ ]
161
177
) ;
162
178
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 ;
164
189
165
190
/**
166
191
* construct component props necessary that would live in field provider
167
192
*/
168
193
return {
169
194
...cleanProps ,
170
- ...( resolveProps ? resolveProps ( cleanProps , fieldProps , formOptions ) : { } ) ,
171
195
...fieldProps ,
172
196
...( arrayValidator ? { arrayValidator } : { } ) ,
173
197
input : {
0 commit comments