Skip to content

Commit ea472f1

Browse files
committed
fix(webapp): separate checkbox names and descriptions
1 parent f436aa1 commit ea472f1

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

apps/webapp/app/components/primitives/Checkbox.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ export const CheckboxWithLabel = React.forwardRef<HTMLInputElement, CheckboxProp
8686
) => {
8787
const [isChecked, setIsChecked] = useState<boolean>(defaultChecked ?? false);
8888
const [isDisabled, setIsDisabled] = useState<boolean>(disabled ?? false);
89+
const generatedId = React.useId();
90+
const inputId = id ?? generatedId;
91+
const labelId = `${inputId}-label`;
92+
const descriptionId = `${inputId}-description`;
93+
const ariaLabelledBy =
94+
props["aria-label"] || props["aria-labelledby"] ? props["aria-labelledby"] : labelId;
8995

9096
const buttonClassName = variants[variant].button;
9197
const labelClassName = variants[variant].label;
@@ -125,6 +131,11 @@ export const CheckboxWithLabel = React.forwardRef<HTMLInputElement, CheckboxProp
125131
type="checkbox"
126132
value={value}
127133
checked={isChecked}
134+
aria-labelledby={ariaLabelledBy}
135+
aria-describedby={
136+
props["aria-describedby"] ??
137+
(variant === "description" && description ? descriptionId : undefined)
138+
}
128139
onChange={(e) => {
129140
if (isDisabled || props.readOnly === true) return;
130141
setIsChecked(e.target.checked);
@@ -139,12 +150,13 @@ export const CheckboxWithLabel = React.forwardRef<HTMLInputElement, CheckboxProp
139150
(isDisabled || props.readOnly) &&
140151
"bg-background-raised! checked:bg-background-raised! checked:group-hover:bg-background-raised! group-hover:bg-background-raised!"
141152
)}
142-
id={id}
153+
id={inputId}
143154
ref={ref}
144155
/>
145156
<div>
146157
<div className="flex items-center gap-x-2">
147158
<span
159+
id={labelId}
148160
className={cn(
149161
props.readOnly || disabled ? "cursor-default" : "cursor-pointer",
150162
labelClassName,
@@ -162,7 +174,11 @@ export const CheckboxWithLabel = React.forwardRef<HTMLInputElement, CheckboxProp
162174
)}
163175
</div>
164176
{variant === "description" && (
165-
<Paragraph variant="small" className={cn("mt-0.5", descriptionClassName)}>
177+
<Paragraph
178+
id={descriptionId}
179+
variant="small"
180+
className={cn("mt-0.5", descriptionClassName)}
181+
>
166182
{description}
167183
</Paragraph>
168184
)}

0 commit comments

Comments
 (0)