1- import { useEffect , useContext , useRef , useReducer } from 'react' ;
1+ import { useEffect , useContext , useRef , useReducer , useState } from 'react' ;
22import { useField } from 'react-final-form' ;
33import enhancedOnChange from '../form-renderer/enhanced-on-change' ;
44import RendererContext from './renderer-context' ;
@@ -66,9 +66,12 @@ const createFieldProps = (name, formOptions) => {
6666 } ;
6767} ;
6868
69- const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, ...props } ) => {
69+ const useFieldApi = ( { name, initializeOnMount, component, render, validate, resolveProps, useWarnings , ...props } ) => {
7070 const { validatorMapper, formOptions } = useContext ( RendererContext ) ;
7171
72+ // eslint-disable-next-line react-hooks/rules-of-hooks
73+ const [ warning , setWarning ] = useWarnings ? useState ( ) : [ undefined , ( ) => undefined ] ;
74+
7275 const { validate : resolvePropsValidate , ...resolvedProps } = resolveProps
7376 ? resolveProps ( props , createFieldProps ( name , formOptions ) , formOptions ) || { }
7477 : { } ;
@@ -91,7 +94,26 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
9194 ...( stateValidate ? { validate : stateValidate } : { } )
9295 } ;
9396
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+ } ) ;
95117
96118 /** Reinitilize type */
97119 useEffect ( ( ) => {
@@ -193,7 +215,13 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, res
193215 return {
194216 ...cleanProps ,
195217 ...fieldProps ,
196- ...( arrayValidator ? { arrayValidator } : { } ) ,
218+ ...( arrayValidator && { arrayValidator } ) ,
219+ ...( useWarnings && {
220+ meta : {
221+ ...fieldProps . meta ,
222+ warning
223+ }
224+ } ) ,
197225 input : {
198226 ...fieldProps . input ,
199227 value :
0 commit comments