Skip to content

Commit 72353ea

Browse files
authored
Merge pull request #470 from mashmatrix/support-slds-2-checkbox-checkbox-group
Update `Checkbox` and `CheckboxGroup` for SLDS2
2 parents 5339bd6 + 3a49bac commit 72353ea

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/scripts/Checkbox.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React, {
22
FC,
33
InputHTMLAttributes,
44
Ref,
5+
useId,
56
useContext,
67
ReactNode,
78
} from 'react';
89
import classnames from 'classnames';
9-
import { FormElement, FormElementProps } from './FormElement';
10+
import { FormElement } from './FormElement';
1011
import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
1112

1213
/**
@@ -15,7 +16,6 @@ import { CheckboxGroupContext, CheckboxValueType } from './CheckboxGroup';
1516
export type CheckboxProps = {
1617
label?: string;
1718
required?: boolean;
18-
error?: FormElementProps['error'];
1919
cols?: number;
2020
name?: string;
2121
value?: CheckboxValueType;
@@ -33,10 +33,10 @@ export type CheckboxProps = {
3333
export const Checkbox: FC<CheckboxProps> = (props) => {
3434
const {
3535
type, // eslint-disable-line @typescript-eslint/no-unused-vars
36+
id: id_,
3637
className,
3738
label,
3839
required,
39-
error,
4040
cols,
4141
tooltip,
4242
tooltipIcon,
@@ -45,22 +45,35 @@ export const Checkbox: FC<CheckboxProps> = (props) => {
4545
children,
4646
...rprops
4747
} = props;
48-
const { grouped } = useContext(CheckboxGroupContext);
48+
49+
const prefix = useId();
50+
const id = id_ ?? `${prefix}-id`;
51+
52+
const { grouped, error, errorId } = useContext(CheckboxGroupContext);
4953
const formElemProps = {
5054
required,
5155
error,
56+
errorId,
5257
cols,
5358
tooltip,
5459
tooltipIcon,
5560
elementRef,
5661
};
5762
const checkClassNames = classnames(className, 'slds-checkbox');
5863
const check = (
59-
<label className={checkClassNames}>
60-
<input ref={inputRef} type='checkbox' {...rprops} />
61-
<span className='slds-checkbox_faux' />
62-
<span className='slds-form-element__label'>{label || children}</span>
63-
</label>
64+
<div className={checkClassNames}>
65+
<input
66+
ref={inputRef}
67+
type='checkbox'
68+
{...rprops}
69+
id={id}
70+
aria-describedby={error ? errorId : undefined}
71+
/>
72+
<label className='slds-checkbox__label' htmlFor={id}>
73+
<span className='slds-checkbox_faux' />
74+
<span className='slds-form-element__label'>{label || children}</span>
75+
</label>
76+
</div>
6477
);
6578
return grouped ? (
6679
check

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)