|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | | -import React, { MutableRefObject, useEffect, useState } from 'react' |
| 2 | +import React, { MutableRefObject, useEffect, useState, useRef } from 'react' |
3 | 3 | import { useSelector, useDispatch } from 'react-redux' |
4 | 4 |
|
5 | 5 | import { from, of, zip, defer } from 'rxjs' |
@@ -61,6 +61,7 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun |
61 | 61 | const getNextDelay = getDelay() |
62 | 62 | const fields = getFormFields(props.accountType) |
63 | 63 | const formRef = ref as MutableRefObject<HTMLInputElement> |
| 64 | + const inputRefs = useRef<Record<string, HTMLInputElement | null>>({}) |
64 | 65 | const { |
65 | 66 | handleTextInputChange, |
66 | 67 | handleSubmit, |
@@ -152,6 +153,15 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun |
152 | 153 | return () => createManualAccount$.unsubscribe() |
153 | 154 | }, [saving]) |
154 | 155 |
|
| 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 | + |
155 | 165 | // When opening the date picker, upon return the focus is back at the beginning of the |
156 | 166 | // form. We set returnField to know which field to focus when we return. If there is no |
157 | 167 | // return field, we focus on the first item in the form. |
@@ -227,7 +237,11 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun |
227 | 237 | fullWidth={true} |
228 | 238 | helperText={errors[field.name]} |
229 | 239 | 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)} |
231 | 245 | label={field.label} |
232 | 246 | name={field.name} |
233 | 247 | onChange={() => { |
@@ -266,7 +280,11 @@ export const ManualAccountForm = React.forwardRef<HTMLInputElement, ManualAccoun |
266 | 280 | fullWidth={true} |
267 | 281 | helperText={errors[field.name]} |
268 | 282 | 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)} |
270 | 288 | label={field.label} |
271 | 289 | name={field.name} |
272 | 290 | onChange={handleTextInputChange} |
|
0 commit comments