Skip to content

Commit 8233ab6

Browse files
committed
fix: accessibility issues
1 parent 27c2386 commit 8233ab6

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

app/components/form/field-radio-group/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export const FieldRadioGroup = <
6666
<RadioGroup
6767
id={ctx.id}
6868
aria-invalid={fieldState.error ? true : undefined}
69+
/**
70+
* RadioGroup is a div with role `radiogroup`. the label's `for` is not working and we need to explicitly set the ARIA attribute.
71+
*/
72+
aria-labelledby={ctx.labelId}
6973
aria-describedby={
7074
!fieldState.error
7175
? `${ctx.descriptionId}`
@@ -76,9 +80,11 @@ export const FieldRadioGroup = <
7680
{...field}
7781
>
7882
{options.map(({ label, ...option }) => {
83+
const radioId = `${ctx.id}-${option.value}`;
84+
7985
if (renderOption) {
8086
return (
81-
<React.Fragment key={`${ctx.id}-${option.value}`}>
87+
<React.Fragment key={radioId}>
8288
{renderOption({
8389
label,
8490
onBlur,
@@ -89,11 +95,7 @@ export const FieldRadioGroup = <
8995
}
9096

9197
return (
92-
<Radio
93-
key={`${ctx.id}-${option.value}`}
94-
onBlur={onBlur}
95-
{...option}
96-
>
98+
<Radio key={radioId} onBlur={onBlur} {...option}>
9799
{label}
98100
</Radio>
99101
);

app/components/form/form-field-helper.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { ComponentProps } from 'react';
22

33
import { cn } from '@/lib/tailwind/utils';
44

5+
import { useFormField } from '@/components/form/form-field';
6+
57
export const FormFieldHelper = ({
68
className,
79
...props
810
}: ComponentProps<'div'>) => {
11+
const ctx = useFormField();
912
return (
1013
<div
14+
id={ctx.descriptionId}
1115
className={cn('text-xs text-muted-foreground', className)}
1216
{...props}
1317
/>

app/components/form/form-field-label.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { cn } from '@/lib/tailwind/utils';
44

55
import { useFormField } from './form-field';
66

7-
type FormFieldLabelProps = ComponentProps<'label'>;
7+
type FormFieldLabelProps = ComponentProps<'label'> & {
8+
asDiv?: boolean;
9+
};
810

911
export const FormFieldLabel = ({
1012
className,
@@ -13,6 +15,7 @@ export const FormFieldLabel = ({
1315
const ctx = useFormField();
1416
return (
1517
<label
18+
id={ctx.labelId}
1619
htmlFor={ctx.id}
1720
className={cn('flex gap-1.5 align-baseline text-sm', className)}
1821
{...props}

app/components/form/form-field.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const FormField = (props: FormFieldProps) => {
1818
const contextValue = useMemo(
1919
() => ({
2020
id,
21+
labelId: `${id}-label`,
2122
descriptionId: `${id}-description`,
2223
errorId: `${id}-error`,
2324
size: props.size,
@@ -36,6 +37,7 @@ export const FormField = (props: FormFieldProps) => {
3637

3738
type FormFieldContextValue = {
3839
id: string;
40+
labelId: string;
3941
descriptionId: string;
4042
errorId: string;
4143
size?: FormFieldSize;

0 commit comments

Comments
 (0)