Skip to content

Commit 4081561

Browse files
bjarnefiOvergaard
andauthored
docs(uui-checkbox): Indeterminate checkbox story (#860)
* Update story for checkbox indeterminate state * Adjust functions * Remove key * Adjust updating values * Update checked state * docs: add a container element to trigger state between checkboxes * docs: use the same UUIBooleanInputEvent as the checkbox element does * docs: use correct import path * docs: fix type error * docs: add description to the story --------- Co-authored-by: Jacob Overgaard <[email protected]>
1 parent d26635f commit 4081561

File tree

2 files changed

+132
-7
lines changed

2 files changed

+132
-7
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
2+
import { UUIBooleanInputEvent } from '@umbraco-ui/uui-boolean-input/lib';
3+
import { html, LitElement } from 'lit';
4+
import { property } from 'lit/decorators.js';
5+
import { repeat } from 'lit/directives/repeat.js';
6+
7+
import '@umbraco-ui/uui-checkbox/lib';
8+
9+
export type Option = {
10+
value: string;
11+
label: string;
12+
};
13+
14+
@defineElement('uui-checkbox-indeterminate-example')
15+
export default class UUICheckboxIndeterminateExample extends LitElement {
16+
@property()
17+
label = 'Indeterminate';
18+
19+
@property({ attribute: false })
20+
parent!: Option;
21+
22+
@property({ type: Array, attribute: false })
23+
options!: Option[];
24+
25+
@property({ type: Array })
26+
values: string[] = [];
27+
28+
private _handleParentChange(e: Event) {
29+
e.stopPropagation();
30+
const parent = e.target as HTMLInputElement;
31+
let values: string[] = [];
32+
if (parent.checked) {
33+
values = this.options.map(option => option.value);
34+
}
35+
this.values = values;
36+
this.dispatchEvent(new UUIBooleanInputEvent(UUIBooleanInputEvent.CHANGE));
37+
}
38+
39+
private _handleOptionChange(e: Event) {
40+
e.stopPropagation();
41+
const option = e.target as HTMLInputElement;
42+
let values = this.values;
43+
if (option.checked) {
44+
values = values.concat(option.value);
45+
} else {
46+
values = values.filter(v => v !== option.value);
47+
}
48+
this.values = values;
49+
this.dispatchEvent(new UUIBooleanInputEvent(UUIBooleanInputEvent.CHANGE));
50+
}
51+
52+
render() {
53+
return html`
54+
<fieldset name="Indeterminate" style="border: none;">
55+
<legend>${this.label}</legend>
56+
<uui-checkbox
57+
value=${this.parent.value}
58+
label=${this.parent.label}
59+
@change=${this._handleParentChange}
60+
name="indeterminate-parent"
61+
?indeterminate=${this.values.length > 0 &&
62+
this.values.length < this.options.length}
63+
?checked=${this.values.length === this.options.length}></uui-checkbox>
64+
<ul style="list-style: none; margin: 0;">
65+
${repeat(
66+
this.options,
67+
option => option.value,
68+
option =>
69+
html` <li>
70+
<uui-checkbox
71+
value=${option.value}
72+
label=${option.label}
73+
@change=${this._handleOptionChange}
74+
name="indeterminate-child"
75+
?checked=${this.values.includes(option.value)}></uui-checkbox>
76+
</li>`,
77+
)}
78+
</ul>
79+
</fieldset>
80+
`;
81+
}
82+
}
83+
84+
declare global {
85+
interface HTMLElementTagNameMap {
86+
'uui-checkbox-indeterminate-example': UUICheckboxIndeterminateExample;
87+
}
88+
}

packages/uui-checkbox/lib/uui-checkbox.story.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import '.';
2+
import './uui-checkbox-indeterminate.example.js';
23

34
import { StoryFn } from '@storybook/web-components';
5+
import { withActions } from '@storybook/addon-actions/decorator';
46
import { html } from 'lit';
57
import readme from '../README.md?raw';
68

@@ -31,7 +33,11 @@ export default {
3133
code: `<uui-checkbox label="Checkbox"></uui-checkbox>`,
3234
},
3335
},
36+
actions: {
37+
handles: ['change'],
38+
},
3439
},
40+
decorators: [withActions as any],
3541
};
3642

3743
export const AAAOverview: StoryFn = props => html`
@@ -177,15 +183,46 @@ Readonly.parameters = {
177183
},
178184
};
179185

180-
export const Indeterminate: Story = props => html`
181-
<uui-checkbox
182-
?indeterminate=${props.indeterminate}
183-
.label=${'Indeterminate'}></uui-checkbox>
184-
`;
185-
Indeterminate.args = { indeterminate: true };
186+
export const Indeterminate: StoryFn = props => {
187+
return html` <uui-checkbox-indeterminate-example
188+
.label=${props.label}
189+
.parent=${props.parent}
190+
.options=${props.options}
191+
.values=${props.values}></uui-checkbox-indeterminate-example>`;
192+
};
193+
194+
Indeterminate.args = {
195+
label: 'Choose your favorite fruits',
196+
values: ['mango'],
197+
parent: {
198+
label: 'All fruits',
199+
value: 'all',
200+
},
201+
options: [
202+
{
203+
label: 'Apple',
204+
value: 'apple',
205+
},
206+
{
207+
label: 'Banana',
208+
value: 'banana',
209+
},
210+
{
211+
label: 'Mango',
212+
value: 'mango',
213+
},
214+
],
215+
};
216+
186217
Indeterminate.parameters = {
187-
controls: { include: ['indeterminate'] },
218+
controls: {
219+
include: ['values', 'parent', 'options', 'label'],
220+
},
188221
docs: {
222+
description: {
223+
story:
224+
'A checkbox group with an indeterminate state. See the `UUICheckboxIndeterminateExample` component for more details.',
225+
},
189226
source: {
190227
code: `<uui-checkbox label="Indeterminate" indeterminate></uui-checkbox>`,
191228
},

0 commit comments

Comments
 (0)