Skip to content

Commit 9a16afd

Browse files
reidbarberdannify
andauthored
Add docs for HelpText to CheckboxGroup and RadioGroup (#3528)
* add to checkbox group * add to radio group * add to CheckboxGroup docs * add to RadioGroup docs * add to useCheckboxGroup docs * add to useRadioGroup docs * update error display logic in docs * fix merge conflict * fix merge * example update Co-authored-by: Danni <[email protected]>
1 parent 1d444f6 commit 9a16afd

File tree

4 files changed

+132
-7
lines changed

4 files changed

+132
-7
lines changed

packages/@react-aria/checkbox/docs/useCheckboxGroup.mdx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ standalone checkbox, use the [useCheckbox](useCheckbox.html) hook instead.
9696

9797
This example uses native input elements for the checkboxes, and React context to share state from the group
9898
to each checkbox. An HTML `<label>` element wraps the native input and the text to provide an implicit label
99-
for the radio.
99+
for the checkbox.
100100

101101
```tsx example export=true
102102
import {useCheckboxGroup, useCheckboxGroupItem} from '@react-aria/checkbox';
@@ -105,16 +105,20 @@ import {useCheckboxGroupState} from '@react-stately/checkbox';
105105
let CheckboxGroupContext = React.createContext(null);
106106

107107
function CheckboxGroup(props) {
108-
let {children, label} = props;
108+
let {children, label, description, errorMessage, validationState} = props;
109109
let state = useCheckboxGroupState(props);
110-
let {groupProps, labelProps} = useCheckboxGroup(props, state);
110+
let {groupProps, labelProps, descriptionProps, errorMessageProps} = useCheckboxGroup(props, state);
111111

112112
return (
113113
<div {...groupProps}>
114114
<span {...labelProps}>{label}</span>
115115
<CheckboxGroupContext.Provider value={state}>
116116
{children}
117117
</CheckboxGroupContext.Provider>
118+
{description && <div {...descriptionProps} style={{fontSize: 12}}>{description}</div>}
119+
{errorMessage && validationState === 'invalid' &&
120+
<div {...errorMessageProps} style={{color: 'red', fontSize: 12}}>{errorMessage}</div>
121+
}
118122
</div>
119123
);
120124
}
@@ -196,6 +200,31 @@ function Example() {
196200
}
197201
```
198202

203+
### Description
204+
205+
The `description` prop can be used to associate additional help text with a checkbox group.
206+
207+
```tsx example
208+
<CheckboxGroup label="Favorite sports" description="Select your favorite sports.">
209+
<Checkbox value="soccer">Soccer</Checkbox>
210+
<Checkbox value="baseball">Baseball</Checkbox>
211+
<Checkbox value="basketball">Basketball</Checkbox>
212+
</CheckboxGroup>
213+
```
214+
215+
### Error message
216+
217+
The `errorMessage` prop can be used to help the user fix a validation error. It should be combined with the `validationState` prop to
218+
semantically mark the checkbox group as invalid for assistive technologies.
219+
220+
```tsx example
221+
<CheckboxGroup label="Favorite sports" errorMessage="Invalid selection of sports." validationState="invalid">
222+
<Checkbox value="soccer">Soccer</Checkbox>
223+
<Checkbox value="baseball">Baseball</Checkbox>
224+
<Checkbox value="basketball">Basketball</Checkbox>
225+
</CheckboxGroup>
226+
```
227+
199228
### Disabled
200229

201230
The entire CheckboxGroup can be disabled with the `isDisabled` prop.

packages/@react-aria/radio/docs/useRadioGroup.mdx

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ import {useRadioGroupState} from '@react-stately/radio';
102102
let RadioContext = React.createContext(null);
103103

104104
function RadioGroup(props) {
105-
let {children, label} = props;
105+
let {children, label, description, errorMessage, validationState} = props;
106106
let state = useRadioGroupState(props);
107-
let {radioGroupProps, labelProps} = useRadioGroup(props, state);
107+
let {radioGroupProps, labelProps, descriptionProps, errorMessageProps} = useRadioGroup(props, state);
108108

109109
return (
110110
<div {...radioGroupProps}>
111111
<span {...labelProps}>{label}</span>
112112
<RadioContext.Provider value={state}>
113113
{children}
114114
</RadioContext.Provider>
115+
{description && <div {...descriptionProps} style={{fontSize: 12}}>{description}</div>}
116+
{errorMessage && validationState === 'invalid' &&
117+
<div {...errorMessageProps} style={{color: 'red', fontSize: 12}}>{errorMessage}</div>
118+
}
115119
</div>
116120
)
117121
}
@@ -158,16 +162,20 @@ import {useFocusRing} from '@react-aria/focus';
158162
let RadioContext = React.createContext(null);
159163

160164
function RadioGroup(props) {
161-
let {children, label} = props;
165+
let {children, label, description, errorMessage, validationState} = props;
162166
let state = useRadioGroupState(props);
163-
let {radioGroupProps, labelProps} = useRadioGroup(props, state);
167+
let {radioGroupProps, labelProps, descriptionProps, errorMessageProps} = useRadioGroup(props, state);
164168

165169
return (
166170
<div {...radioGroupProps}>
167171
<span {...labelProps}>{label}</span>
168172
<RadioContext.Provider value={state}>
169173
{children}
170174
</RadioContext.Provider>
175+
{description && <div {...descriptionProps} style={{fontSize: 12}}>{description}</div>}
176+
{errorMessage && validationState === 'invalid' &&
177+
<div {...errorMessageProps} style={{color: 'red', fontSize: 12}}>{errorMessage}</div>
178+
}
171179
</div>
172180
)
173181
}
@@ -274,6 +282,29 @@ function Example() {
274282
}
275283
```
276284

285+
### Description
286+
287+
The `description` prop can be used to associate additional help text with a radio group.
288+
289+
```tsx example
290+
<RadioGroup label="Favorite pet" description="Select your favorite pet.">
291+
<Radio value="dogs">Dogs</Radio>
292+
<Radio value="cats">Cats</Radio>
293+
</RadioGroup>
294+
```
295+
296+
### Error message
297+
298+
The `errorMessage` prop can be used to help the user fix a validation error. It should be combined with the `validationState` prop to
299+
semantically mark the radio group as invalid for assistive technologies.
300+
301+
```tsx example
302+
<RadioGroup label="Favorite pet" errorMessage="Invalid pet selection." validationState="invalid">
303+
<Radio value="dogs">Dogs</Radio>
304+
<Radio value="cats">Cats</Radio>
305+
</RadioGroup>
306+
```
307+
277308
### Disabled
278309

279310
The entire RadioGroup can be disabled with the `isDisabled` prop.

packages/@react-spectrum/checkbox/docs/CheckboxGroup.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,36 @@ and "end" refers to the right most edge. For right-to-left (RTL) languages, this
228228
</CheckboxGroup>
229229
```
230230

231+
### Help text
232+
[View guidelines](https://spectrum.adobe.com/page/checkbox-group/#Help-text-(description-and-error-message))
233+
234+
Both a description and an error message can be supplied to a CheckboxGroup. The description is always visible unless the `validationState` is “invalid” and an error message is provided. The error message can be used to help the user fix their input quickly and should be specific to the detected error. All strings should be localized.
235+
236+
```tsx example
237+
function Example() {
238+
let [checked, setChecked] = React.useState(['dogs', 'dragons']);
239+
let isValid = checked.length === 2 && checked.includes('dogs') && checked.includes('dragons');
240+
241+
return (
242+
<CheckboxGroup
243+
label="Pets"
244+
onChange={setChecked}
245+
value={checked}
246+
validationState={isValid ? 'valid' : 'invalid'}
247+
description="Select your pets."
248+
errorMessage={
249+
checked.includes('cats')
250+
? 'No cats allowed.'
251+
: 'Select only dogs and dragons.'
252+
}>
253+
<Checkbox value="dogs">Dogs</Checkbox>
254+
<Checkbox value="cats">Cats</Checkbox>
255+
<Checkbox value="dragons">Dragons</Checkbox>
256+
</CheckboxGroup>
257+
);
258+
}
259+
```
260+
231261
### Disabled
232262
[View guidelines](https://spectrum.adobe.com/page/checkbox/#Disabled)
233263

packages/@react-spectrum/radio/docs/RadioGroup.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,41 @@ and "end" refers to the right most edge. For right-to-left (RTL) languages, this
185185
</RadioGroup>
186186
```
187187

188+
### Help text
189+
[View guidelines](https://spectrum.adobe.com/page/radio-group/#Help-text-(description-and-error-message))
190+
191+
Both a description and an error message can be supplied to a RadioGroup. The description is always visible unless the `validationState` is “invalid” and an error message is provided. The error message can be used to help the user fix their input quickly and should be specific to the detected error. All strings should be localized.
192+
193+
```tsx example
194+
function Example() {
195+
let [selected, setSelected] = React.useState('dogs');
196+
let isValid = selected === 'dogs';
197+
198+
return (
199+
<RadioGroup
200+
aria-label="Favorite pet"
201+
onChange={setSelected}
202+
validationState={isValid ? 'valid' : 'invalid'}
203+
description="Please select a pet."
204+
errorMessage={
205+
selected === 'cats'
206+
? 'No cats allowed.'
207+
: 'Please select dogs.'
208+
}>
209+
<Radio value="dogs">
210+
Dogs
211+
</Radio>
212+
<Radio value="cats">
213+
Cats
214+
</Radio>
215+
<Radio value="dragons">
216+
Dragons
217+
</Radio>
218+
</RadioGroup>
219+
);
220+
}
221+
```
222+
188223
### Disabled
189224
[View guidelines](https://spectrum.adobe.com/page/radio-button/#Disabled)
190225

0 commit comments

Comments
 (0)