From 7af78d490aa61c867380bc8e98876bdf2452feb9 Mon Sep 17 00:00:00 2001 From: Khayal Alasgarov Date: Wed, 4 Dec 2024 12:36:22 -0800 Subject: [PATCH] feat: improve accessibility for single field components tckt-393 (#387) * feat: improve accessibility for single field components tckt-000 * refactor: improve accessibility and error handling for single field components tckt-393 --------- Co-authored-by: kalasgarov --- .../components/DateOfBirth/DateOfBirth.tsx | 56 ++++++++++++++----- .../Form/components/EmailInput/EmailInput.tsx | 17 ++++-- .../components/PhoneNumber/PhoneNumber.tsx | 16 +++--- .../SelectDropdown/SelectDropdown.tsx | 23 +++++--- 4 files changed, 77 insertions(+), 35 deletions(-) diff --git a/packages/design/src/Form/components/DateOfBirth/DateOfBirth.tsx b/packages/design/src/Form/components/DateOfBirth/DateOfBirth.tsx index 79f4432b..ff14afad 100644 --- a/packages/design/src/Form/components/DateOfBirth/DateOfBirth.tsx +++ b/packages/design/src/Form/components/DateOfBirth/DateOfBirth.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import classNames from 'classnames'; import { useFormContext } from 'react-hook-form'; import { type DateOfBirthProps } from '@atj/forms'; import { type PatternComponent } from '../../index.js'; @@ -18,6 +19,14 @@ const months = [ { value: '12', label: 'December' }, ]; +const getAriaDescribedBy = ( + errorId: string | null, + hintId: string | null +): string | undefined => { + const ids = [errorId, hintId].filter(Boolean).join(' '); + return ids || undefined; +}; + export const DateOfBirthPattern: PatternComponent = ({ monthId, dayId, @@ -28,6 +37,8 @@ export const DateOfBirthPattern: PatternComponent = ({ error, }) => { const { register } = useFormContext(); + const errorId = `input-error-message-${monthId}`; + const hintId = `hint-${monthId}`; return (
@@ -36,20 +47,32 @@ export const DateOfBirthPattern: PatternComponent = ({ {required && *} {hint && ( - + {hint} )} + {error && ( + + )}
@@ -81,22 +109,22 @@ export const DateOfBirthPattern: PatternComponent = ({ Year
- {error && ( - - {error.message} - - )}
); }; diff --git a/packages/design/src/Form/components/EmailInput/EmailInput.tsx b/packages/design/src/Form/components/EmailInput/EmailInput.tsx index 31588db4..2f2d9cc8 100644 --- a/packages/design/src/Form/components/EmailInput/EmailInput.tsx +++ b/packages/design/src/Form/components/EmailInput/EmailInput.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import classNames from 'classnames'; import { useFormContext } from 'react-hook-form'; import { type EmailInputProps } from '@atj/forms'; import { type PatternComponent } from '../../index.js'; @@ -10,6 +11,7 @@ export const EmailInputPattern: PatternComponent = ({ error, }) => { const { register } = useFormContext(); + const errorId = `input-error-message-${emailId}`; return (
@@ -18,20 +20,23 @@ export const EmailInputPattern: PatternComponent = ({ {label} {required && *} + {error && ( + + )} - {error && ( - - {error.message} - - )}
); }; diff --git a/packages/design/src/Form/components/PhoneNumber/PhoneNumber.tsx b/packages/design/src/Form/components/PhoneNumber/PhoneNumber.tsx index ee4adc1d..2661a3d2 100644 --- a/packages/design/src/Form/components/PhoneNumber/PhoneNumber.tsx +++ b/packages/design/src/Form/components/PhoneNumber/PhoneNumber.tsx @@ -13,6 +13,8 @@ export const PhoneNumberPattern: PatternComponent = ({ value, }) => { const { register } = useFormContext(); + const errorId = `input-error-message-${phoneId}`; + const hintId = `hint-${phoneId}`; return (
@@ -21,23 +23,18 @@ export const PhoneNumberPattern: PatternComponent = ({ className={classNames('usa-label', { 'usa-label--error': error, })} - id={`input-message-${phoneId}`} htmlFor={phoneId} > {label} {required && *} {hint && ( -
+
{hint}
)} {error && ( -
diff --git a/packages/design/src/Form/components/SelectDropdown/SelectDropdown.tsx b/packages/design/src/Form/components/SelectDropdown/SelectDropdown.tsx index ab9c3485..9ae3a59a 100644 --- a/packages/design/src/Form/components/SelectDropdown/SelectDropdown.tsx +++ b/packages/design/src/Form/components/SelectDropdown/SelectDropdown.tsx @@ -1,6 +1,6 @@ import React from 'react'; +import classNames from 'classnames'; import { useFormContext } from 'react-hook-form'; - import { type SelectDropdownProps } from '@atj/forms'; import { type PatternComponent } from '../../index.js'; @@ -12,13 +12,27 @@ export const SelectDropdownPattern: PatternComponent = ({ error, }) => { const { register } = useFormContext(); + const errorId = `input-error-message-${selectId}`; + return (
- @@ -28,11 +42,6 @@ export const SelectDropdownPattern: PatternComponent = ({ ))} - {error && ( - - {error.message} - - )}
); };