Skip to content

Commit ba04e9c

Browse files
committed
chore(checkbox): update docs and stories to align checkbox help text handling w/other components
1 parent d278fc1 commit ba04e9c

File tree

5 files changed

+204
-13
lines changed

5 files changed

+204
-13
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ parameters:
2222
# 3. Commit this change to the PR branch where the changes exist.
2323
current_golden_images_hash:
2424
type: string
25-
default: 53d43cef8f05a527cd662707fd6a7a8e930a6a52
25+
default: 618dd949972712d04808ff96e90ba6127e3dffbc
2626

2727
wireit_cache_name:
2828
type: string

1st-gen/packages/checkbox/README.md

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,14 @@ focus to the content.
108108

109109
<div style="display: flex; flex-direction: column;">
110110
<h4 class="spectrum-Heading--subtitle1">Invalid</h4>
111-
<sp-checkbox invalid>Web component</sp-checkbox>
112-
<sp-checkbox checked invalid>Web component</sp-checkbox>
113-
<sp-checkbox indeterminate invalid>Web component</sp-checkbox>
111+
<sp-field-group vertical label="Select an option" invalid>
112+
<sp-checkbox invalid>Web component</sp-checkbox>
113+
<sp-checkbox checked invalid>Web component</sp-checkbox>
114+
<sp-checkbox indeterminate invalid>Web component</sp-checkbox>
115+
<sp-help-text slot="negative-help-text" icon>
116+
This selection is invalid.
117+
</sp-help-text>
118+
</sp-field-group>
114119
</div>
115120

116121
<div style="display: flex; flex-direction: column;">
@@ -141,11 +146,16 @@ of assets, etc. where the checkboxes need to be noticed.
141146

142147
<div style="display: flex; flex-direction: column;">
143148
<h4 class="spectrum-Heading--subtitle1">Invalid</h4>
144-
<sp-checkbox emphasized invalid>Web component</sp-checkbox>
145-
<sp-checkbox emphasized checked invalid>Web component</sp-checkbox>
146-
<sp-checkbox emphasized indeterminate invalid>
147-
Web component
148-
</sp-checkbox>
149+
<sp-field-group vertical label="Select an option" invalid>
150+
<sp-checkbox emphasized invalid>Web component</sp-checkbox>
151+
<sp-checkbox emphasized checked invalid>Web component</sp-checkbox>
152+
<sp-checkbox emphasized indeterminate invalid>
153+
Web component
154+
</sp-checkbox>
155+
<sp-help-text slot="negative-help-text" icon>
156+
This selection is invalid.
157+
</sp-help-text>
158+
</sp-field-group>
149159
</div>
150160

151161
<div style="display: flex; flex-direction: column;">
@@ -168,8 +178,15 @@ of assets, etc. where the checkboxes need to be noticed.
168178

169179
The `invalid` attribute indicates that the checkbox's value is invalid. When set, appropriate ARIA attributes will be automatically applied.
170180

181+
When a checkbox is in an invalid state, provide help text to explain the error and guide the user toward a solution. Wrap the checkbox in an `<sp-field-group>` to associate the help text with the checkbox. (See [help text](#help-text) for more information.)
182+
171183
```html
172-
<sp-checkbox invalid>Invalid</sp-checkbox>
184+
<sp-field-group vertical label="Terms and conditions" invalid>
185+
<sp-checkbox invalid>I accept the terms and conditions</sp-checkbox>
186+
<sp-help-text slot="negative-help-text" icon>
187+
You must accept the terms to continue.
188+
</sp-help-text>
189+
</sp-field-group>
173190
```
174191

175192
#### Disabled
@@ -196,6 +213,86 @@ Checkboxes have a `readonly` attribute for when they’re in the disabled state
196213
<sp-checkbox readonly>Read-only</sp-checkbox>
197214
```
198215

216+
### Help text
217+
218+
Help text can be accessibly associated with checkboxes by using the `help-text` or `negative-help-text` slots on an `<sp-field-group>` element. When using the `negative-help-text` slot, `<sp-field-group>` will self manage the presence of this content based on the value of the `invalid` property on your `<sp-field-group>` element. Content within the `help-text` slot will be shown by default. When your `<sp-field-group>` should receive help text based on state outside of the complexity of `invalid` or not, manage the content addressed to the `help-text` from above to ensure that it displays the right messaging and possesses the right `variant`.
219+
220+
Read more about using [help text](../help-text).
221+
222+
<sp-tabs selected="self" auto label="Help text usage with checkboxes">
223+
<sp-tab value="self">Self managed</sp-tab>
224+
<sp-tab-panel value="self">
225+
226+
```html
227+
<sp-field-group
228+
vertical
229+
id="self"
230+
label="Notification preferences"
231+
onchange="
232+
const checkboxes = this.querySelectorAll('sp-checkbox');
233+
const noneChecked = ![...checkboxes].some(cb => cb.checked);
234+
this.invalid = noneChecked;
235+
"
236+
>
237+
<sp-checkbox value="email">Email notifications</sp-checkbox>
238+
<sp-checkbox value="sms">SMS notifications</sp-checkbox>
239+
<sp-checkbox value="push" checked>Push notifications</sp-checkbox>
240+
<sp-help-text slot="help-text">
241+
Choose how you'd like to be notified.
242+
</sp-help-text>
243+
<sp-help-text slot="negative-help-text" icon>
244+
Select at least one notification method.
245+
</sp-help-text>
246+
</sp-field-group>
247+
```
248+
249+
</sp-tab-panel>
250+
<sp-tab value="above">Managed from above</sp-tab>
251+
<sp-tab-panel value="above">
252+
253+
```html
254+
<sp-field-label for="above">Notification preferences</sp-field-label>
255+
<sp-field-group
256+
vertical
257+
id="above"
258+
onchange="
259+
const checkboxes = this.querySelectorAll('sp-checkbox');
260+
const noneChecked = ![...checkboxes].some(cb => cb.checked);
261+
const helpText = this.querySelector(`[slot='help-text']`);
262+
helpText.icon = noneChecked;
263+
helpText.textContent = noneChecked ? 'Select at least one notification method.' : 'Choose how you\'d like to be notified.';
264+
helpText.variant = noneChecked ? 'negative' : 'neutral';
265+
"
266+
>
267+
<sp-checkbox value="email">Email notifications</sp-checkbox>
268+
<sp-checkbox value="sms">SMS notifications</sp-checkbox>
269+
<sp-checkbox value="push" checked>Push notifications</sp-checkbox>
270+
<sp-help-text slot="help-text">
271+
Choose how you'd like to be notified.
272+
</sp-help-text>
273+
</sp-field-group>
274+
```
275+
276+
</sp-tab-panel>
277+
<sp-tab value="single">Single checkbox</sp-tab>
278+
<sp-tab-panel value="single">
279+
280+
When a single checkbox requires validation, wrap it in an `<sp-field-group>` to associate help text:
281+
282+
```html
283+
<sp-field-group vertical label="Agreement" invalid>
284+
<sp-checkbox invalid>
285+
I have read and accept the terms of service
286+
</sp-checkbox>
287+
<sp-help-text slot="negative-help-text" icon>
288+
You must accept the terms of service to continue.
289+
</sp-help-text>
290+
</sp-field-group>
291+
```
292+
293+
</sp-tab-panel>
294+
</sp-tabs>
295+
199296
### Behaviors
200297

201298
#### Handling events
@@ -236,6 +333,14 @@ Sets of checkboxes should always have a clear label that describes what the list
236333
</sp-field-group>
237334
```
238335

336+
#### Provide help text in the correct location
337+
338+
Checkbox groups should use help text for error messaging and descriptions. Descriptions are valuable for giving context about a selection or for clarifying the options.
339+
340+
It is [not currently possible](https://w3c.github.io/webcomponents-cg/#cross-root-aria) to provide accessible ARIA references between elements in different shadow roots. To ensure proper association between elements, help text must be included via the `slot="help-text"` or `slot="negative-help-text"` on the parent `<sp-field-group>`.
341+
342+
See [help text](../help-text) for more information.
343+
239344
#### Keyboard Navigation
240345

241346
Checkboxes can be toggled using the <kbd>Space</kbd> key when focused. They follow the standard tab order of the page.

1st-gen/packages/checkbox/stories/checkbox.stories.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
*/
1212
import '@spectrum-web-components/checkbox/sp-checkbox.js';
1313
import '@spectrum-web-components/field-group/sp-field-group.js';
14+
import '@spectrum-web-components/help-text/sp-help-text.js';
1415
import { html, TemplateResult } from '@spectrum-web-components/base';
16+
import type { Checkbox } from '@spectrum-web-components/checkbox';
1517

1618
export default {
1719
component: 'sp-checkbox',
@@ -167,3 +169,56 @@ export const verticalTabIndexExample = (): TemplateResult => {
167169
</sp-field-group>
168170
`;
169171
};
172+
173+
export const invalidWithHelpText = (): TemplateResult => {
174+
return html`
175+
<sp-field-group vertical label="Required selections" invalid>
176+
<sp-checkbox invalid>Option A</sp-checkbox>
177+
<sp-checkbox invalid>Option B</sp-checkbox>
178+
<sp-checkbox invalid>Option C</sp-checkbox>
179+
<sp-help-text slot="negative-help-text" icon>
180+
Select at least one option to continue.
181+
</sp-help-text>
182+
</sp-field-group>
183+
`;
184+
};
185+
186+
export const invalidSingleCheckboxWithHelpText = (): TemplateResult => {
187+
return html`
188+
<sp-field-group vertical label="Agreement" invalid>
189+
<sp-checkbox invalid>
190+
I have read and accept the terms of service
191+
</sp-checkbox>
192+
<sp-help-text slot="negative-help-text" icon>
193+
You must accept the terms of service to continue.
194+
</sp-help-text>
195+
</sp-field-group>
196+
`;
197+
};
198+
199+
export const helpTextSelfManaged = (): TemplateResult => {
200+
return html`
201+
<sp-field-group
202+
vertical
203+
label="Notification preferences"
204+
@change=${(event: Event) => {
205+
const fieldGroup = event.currentTarget as HTMLElement;
206+
const checkboxes = fieldGroup.querySelectorAll('sp-checkbox');
207+
const noneChecked = ![...checkboxes].some(
208+
(cb) => (cb as Checkbox).checked
209+
);
210+
fieldGroup.toggleAttribute('invalid', noneChecked);
211+
}}
212+
>
213+
<sp-checkbox value="email">Email notifications</sp-checkbox>
214+
<sp-checkbox value="sms">SMS notifications</sp-checkbox>
215+
<sp-checkbox value="push" checked>Push notifications</sp-checkbox>
216+
<sp-help-text slot="help-text">
217+
Choose how you'd like to be notified.
218+
</sp-help-text>
219+
<sp-help-text slot="negative-help-text" icon>
220+
Select at least one notification method.
221+
</sp-help-text>
222+
</sp-field-group>
223+
`;
224+
};

1st-gen/packages/field-group/README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ Help text can be accessibly associated with an `<sp-field-group>` element by usi
121121
</sp-field-group>
122122
```
123123

124+
### States
125+
126+
#### Invalid
127+
128+
When a group of checkboxes fails validation, use the `invalid` attribute on the field group along with `negative-help-text` to explain the error. Set the `invalid` attribute on individual checkboxes as well to apply the appropriate visual styling.
129+
130+
```html
131+
<sp-field-group vertical label="Required selections" invalid>
132+
<sp-checkbox invalid>Option A</sp-checkbox>
133+
<sp-checkbox invalid>Option B</sp-checkbox>
134+
<sp-checkbox invalid>Option C</sp-checkbox>
135+
<sp-help-text slot="negative-help-text" icon>
136+
Select at least one option to continue.
137+
</sp-help-text>
138+
</sp-field-group>
139+
```
140+
124141
### Accessibility
125142

126143
#### Include a label
@@ -131,9 +148,9 @@ Every field group should have a label. A field without a label is ambiguous and
131148

132149
The description in the help text is flexible and encompasses a range of guidance. Sometimes this guidance is about what to input, and sometime it’s about how to input. This includes information such as:
133150

134-
- An overall description of the input field
135-
- Hints for what kind of information needs to be input
136-
- Specific formatting examples or requirements
151+
- An overall description of the input field
152+
- Hints for what kind of information needs to be input
153+
- Specific formatting examples or requirements
137154

138155
Learn more about [using help text](https://spectrum.adobe.com/page/text-field/#Use-help-text-to-show-hints,-formatting,-and-requirements).
139156

1st-gen/packages/field-group/stories/field-group.stories.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { html, TemplateResult } from '@spectrum-web-components/base';
1515
import '@spectrum-web-components/field-group/sp-field-group.js';
1616
import '@spectrum-web-components/checkbox/sp-checkbox.js';
1717
import '@spectrum-web-components/radio/sp-radio.js';
18+
import '@spectrum-web-components/help-text/sp-help-text.js';
1819

1920
export default {
2021
title: 'Field Group',
@@ -44,3 +45,16 @@ export const vertical = (): TemplateResult => {
4445
</sp-field-group>
4546
`;
4647
};
48+
49+
export const invalid = (): TemplateResult => {
50+
return html`
51+
<sp-field-group vertical label="Required selections" invalid>
52+
<sp-checkbox invalid>Option A</sp-checkbox>
53+
<sp-checkbox invalid>Option B</sp-checkbox>
54+
<sp-checkbox invalid>Option C</sp-checkbox>
55+
<sp-help-text slot="negative-help-text" icon>
56+
Select at least one option to continue.
57+
</sp-help-text>
58+
</sp-field-group>
59+
`;
60+
};

0 commit comments

Comments
 (0)