Skip to content

Commit 5339bd6

Browse files
authored
Merge pull request #469 from mashmatrix/support-slds-2-radio-radio-group
Update `Radio` and `RadioGroup` for SLDS2
2 parents 896364e + 03449e5 commit 5339bd6

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/scripts/Radio.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, InputHTMLAttributes, Ref, useContext } from 'react';
1+
import React, { FC, InputHTMLAttributes, Ref, useId, useContext } from 'react';
22
import classnames from 'classnames';
33
import { RadioGroupContext, RadioValueType } from './RadioGroup';
44
import { useEventCallback } from './hooks';
@@ -17,6 +17,7 @@ export type RadioProps = {
1717
*
1818
*/
1919
export const Radio: FC<RadioProps> = ({
20+
id: id_,
2021
className,
2122
label,
2223
name,
@@ -26,7 +27,16 @@ export const Radio: FC<RadioProps> = ({
2627
children,
2728
...props
2829
}) => {
29-
const { name: grpName, onValueChange } = useContext(RadioGroupContext);
30+
const {
31+
name: grpName,
32+
error,
33+
errorId,
34+
onValueChange,
35+
} = useContext(RadioGroupContext);
36+
37+
const prefix = useId();
38+
const id = id_ ?? `${prefix}-id`;
39+
3040
const onChange = useEventCallback(
3141
(e: React.ChangeEvent<HTMLInputElement>) => {
3242
onChange_?.(e);
@@ -37,17 +47,21 @@ export const Radio: FC<RadioProps> = ({
3747
);
3848
const radioClassNames = classnames(className, 'slds-radio');
3949
return (
40-
<label className={radioClassNames}>
50+
<span className={radioClassNames}>
4151
<input
4252
ref={inputRef}
4353
type='radio'
4454
name={name ?? grpName}
4555
value={value}
4656
onChange={onChange}
4757
{...props}
58+
id={id}
59+
aria-describedby={error ? errorId : undefined}
4860
/>
49-
<span className='slds-radio_faux' />
50-
<span className='slds-form-element__label'>{label || children}</span>
51-
</label>
61+
<label className='slds-radio__label' htmlFor={id}>
62+
<span className='slds-radio_faux' />
63+
<span className='slds-form-element__label'>{label || children}</span>
64+
</label>
65+
</span>
5266
);
5367
};

src/scripts/RadioGroup.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {
2+
useId,
23
HTMLAttributes,
34
Ref,
45
createContext,
@@ -9,6 +10,7 @@ import React, {
910
import classnames from 'classnames';
1011
import { FieldSetColumnContext } from './FieldSet';
1112
import { TooltipContent } from './TooltipContent';
13+
import { FormElementProps } from './FormElement';
1214
import { createFC } from './common';
1315
import { Bivariant } from './typeUtils';
1416

@@ -22,6 +24,8 @@ export type RadioValueType = string | number;
2224
*/
2325
export const RadioGroupContext = createContext<{
2426
name?: string;
27+
error?: FormElementProps['error'];
28+
errorId?: string;
2529
onValueChange?: Bivariant<(value: RadioValueType) => void>;
2630
}>({});
2731

@@ -83,21 +87,25 @@ export const RadioGroup = createFC<RadioGroupProps, { isFormElement: boolean }>(
8387
? error.message
8488
: undefined
8589
: undefined;
90+
91+
const errorId = useId();
8692
const grpCtx = useMemo(
87-
() => ({ name, onValueChange }),
88-
[name, onValueChange]
93+
() => ({ name, error, errorId, onValueChange }),
94+
[name, error, errorId, onValueChange]
8995
);
9096

9197
return (
9298
<fieldset
9399
ref={elementRef}
94100
className={grpClassNames}
95101
style={grpStyles}
102+
role='radiogroup'
103+
aria-required={required}
96104
{...rprops}
97105
>
98106
<legend className='slds-form-element__label'>
99107
{required ? (
100-
<abbr className='slds-required' title='required'>
108+
<abbr className='slds-required' title='required' aria-hidden='true'>
101109
*
102110
</abbr>
103111
) : undefined}
@@ -112,7 +120,12 @@ export const RadioGroup = createFC<RadioGroupProps, { isFormElement: boolean }>(
112120
<RadioGroupContext.Provider value={grpCtx}>
113121
{children}
114122
{errorMessage ? (
115-
<div className='slds-form-element__help'>{errorMessage}</div>
123+
<div
124+
className='slds-form-element__help'
125+
id={error ? errorId : undefined}
126+
>
127+
{errorMessage}
128+
</div>
116129
) : undefined}
117130
</RadioGroupContext.Provider>
118131
</div>

0 commit comments

Comments
 (0)