@@ -44,7 +44,10 @@ interface FormProps<ValueType, RequestType> {
4444 readonly shouldDisableButton ?: ( isPrimaryGroup : boolean , index : number ) => boolean
4545}
4646
47- const Form : < ValueType extends FormValue , RequestType extends FormValue > ( props : FormProps < ValueType , RequestType > ) => JSX . Element
47+ const Form : <
48+ ValueType extends FormValue ,
49+ RequestType extends FormValue
50+ > ( props : FormProps < ValueType , RequestType > ) => JSX . Element
4851 = < ValueType extends FormValue , RequestType extends FormValue > ( props : FormProps < ValueType , RequestType > ) => {
4952
5053 const [ formDef , setFormDef ] : [ FormDefinition , Dispatch < SetStateAction < FormDefinition > > ]
@@ -60,8 +63,10 @@ const Form: <ValueType extends FormValue, RequestType extends FormValue>(props:
6063 = useState < RefObject < HTMLFormElement > > ( createRef < HTMLFormElement > ( ) )
6164
6265 // This will hold all the inputs
63- const [ inputs , setInputs ] : [ Array < FormInputModel > , Dispatch < SetStateAction < Array < FormInputModel > > > ] = useState < Array < FormInputModel > > ( formGetInputFields ( formDef . groups || [ ] ) )
64- const [ isFormInvalid , setFormInvalid ] : [ boolean , Dispatch < boolean > ] = useState < boolean > ( inputs . filter ( item => ! ! item . error ) . length > 0 )
66+ const [ inputs , setInputs ] : [ Array < FormInputModel > , Dispatch < SetStateAction < Array < FormInputModel > > > ]
67+ = useState < Array < FormInputModel > > ( formGetInputFields ( formDef . groups || [ ] ) )
68+ const [ isFormInvalid , setFormInvalid ] : [ boolean , Dispatch < boolean > ]
69+ = useState < boolean > ( inputs . filter ( item => ! ! item . error ) . length > 0 )
6570
6671 useEffect ( ( ) => {
6772 if ( ! formRef . current ?. elements ) {
@@ -129,7 +134,14 @@ const Form: <ValueType extends FormValue, RequestType extends FormValue>(props:
129134
130135 async function onSubmitAsync ( event : FormEvent < HTMLFormElement > ) : Promise < void > {
131136 const values : RequestType = props . requestGenerator ( inputs )
132- formOnSubmitAsync < RequestType > ( props . action || 'submit' , event , formDef , values , props . save , props . onSuccess )
137+ formOnSubmitAsync < RequestType > (
138+ props . action || 'submit' ,
139+ event ,
140+ formDef ,
141+ values ,
142+ props . save ,
143+ props . onSuccess ,
144+ )
133145 . then ( ( ) => {
134146 if ( ! props . resetFormAfterSave ) {
135147 setFormKey ( Date . now ( ) )
@@ -149,22 +161,17 @@ const Form: <ValueType extends FormValue, RequestType extends FormValue>(props:
149161
150162 formInitializeValues ( inputs , props . formValues )
151163
152- const setOnClickOnReset : ( button : FormButton ) => FormButton = button => {
164+ const setOnClickOnReset : ( button : FormButton ) => FormButton = button => (
153165 // if this is a reset button, set its onclick to reset
154- if ( ! ! button . isReset ) {
155- button = {
156- ...button ,
157- onClick : onReset ,
158- }
159- }
160-
161- return button
162- }
166+ ! button . isReset ? button : { ...button , onClick : onReset }
167+ )
163168
164- const createButtonGroup : ( groups : ReadonlyArray < FormButton > , isPrimaryGroup : boolean ) => Array < JSX . Element > = ( groups , isPrimaryGroup ) => groups . map ( ( button , index ) => {
165- button = setOnClickOnReset ( button )
169+ const createButtonGroup : ( groups : ReadonlyArray < FormButton > , isPrimaryGroup : boolean ) => Array < JSX . Element >
170+ = ( groups , isPrimaryGroup ) => groups . map ( ( button , index ) => {
171+ Object . assign ( button , setOnClickOnReset ( button ) )
166172
167- const disabled : boolean = ( button . isSubmit && isFormInvalid ) || ! ! props . shouldDisableButton ?.( isPrimaryGroup , index )
173+ const disabled : boolean = ( button . isSubmit && isFormInvalid )
174+ || ! ! props . shouldDisableButton ?.( isPrimaryGroup , index )
168175
169176 return (
170177 < Button
0 commit comments