Skip to content

Commit

Permalink
[Accessibility] Input/Select - wrapped error message within live regi…
Browse files Browse the repository at this point in the history
…on (#312)

* chore: Input - wrapped error message within live region

* chore: input aria-live changed from assertive to polite

* chore: Select component, added support for aria-live
  • Loading branch information
Destinyyyy authored Sep 23, 2024
1 parent 44243e3 commit 296de45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
49 changes: 26 additions & 23 deletions src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const Input = memo(
})();

const messageId = `${inputId}-desc-error`;
const messagesGroupId = `${inputId}-messages-group`;

return (
<div
Expand Down Expand Up @@ -182,29 +183,31 @@ export const Input = memo(
nativeInputOrTextArea
);
})()}
{state !== "default" && (
<p
id={messageId}
className={cx(
fr.cx(
(() => {
switch (state) {
case "error":
return "fr-error-text";
case "success":
return "fr-valid-text";
case "info":
return "fr-info-text";
}
assert<Equals<typeof state, never>>();
})()
),
classes.message
)}
>
{stateRelatedMessage}
</p>
)}
<div id={messagesGroupId} className={fr.cx("fr-messages-group")} aria-live="polite">
{state !== "default" && (
<p
id={messageId}
className={cx(
fr.cx(
(() => {
switch (state) {
case "error":
return "fr-error-text";
case "success":
return "fr-valid-text";
case "info":
return "fr-info-text";
}
assert<Equals<typeof state, never>>();
})()
),
classes.message
)}
>
{stateRelatedMessage}
</p>
)}
</div>
</div>
);
})
Expand Down
39 changes: 21 additions & 18 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const Select = memo(
})();

const stateDescriptionId = `select-${useId()}-desc`;
const messagesGroupId = `${selectId}-messages-group`;

return (
<div
Expand Down Expand Up @@ -107,24 +108,26 @@ export const Select = memo(
>
{children}
</select>
{state !== "default" && (
<p
id={stateDescriptionId}
className={fr.cx(
(() => {
switch (state) {
case "error":
return "fr-error-text";
case "success":
return "fr-valid-text";
}
assert<Equals<typeof state, never>>(false);
})()
)}
>
{stateRelatedMessage}
</p>
)}
<div id={messagesGroupId} className={fr.cx("fr-messages-group")} aria-live="polite">
{state !== "default" && (
<p
id={stateDescriptionId}
className={fr.cx(
(() => {
switch (state) {
case "error":
return "fr-error-text";
case "success":
return "fr-valid-text";
}
assert<Equals<typeof state, never>>(false);
})()
)}
>
{stateRelatedMessage}
</p>
)}
</div>
</div>
);
})
Expand Down

0 comments on commit 296de45

Please sign in to comment.