88 FormErrorMessage ,
99 formGetInputModel ,
1010 formInitializeValues ,
11+ formOnChange ,
1112 formReset ,
1213 formSubmitAsync ,
13- formValidateAndUpdateAsync ,
1414} from './form-functions'
1515import { InputText , InputTextarea } from './form-input'
1616import { FormInputModel } from './form-input.model'
@@ -22,13 +22,14 @@ interface FormProps<ValueType, RequestType> {
2222 readonly requestGenerator : ( inputs : ReadonlyArray < FormInputModel > ) => RequestType
2323 readonly resetOnError : boolean
2424 readonly save : ( value : RequestType ) => Promise < void >
25+ readonly succeeded ?: ( ) => void
2526}
2627
2728const Form : < ValueType extends any , RequestType extends any > ( props : FormProps < ValueType , RequestType > ) => JSX . Element
2829 = < ValueType extends any , RequestType extends any > ( props : FormProps < ValueType , RequestType > ) => {
2930
3031 const [ disableSave , setDisableSave ] : [ boolean , Dispatch < SetStateAction < boolean > > ]
31- = useState < boolean > ( true )
32+ = useState < boolean > ( false )
3233
3334 const [ formDef , setFormDef ] : [ FormDefinition , Dispatch < SetStateAction < FormDefinition > > ]
3435 = useState < FormDefinition > ( { ...props . formDef } )
@@ -48,14 +49,14 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
4849 }
4950
5051 async function onChange ( event : FormEvent < HTMLFormElement > ) : Promise < void > {
51- const isValid : boolean = await formValidateAndUpdateAsync ( event , formDef . inputs )
52+ const isValid : boolean = await formOnChange ( event , formDef . inputs )
5253 setFormDef ( { ...formDef } )
5354 setDisableSave ( ! isValid )
5455 }
5556
5657 function onFocus ( event : FocusEvent < HTMLInputElement | HTMLTextAreaElement > ) : void {
5758 const inputDef : FormInputModel = formGetInputModel ( props . formDef . inputs , event . target . name )
58- inputDef . dirtyOrTouched = true
59+ inputDef . touched = true
5960 setFormDef ( { ...formDef } )
6061 }
6162
@@ -67,7 +68,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
6768
6869 async function onSubmit ( event : FormEvent < HTMLFormElement > ) : Promise < void > {
6970 const values : RequestType = props . requestGenerator ( formDef . inputs )
70- formSubmitAsync < RequestType , void > ( event , formDef . inputs , props . formDef . title , values , props . save , setDisableSave )
71+ formSubmitAsync < RequestType , void > ( event , formDef . inputs , props . formDef . title , values , props . save , setDisableSave , props . succeeded )
7172 . then ( ( ) => {
7273 setFormKey ( Date . now ( ) )
7374 formReset ( formDef . inputs , props . formValues )
@@ -85,9 +86,10 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
8586
8687 formInitializeValues ( formDef . inputs , props . formValues )
8788
88- const formInputs : Array < JSX . Element > = props . formDef . inputs
89- . map ( input => formGetInputModel ( props . formDef . inputs , input . name ) )
89+ const formInputs : Array < JSX . Element > = formDef . inputs
90+ . map ( input => formGetInputModel ( formDef . inputs , input . name ) )
9091 . map ( ( inputModel , index ) => {
92+ const tabIndex : number = inputModel . notTabbable ? - 1 : index + 1 + ( formDef . tabIndexStart || 0 )
9193 switch ( inputModel . type ) {
9294 case 'textarea' :
9395 return (
@@ -96,7 +98,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
9698 key = { inputModel . name }
9799 onBlur = { onBlur }
98100 onFocus = { onFocus }
99- tabIndex = { inputModel . notTabbable ? - 1 : index + 1 }
101+ tabIndex = { tabIndex }
100102 value = { inputModel . value }
101103 />
102104 )
@@ -107,7 +109,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
107109 key = { inputModel . name }
108110 onBlur = { onBlur }
109111 onFocus = { onFocus }
110- tabIndex = { inputModel . notTabbable ? - 1 : index + 1 }
112+ tabIndex = { tabIndex }
111113 type = { inputModel . type || 'text' }
112114 value = { inputModel . value }
113115 />
@@ -116,7 +118,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
116118
117119 } )
118120
119- const buttons : Array < JSX . Element > = props . formDef . buttons
121+ const buttons : Array < JSX . Element > = formDef . buttons
120122 . map ( ( button , index ) => {
121123 // if this is a reset button, set its onclick to reset
122124 if ( ! ! button . isReset ) {
@@ -130,7 +132,7 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
130132 { ...button }
131133 disable = { button . isSave && disableSave }
132134 key = { button . label }
133- tabIndex = { button . notTabble ? - 1 : index + props . formDef . inputs . length }
135+ tabIndex = { button . notTabble ? - 1 : index + formDef . inputs . length + ( formDef . tabIndexStart || 0 ) }
134136 />
135137 )
136138 } )
@@ -145,12 +147,16 @@ const Form: <ValueType extends any, RequestType extends any>(props: FormProps<Va
145147
146148 < h2 > { props . formDef . title } </ h2 >
147149
150+ < hr />
151+
148152 < div className = { styles [ 'form-fields' ] } >
149153 { formInputs }
150154 </ div >
151155
152- < div className = 'button-container' >
153- { buttons }
156+ < div className = 'button-container-outer' >
157+ < div className = 'button-container-inner' >
158+ { buttons }
159+ </ div >
154160 </ div >
155161
156162 </ form >
0 commit comments