1
- import { useEffect , useContext } from 'react' ;
1
+ import { useEffect , useContext , useState } from 'react' ;
2
2
import { useField } from 'react-final-form' ;
3
3
import useFormApi from './use-form-api' ;
4
4
import enhancedOnChange from '../form-renderer/enhanced-on-change' ;
@@ -8,25 +8,25 @@ import assignSpecialType from '../form-renderer/assign-special-type';
8
8
import componentTypes from './component-types' ;
9
9
import { prepareArrayValidator , getValidate } from '../form-renderer/validator-helpers' ;
10
10
import composeValidators from './compose-validators' ;
11
+ import isEqual from 'lodash/isEqual' ;
11
12
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 } ) => {
13
20
const { actionMapper, validatorMapper } = useContext ( RendererContext ) ;
21
+ const [ initialValue , setInitialValue ] = useState ( ( ) => calculateInitialValue ( props ) ) ;
14
22
15
23
const formOptions = useFormApi ( ) ;
16
24
17
25
/** Assign type (checkbox, radio ) */
18
26
let enhancedProps = {
19
- type : type || assignSpecialType ( component )
27
+ type : assignSpecialType ( component )
20
28
} ;
21
29
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
-
30
30
/** Add validate/array validator when needed */
31
31
let arrayValidator ;
32
32
if ( validate || props . dataType ) {
@@ -41,24 +41,33 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, typ
41
41
}
42
42
43
43
enhancedProps = {
44
+ ...enhancedProps ,
44
45
...props ,
45
- ...enhancedProps
46
+ ...( initialValue ? { initialValue } : { } )
46
47
} ;
47
48
48
49
const fieldProps = useField ( name , enhancedProps ) ;
49
50
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
+
50
59
useEffect ( ( ) => {
51
60
/**
52
61
* Re initialize field when mounted to the Form
53
62
* This affects conditional fields
54
63
*/
55
64
if ( initializeOnMount ) {
56
- const initialValue = Object . prototype . hasOwnProperty . call ( enhancedProps , 'initialValue' )
65
+ const value = Object . prototype . hasOwnProperty . call ( enhancedProps , 'initialValue' )
57
66
? enhancedProps . initialValue
58
67
: formOptions . getFieldState ( name ) . initial ;
59
- fieldProps . input . onChange ( initialValue ) ;
68
+ fieldProps . input . onChange ( value ) ;
60
69
}
61
- } , [ initializeOnMount , enhancedProps . initialValue , fieldProps . meta . initial ] ) ;
70
+ } , [ initializeOnMount , enhancedProps . initialValue , fieldProps . meta . initial , props . dataType ] ) ;
62
71
63
72
/**
64
73
* Prepare deleted value of field
@@ -89,7 +98,7 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, typ
89
98
} ) ;
90
99
}
91
100
92
- const { initialValue, clearOnUnmount, dataType, ...cleanProps } = props ;
101
+ const { initialValue : _initialValue , clearOnUnmount, dataType, clearedValue , ...cleanProps } = props ;
93
102
94
103
/**
95
104
* construct component props necessary that would live in field provider
0 commit comments