11import arrayMutators from 'final-form-arrays' ;
22import createFocusDecorator from 'final-form-focus' ;
33import PropTypes from 'prop-types' ;
4- import React , { useCallback , useMemo , useRef , useState } from 'react' ;
4+ import React , { useCallback , useMemo , useRef , useState } from 'react' ;
55
66import defaultSchemaValidator from '../default-schema-validator' ;
77import defaultValidatorMapper from '../validator-mapper' ;
@@ -10,8 +10,7 @@ import RendererContext from '../renderer-context';
1010import renderForm from './render-form' ;
1111import SchemaErrorComponent from './schema-error-component' ;
1212
13-
14- const isFunc = ( fn ) => ( typeof fn === 'function' ) ;
13+ const isFunc = ( fn ) => typeof fn === 'function' ;
1514
1615const FormRenderer = ( {
1716 actionMapper,
@@ -34,99 +33,102 @@ const FormRenderer = ({
3433 ...props
3534} ) => {
3635 const [ fileInputs , setFileInputs ] = useState ( [ ] ) ;
36+ const formFields = useMemo ( ( ) => renderForm ( schema . fields ) , [ schema ] ) ;
3737 const registeredFields = useRef ( { } ) ;
3838 const focusDecorator = useRef ( createFocusDecorator ( ) ) ;
39- const validatorMapperMerged = useMemo ( ( ) => ( {
40- ...defaultValidatorMapper ,
41- ...validatorMapper
42- } ) , [ validatorMapper ] ) ;
43- const mutatorsMerged = useMemo ( ( ) => ( {
44- ...arrayMutators ,
45- ...mutators
46- } ) , [ mutators ] ) ;
47- const decoratorsMerged = useMemo ( ( ) => ( [
48- focusDecorator . current ,
49- ...( Array . isArray ( decorators ) ? decorators : [ ] )
50- ] ) , [ decorators ] ) ;
51-
52- const handleSubmitCallback = useCallback ( ( values , formApi , ...args ) => {
53- return ! isFunc ( onSubmit ) ? undefined : onSubmit ( values , { ...formApi , fileInputs} , ...args ) ;
54- } , [ onSubmit , fileInputs ] ) ;
55-
56- const handleCancelCallback = useCallback ( ( getState ) => {
57- return ( ( ...args ) => onCancel ( getState ( ) . values , ...args ) ) ;
58- } , [ onCancel ] ) ;
59-
60- const handleResetCallback = useCallback ( ( reset ) => ( ...args ) => {
61- reset ( ) ;
62- return ! isFunc ( onReset ) ? void 0 : onReset ( ...args ) ;
63- } , [ onReset ] ) ;
64-
65- const handleErrorCallback = useCallback ( ( ...args ) => {
66- // eslint-disable-next-line no-console
67- console . error ( ...args ) ;
68- return ! isFunc ( onError ) ? void 0 : onError ( ...args ) ;
69- } , [ onError ] ) ;
39+ const validatorMapperMerged = useMemo ( ( ) => {
40+ return { ...defaultValidatorMapper , ...validatorMapper }
41+ } , [ validatorMapper ] ) ;
42+ const mutatorsMerged = useMemo (
43+ ( ) => ( { ...arrayMutators , ...mutators } ) ,
44+ [ mutators ]
45+ ) ;
46+ const decoratorsMerged = useMemo (
47+ ( ) => ( [ focusDecorator . current , ...( Array . isArray ( decorators ) ? decorators : [ ] ) ] ) ,
48+ [ decorators ]
49+ ) ;
50+
51+ const handleSubmitCallback = useCallback (
52+ ( values , formApi , ...args ) => {
53+ return ! isFunc ( onSubmit ) ? undefined : onSubmit ( values , { ...formApi , fileInputs } , ...args ) ;
54+ } ,
55+ [ onSubmit , fileInputs ]
56+ ) ;
57+
58+ const handleCancelCallback = useCallback (
59+ ( getState ) => {
60+ return ( ...args ) => onCancel ( getState ( ) . values , ...args ) ;
61+ } ,
62+ [ onCancel ]
63+ ) ;
64+
65+ const handleResetCallback = useCallback (
66+ ( reset ) => ( ...args ) => {
67+ reset ( ) ;
68+ return ! isFunc ( onReset ) ? void 0 : onReset ( ...args ) ;
69+ } ,
70+ [ onReset ]
71+ ) ;
72+
73+ const handleErrorCallback = useCallback (
74+ ( ...args ) => {
75+ // eslint-disable-next-line no-console
76+ console . error ( ...args ) ;
77+ return ! isFunc ( onError ) ? void 0 : onError ( ...args ) ;
78+ } ,
79+ [ onError ]
80+ ) ;
7081
7182 const registerInputFile = useCallback ( ( name ) => {
7283 setFileInputs ( ( prevFiles ) => [ ...prevFiles , name ] ) ;
7384 } , [ ] ) ;
7485
7586 const unRegisterInputFile = useCallback ( ( name ) => {
76- setFileInputs ( ( prevFiles ) => [
77- ...prevFiles . splice ( prevFiles . indexOf ( name ) )
78- ] ) ;
87+ setFileInputs ( ( prevFiles ) => [ ...prevFiles . splice ( prevFiles . indexOf ( name ) ) ] ) ;
7988 } , [ ] ) ;
8089
8190 const setRegisteredFields = useCallback ( ( fn ) => {
82- return registeredFields . current = fn ( { ...registeredFields . current } ) ;
91+ return registeredFields . current = fn ( { ...registeredFields . current } ) ;
8392 } , [ ] ) ;
8493
8594 const internalRegisterField = useCallback ( ( name ) => {
8695 setRegisteredFields ( ( prev ) => (
87- prev [ name ] ? { ...prev , [ name ] : prev [ name ] + 1 } : { ...prev , [ name ] : 1 } )
96+ prev [ name ] ? { ...prev , [ name ] : prev [ name ] + 1 } : { ...prev , [ name ] : 1 } )
8897 ) ;
8998 } , [ ] ) ;
9099
91100 const internalUnRegisterField = useCallback ( ( name ) => {
92- setRegisteredFields ( ( { [ name ] : currentField , ...prev } ) => (
93- currentField && currentField > 1 ? { [ name ] : currentField - 1 , ...prev } : prev
101+ setRegisteredFields ( ( { [ name ] : currentField , ...prev } ) => (
102+ currentField && currentField > 1 ? { [ name ] : currentField - 1 , ...prev } : prev
94103 ) ) ;
95104 } , [ ] ) ;
96105
97106 const internalGetRegisteredFields = useCallback ( ( ) => {
98107 const fields = registeredFields . current ;
99- return Object . entries ( fields ) . reduce ( ( acc , [ name , value ] ) => (
100- value > 0 ? [ ...acc , name ] : acc
101- ) , [ ] ) ;
108+ return Object . entries ( fields ) . reduce (
109+ ( acc , [ name , value ] ) => value > 0 ? [ ...acc , name ] : acc ,
110+ [ ]
111+ ) ;
102112 } , [ ] ) ;
103113
104114 try {
105115 const validatorTypes = Object . keys ( validatorMapperMerged ) ;
106116 const actionTypes = actionMapper ? Object . keys ( actionMapper ) : [ ] ;
107117
108- defaultSchemaValidator (
109- schema ,
110- componentMapper ,
111- validatorTypes ,
112- actionTypes ,
113- schemaValidatorMapper
114- ) ;
118+ defaultSchemaValidator ( schema , componentMapper , validatorTypes , actionTypes , schemaValidatorMapper ) ;
115119 }
116120 catch ( error ) {
117121 handleErrorCallback ( 'schema-error' , error ) ;
118122 return < SchemaErrorComponent name = { error . name } message = { error . message } /> ;
119123 }
120124
121- const formFields = useMemo ( ( ) => renderForm ( schema . fields ) , [ schema ] ) ;
122-
123125 return (
124126 < Form
125127 onSubmit = { handleSubmitCallback }
126128 mutators = { mutatorsMerged }
127129 decorators = { decoratorsMerged }
128- subscription = { { pristine : true , submitting : true , valid : true , ...subscription } }
129- render = { ( { handleSubmit, pristine, valid, form : { reset, mutators, getState, submit, ...form } } ) => (
130+ subscription = { { pristine : true , submitting : true , valid : true , ...subscription } }
131+ render = { ( { handleSubmit, pristine, valid, form : { reset, mutators, getState, submit, ...form } } ) => (
130132 < RendererContext . Provider
131133 value = { {
132134 componentMapper,
@@ -159,15 +161,9 @@ const FormRenderer = ({
159161 } }
160162 >
161163
162- { FormTemplate && (
163- < FormTemplate
164- formFields = { formFields }
165- schema = { schema }
166- { ...FormTemplateProps }
167- />
168- ) }
164+ { FormTemplate && < FormTemplate formFields = { formFields } schema = { schema } { ...FormTemplateProps } /> }
169165
170- { isFunc ( children ) ? children ( { formFields, schema} ) : children }
166+ { isFunc ( children ) ? children ( { formFields, schema } ) : children }
171167
172168 </ RendererContext . Provider >
173169 ) }
@@ -184,15 +180,10 @@ FormRenderer.propTypes = {
184180 onError : PropTypes . func ,
185181 schema : PropTypes . object . isRequired ,
186182 clearOnUnmount : PropTypes . bool ,
187- subscription : PropTypes . shape ( { [ PropTypes . string ] : PropTypes . bool } ) ,
183+ subscription : PropTypes . shape ( { [ PropTypes . string ] : PropTypes . bool } ) ,
188184 clearedValue : PropTypes . any ,
189185 componentMapper : PropTypes . shape ( {
190- [ PropTypes . string ] : PropTypes . oneOfType ( [
191- PropTypes . node ,
192- PropTypes . element ,
193- PropTypes . func ,
194- PropTypes . elementType
195- ] ) ,
186+ [ PropTypes . string ] : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . element , PropTypes . func , PropTypes . elementType ] ) ,
196187 } ) . isRequired ,
197188 FormTemplate : PropTypes . elementType ,
198189 FormTemplateProps : PropTypes . object ,
@@ -214,6 +205,8 @@ FormRenderer.propTypes = {
214205 } ) ,
215206 } ) ,
216207 initialValues : PropTypes . object ,
208+ decorators : PropTypes . array ,
209+ mutators : PropTypes . object ,
217210} ;
218211
219212FormRenderer . defaultProps = {
0 commit comments