1
- import { useEffect , useContext , useRef , useReducer } from 'react' ;
1
+ import { useEffect , useContext , useRef , useReducer , useState } from 'react' ;
2
2
import { useField } from 'react-final-form' ;
3
3
import enhancedOnChange from '../form-renderer/enhanced-on-change' ;
4
4
import RendererContext from './renderer-context' ;
@@ -66,9 +66,12 @@ const createFieldProps = (name, formOptions) => {
66
66
} ;
67
67
} ;
68
68
69
- const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, ...props } ) => {
69
+ const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, useWarnings , ...props } ) => {
70
70
const { validatorMapper, formOptions } = useContext ( RendererContext ) ;
71
71
72
+ // eslint-disable-next-line react-hooks/rules-of-hooks
73
+ const [ warning , setWarning ] = useWarnings ? useState ( ) : [ undefined , ( ) => undefined ] ;
74
+
72
75
const { validate : resolvePropsValidate , ...resolvedProps } = resolveProps
73
76
? resolveProps ( props , createFieldProps ( name , formOptions ) , formOptions ) || { }
74
77
: { } ;
@@ -91,7 +94,26 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
91
94
...( stateValidate ? { validate : stateValidate } : { } )
92
95
} ;
93
96
94
- const fieldProps = useField ( name , enhancedProps ) ;
97
+ const fieldProps = useField ( name , {
98
+ ...enhancedProps ,
99
+ ...( useWarnings && {
100
+ validate : async ( ...args ) => {
101
+ warning && setWarning ( undefined ) ;
102
+
103
+ const result = await enhancedProps . validate ( ...args ) ;
104
+
105
+ if ( result ?. type === 'warning' ) {
106
+ if ( warning !== result . error ) {
107
+ setWarning ( result . error ) ;
108
+ }
109
+
110
+ return ;
111
+ }
112
+
113
+ return result ;
114
+ }
115
+ } )
116
+ } ) ;
95
117
96
118
/** Reinitilize type */
97
119
useEffect ( ( ) => {
@@ -193,7 +215,13 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
193
215
return {
194
216
...cleanProps ,
195
217
...fieldProps ,
196
- ...( arrayValidator ? { arrayValidator } : { } ) ,
218
+ ...( arrayValidator && { arrayValidator } ) ,
219
+ ...( useWarnings && {
220
+ meta : {
221
+ ...fieldProps . meta ,
222
+ warning
223
+ }
224
+ } ) ,
197
225
input : {
198
226
...fieldProps . input ,
199
227
value :
0 commit comments