1- import { useEffect , useContext , useState , useRef } from 'react' ;
1+ import { useEffect , useContext , useRef , useReducer } from 'react' ;
22import { useField } from 'react-final-form' ;
33import enhancedOnChange from '../form-renderer/enhanced-on-change' ;
44import RendererContext from './renderer-context' ;
@@ -27,12 +27,45 @@ const calculateValidate = (props, validate, component, validatorMapper) => {
2727 }
2828} ;
2929
30+ const init = ( { props, validate, component, validatorMapper } ) => ( {
31+ initialValue : calculateInitialValue ( props ) ,
32+ arrayValidator : calculateArrayValidator ( props , validate , component , validatorMapper ) ,
33+ validate : calculateValidate ( props , validate , component , validatorMapper ) ,
34+ type : assignSpecialType ( component )
35+ } ) ;
36+
37+ const reducer = ( state , { type, specialType, validate, arrayValidator, initialValue } ) => {
38+ switch ( type ) {
39+ case 'setType' :
40+ return {
41+ ...state ,
42+ type : specialType
43+ } ;
44+ case 'setValidators' :
45+ return {
46+ ...state ,
47+ validate,
48+ arrayValidator
49+ } ;
50+ case 'setInitialValue' :
51+ return {
52+ ...state ,
53+ initialValue
54+ } ;
55+ default :
56+ return state ;
57+ }
58+ } ;
59+
3060const useFieldApi = ( { name, initializeOnMount, component, render, validate, ...props } ) => {
3161 const { actionMapper, validatorMapper, formOptions } = useContext ( RendererContext ) ;
32- const [ initialValue , setInitialValue ] = useState ( ( ) => calculateInitialValue ( props ) ) ;
33- const [ arrayValidator , setArrayValidator ] = useState ( ( ) => calculateArrayValidator ( props , validate , component , validatorMapper ) ) ;
34- const [ stateValidate , setValidate ] = useState ( ( ) => calculateValidate ( props , validate , component , validatorMapper ) ) ;
35- const [ type , setType ] = useState ( ( ) => assignSpecialType ( component ) ) ;
62+
63+ const [ { type, initialValue, validate : stateValidate , arrayValidator } , dispatch ] = useReducer (
64+ reducer ,
65+ { props, validate, component, validatorMapper } ,
66+ init
67+ ) ;
68+
3669 const mounted = useRef ( false ) ;
3770
3871 const enhancedProps = {
@@ -49,16 +82,19 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, ...
4982 if ( mounted . current ) {
5083 const specialType = assignSpecialType ( component ) ;
5184 if ( specialType !== type ) {
52- setType ( specialType ) ;
85+ dispatch ( { type : 'setType' , specialType } ) ;
5386 }
5487 }
5588 } , [ component ] ) ;
5689
5790 /** Reinitilize array validator/validate */
5891 useEffect ( ( ) => {
5992 if ( mounted . current ) {
60- setArrayValidator ( calculateArrayValidator ( props , validate , component , validatorMapper ) ) ;
61- setValidate ( calculateValidate ( props , validate , component , validatorMapper ) ) ;
93+ dispatch ( {
94+ type : 'setValidators' ,
95+ validate : calculateValidate ( props , validate , component , validatorMapper ) ,
96+ arrayValidator : calculateArrayValidator ( props , validate , component , validatorMapper )
97+ } ) ;
6298 }
6399 } , [ validate , component , props . dataType ] ) ;
64100
@@ -67,7 +103,10 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, ...
67103 if ( mounted . current ) {
68104 const newInitialValue = calculateInitialValue ( props ) ;
69105 if ( ! isEqual ( initialValue , newInitialValue ) ) {
70- setInitialValue ( newInitialValue ) ;
106+ dispatch ( {
107+ type : 'setInitialValue' ,
108+ initialValue : newInitialValue
109+ } ) ;
71110 }
72111 }
73112 } , [ props . initialValue , props . dataType ] ) ;
@@ -95,6 +134,7 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, ...
95134 mounted . current = true ;
96135
97136 return ( ) => {
137+ mounted . current = false ;
98138 /**
99139 * Delete the value from form state when field is inmounted
100140 */
0 commit comments