Skip to content

Commit

Permalink
fix: ensure aria-describedby only show when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jordankoschei-okta committed Jan 20, 2023
1 parent 1eb7608 commit a75a290
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/odyssey-react-mui/src/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Radio,
} from "./";
import { RadioGroup as MuiRadioGroup } from "@mui/material";
import { useMemo } from "react";

export interface RadioGroupProps {
/**
Expand Down Expand Up @@ -65,11 +66,20 @@ export const RadioGroup = ({
children,
defaultValue,
}: RadioGroupProps) => {
// Setting ariaDescribedBy this way to avoid linter's prefer-const error
const ariaDescribedBy = [
hint && `${name}-hint`,
error && `${name}-error`,
].filter(Boolean);
const radioGroupProps = useMemo(
() =>
error || hint
? {
"aria-describedby": [
hint && `${name}-hint`,
error && `${name}-error`,
]
.filter(Boolean)
.join(" "),
}
: undefined,
[error, hint]
);

return (
<FormControl component="fieldset" disabled={disabled} error={invalid}>
Expand All @@ -78,7 +88,7 @@ export const RadioGroup = ({
<MuiRadioGroup
defaultValue={defaultValue}
name={`${name}-group`}
aria-describedby={ariaDescribedBy.join(" ")}
{...radioGroupProps}
>
{children}
</MuiRadioGroup>
Expand Down

0 comments on commit a75a290

Please sign in to comment.