1- import { useEffect , useContext } from 'react' ;
1+ import { useEffect , useContext , useState } from 'react' ;
22import { useField } from 'react-final-form' ;
33import useFormApi from './use-form-api' ;
44import enhancedOnChange from '../form-renderer/enhanced-on-change' ;
@@ -8,25 +8,25 @@ import assignSpecialType from '../form-renderer/assign-special-type';
88import componentTypes from './component-types' ;
99import { prepareArrayValidator , getValidate } from '../form-renderer/validator-helpers' ;
1010import composeValidators from './compose-validators' ;
11+ import isEqual from 'lodash/isEqual' ;
1112
12- const useFieldApi = ( { name, initializeOnMount, component, render, validate, type, ...props } ) => {
13+ const calculateInitialValue = ( props ) => {
14+ if ( Object . prototype . hasOwnProperty . call ( props , 'initialValue' ) && Object . prototype . hasOwnProperty . call ( props , 'dataType' ) ) {
15+ return convertInitialValue ( props . initialValue , props . dataType ) ;
16+ }
17+ } ;
18+
19+ const useFieldApi = ( { name, initializeOnMount, component, render, validate, ...props } ) => {
1320 const { actionMapper, validatorMapper } = useContext ( RendererContext ) ;
21+ const [ initialValue , setInitialValue ] = useState ( ( ) => calculateInitialValue ( props ) ) ;
1422
1523 const formOptions = useFormApi ( ) ;
1624
1725 /** Assign type (checkbox, radio ) */
1826 let enhancedProps = {
19- type : type || assignSpecialType ( component )
27+ type : assignSpecialType ( component )
2028 } ;
2129
22- /** Convert initialValue to correct dataType */
23- if ( Object . prototype . hasOwnProperty . call ( props , 'initialValue' ) && Object . prototype . hasOwnProperty . call ( props , 'dataType' ) ) {
24- enhancedProps = {
25- ...enhancedProps ,
26- initialValue : convertInitialValue ( props . initialValue , props . dataType )
27- } ;
28- }
29-
3030 /** Add validate/array validator when needed */
3131 let arrayValidator ;
3232 if ( validate || props . dataType ) {
@@ -41,24 +41,33 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, typ
4141 }
4242
4343 enhancedProps = {
44+ ...enhancedProps ,
4445 ...props ,
45- ...enhancedProps
46+ ...( initialValue ? { initialValue } : { } )
4647 } ;
4748
4849 const fieldProps = useField ( name , enhancedProps ) ;
4950
51+ /** Re-convert initialValue when changed */
52+ useEffect ( ( ) => {
53+ const newInitialValue = calculateInitialValue ( props ) ;
54+ if ( ! isEqual ( initialValue , newInitialValue ) ) {
55+ setInitialValue ( newInitialValue ) ;
56+ }
57+ } , [ props . initialValue , props . dataType ] ) ;
58+
5059 useEffect ( ( ) => {
5160 /**
5261 * Re initialize field when mounted to the Form
5362 * This affects conditional fields
5463 */
5564 if ( initializeOnMount ) {
56- const initialValue = Object . prototype . hasOwnProperty . call ( enhancedProps , 'initialValue' )
65+ const value = Object . prototype . hasOwnProperty . call ( enhancedProps , 'initialValue' )
5766 ? enhancedProps . initialValue
5867 : formOptions . getFieldState ( name ) . initial ;
59- fieldProps . input . onChange ( initialValue ) ;
68+ fieldProps . input . onChange ( value ) ;
6069 }
61- } , [ initializeOnMount , enhancedProps . initialValue , fieldProps . meta . initial ] ) ;
70+ } , [ initializeOnMount , enhancedProps . initialValue , fieldProps . meta . initial , props . dataType ] ) ;
6271
6372 /**
6473 * Prepare deleted value of field
@@ -89,7 +98,7 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, typ
8998 } ) ;
9099 }
91100
92- const { initialValue, clearOnUnmount, dataType, ...cleanProps } = props ;
101+ const { initialValue : _initialValue , clearOnUnmount, dataType, clearedValue , ...cleanProps } = props ;
93102
94103 /**
95104 * construct component props necessary that would live in field provider
0 commit comments