Skip to content

Commit a67822f

Browse files
committed
added focus to input on error on manual account form
1 parent edc7239 commit a67822f

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

src/views/manualAccount/ManualAccountForm.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import React, { MutableRefObject, useEffect, useState } from 'react'
2+
import React, { MutableRefObject, useEffect, useState, useRef } from 'react'
33
import { useSelector, useDispatch } from 'react-redux'
44

55
import { from, of, zip, defer } from 'rxjs'
@@ -61,6 +61,7 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun
6161
const getNextDelay = getDelay()
6262
const fields = getFormFields(props.accountType)
6363
const formRef = ref as MutableRefObject<HTMLInputElement>
64+
const inputRefs = useRef<Record<string, HTMLInputElement | null>>({})
6465
const {
6566
handleTextInputChange,
6667
handleSubmit,
@@ -152,6 +153,15 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun
152153
return () => createManualAccount$.unsubscribe()
153154
}, [saving])
154155

156+
useEffect(() => {
157+
for (const field of fields) {
158+
if (errors[field.name]) {
159+
inputRefs.current[field.name]?.focus()
160+
break
161+
}
162+
}
163+
}, [errors])
164+
155165
// When opening the date picker, upon return the focus is back at the beginning of the
156166
// form. We set returnField to know which field to focus when we return. If there is no
157167
// return field, we focus on the first item in the form.
@@ -227,7 +237,11 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun
227237
fullWidth={true}
228238
helperText={errors[field.name]}
229239
id={field.name}
230-
inputProps={{ 'data-test': 'date-input' }}
240+
inputProps={{
241+
'data-test': 'date-input',
242+
'aria-describedby': errors[field.name] ? field.name + '-error' : undefined,
243+
}}
244+
inputRef={(el: HTMLInputElement | null) => (inputRefs.current[field.name] = el)}
231245
label={field.label}
232246
name={field.name}
233247
onChange={() => {
@@ -266,7 +280,11 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun
266280
fullWidth={true}
267281
helperText={errors[field.name]}
268282
id={field.name}
269-
inputProps={{ 'data-test': `text-input-${field.name}` }}
283+
inputProps={{
284+
'data-test': `text-input-${field.name}`,
285+
'aria-describedby': errors[field.name] ? field.name + '-error' : undefined,
286+
}}
287+
inputRef={(el: HTMLInputElement | null) => (inputRefs.current[field.name] = el)}
270288
label={field.label}
271289
name={field.name}
272290
onChange={handleTextInputChange}

0 commit comments

Comments
 (0)