Skip to content

(fix): accessibility on Select, aria-described-by of <select> should be defined when state is different of default #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ export const Select = memo(
return `select-${id}`;
})();

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

const selectStateDescriptionId = useId();
const stateIsDefault = state === "default";

const stateDescriptionId = getStateDescriptionId(stateIsDefault, selectStateDescriptionId);
const nativeDescription = getSelectNativeDescription(nativeSelectProps["aria-describedby"]);

const selectAriaDescribedBy = `${stateDescriptionId} ${nativeDescription}`.trim();

return (
<div
id={id}
Expand Down Expand Up @@ -104,17 +111,13 @@ export const Select = memo(
{...nativeSelectProps}
className={cx(fr.cx("fr-select"), nativeSelectProps.className)}
id={selectId}
aria-describedby={
nativeSelectProps["aria-describedby"] !== undefined
? `${stateDescriptionId} ${nativeSelectProps["aria-describedby"]}`
: stateDescriptionId
}
aria-describedby={selectAriaDescribedBy}
disabled={disabled}
>
{children}
</select>
<div id={messagesGroupId} className={fr.cx("fr-messages-group")} aria-live="polite">
{state !== "default" && (
{!stateIsDefault && (
<p
id={stateDescriptionId}
className={fr.cx(
Expand All @@ -140,6 +143,16 @@ export const Select = memo(
})
);

function getStateDescriptionId(stateIsDefault: boolean, selectStateDescriptionId: string) {
return stateIsDefault ? "" : `select-${selectStateDescriptionId}-desc`;
}

function getSelectNativeDescription(
nativeSelectPropsAriaDescribedBy: React.SelectHTMLAttributes<HTMLSelectElement>["aria-describedby"]
) {
return nativeSelectPropsAriaDescribedBy !== undefined ? nativeSelectPropsAriaDescribedBy : "";
}

Select.displayName = symToStr({ Select });

export default Select;