Skip to content

Commit 8c20590

Browse files
(Checkbox, CheckboxGroup): handle a11y
1 parent 4bde0ab commit 8c20590

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/scripts/Checkbox.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React, {
66
ReactNode,
77
} from 'react';
88
import classnames from 'classnames';
9-
import { FormElement, FormElementProps } from './FormElement';
9+
import { FormElement } from './FormElement';
1010
import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
1111

1212
/**
@@ -15,7 +15,6 @@ import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
1515
export type CheckboxProps = {
1616
label?: string;
1717
required?: boolean;
18-
error?: FormElementProps['error'];
1918
cols?: number;
2019
name?: string;
2120
value?: CheckboxValueType;
@@ -33,10 +32,10 @@ export type CheckboxProps = {
3332
export const Checkbox: FC<CheckboxProps> = (props) => {
3433
const {
3534
type, // eslint-disable-line @typescript-eslint/no-unused-vars
35+
id,
3636
className,
3737
label,
3838
required,
39-
error,
4039
cols,
4140
tooltip,
4241
tooltipIcon,
@@ -45,10 +44,12 @@ export const Checkbox: FC<CheckboxProps> = (props) => {
4544
children,
4645
...rprops
4746
} = props;
48-
const { grouped } = useContext(CheckboxGroupContext);
47+
48+
const { grouped, error, errorId } = useContext(CheckboxGroupContext);
4949
const formElemProps = {
5050
required,
5151
error,
52+
errorId,
5253
cols,
5354
tooltip,
5455
tooltipIcon,
@@ -57,8 +58,14 @@ export const Checkbox: FC<CheckboxProps> = (props) => {
5758
const checkClassNames = classnames(className, 'slds-checkbox');
5859
const check = (
5960
<div className={checkClassNames}>
60-
<input ref={inputRef} type='checkbox' {...rprops} />
61-
<label className='slds-checkbox__label'>
61+
<input
62+
ref={inputRef}
63+
type='checkbox'
64+
{...rprops}
65+
id={id}
66+
aria-describedby={error ? errorId : undefined}
67+
/>
68+
<label className='slds-checkbox__label' htmlFor={id}>
6269
<span className='slds-checkbox_faux' />
6370
<span className='slds-form-element__label'>{label || children}</span>
6471
</label>

src/scripts/CheckboxGroup.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {
2+
useId,
23
createContext,
34
FieldsetHTMLAttributes,
45
Ref,
@@ -23,7 +24,11 @@ export type CheckboxValueType = string | number;
2324
/**
2425
*
2526
*/
26-
export const CheckboxGroupContext = createContext<{ grouped?: boolean }>({});
27+
export const CheckboxGroupContext = createContext<{
28+
grouped?: boolean;
29+
error?: FormElementProps['error'];
30+
errorId?: string;
31+
}>({});
2732

2833
/**
2934
*
@@ -107,19 +112,25 @@ export const CheckboxGroup = createFC<
107112
? error.message
108113
: undefined
109114
: undefined;
110-
const grpCtx = useMemo(() => ({ grouped: true }), []);
115+
116+
const errorId = useId();
117+
const grpCtx = useMemo(
118+
() => ({ grouped: true, error, errorId }),
119+
[error, errorId]
120+
);
111121

112122
return (
113123
<fieldset
114124
ref={elementRef}
115125
className={grpClassNames}
116126
style={grpStyles}
127+
aria-required={required}
117128
{...rprops}
118129
onChange={onChange}
119130
>
120131
<legend className='slds-form-element__label'>
121132
{required ? (
122-
<abbr className='slds-required' title='required'>
133+
<abbr className='slds-required' title='required' aria-hidden='true'>
123134
*
124135
</abbr>
125136
) : undefined}
@@ -135,7 +146,12 @@ export const CheckboxGroup = createFC<
135146
{children}
136147
</CheckboxGroupContext.Provider>
137148
{errorMessage ? (
138-
<div className='slds-form-element__help'>{errorMessage}</div>
149+
<div
150+
className='slds-form-element__help'
151+
id={error ? errorId : undefined}
152+
>
153+
{errorMessage}
154+
</div>
139155
) : undefined}
140156
</div>
141157
</fieldset>

0 commit comments

Comments
 (0)