-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpie-switch.stories.ts
More file actions
304 lines (276 loc) · 9.44 KB
/
Copy pathpie-switch.stories.ts
File metadata and controls
304 lines (276 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { html, nothing } from 'lit';
import { action } from 'storybook/actions';
import { type Meta } from '@storybook/web-components';
import '@justeattakeaway/pie-webc/components/switch';
import '@justeattakeaway/pie-webc/components/button';
import { type SwitchProps, labelPlacements, defaultProps } from '@justeattakeaway/pie-webc/components/switch';
import '@justeattakeaway/pie-icons-webc/dist/IconCheck.js';
import { createStory, type TemplateFunction } from '../utilities';
type SwitchStoryMeta = Meta<SwitchProps>;
const defaultArgs: SwitchProps = {
...defaultProps,
label: 'Label',
name: 'switch',
value: 'switchValue',
};
const switchStoryMeta: SwitchStoryMeta = {
title: 'Components/Switch',
component: 'pie-switch',
argTypes: {
checked: {
description: 'Same as the HTML checked attribute - indicates whether the switch is on or off',
control: 'boolean',
defaultValue: {
summary: defaultProps.checked,
},
},
defaultChecked: {
description: 'The default checked state of the switch (not necessarily the same as the current checked state). Used when the switch is part of a form that is reset.',
control: 'boolean',
defaultValue: {
summary: defaultProps.defaultChecked,
},
},
disabled: {
description: 'Same as the HTML disabled attribute - indicates whether the switch is disabled or not',
control: 'boolean',
defaultValue: {
summary: defaultProps.disabled,
},
},
label: {
description: 'The label text for the switch',
control: {
type: 'text',
defaultValue: {
summary: 'Label',
},
},
},
labelPlacement: {
description: 'Set the placement of the label.',
control: 'select',
if: { arg: 'label', truthy: true },
options: labelPlacements,
defaultValue: {
summary: defaultProps.labelPlacement,
},
},
aria: {
description: 'The ARIA labels used for the switch.',
control: 'object',
},
name: {
description: 'Same as the HTML name attribute - indicates the name of the switch (for use with forms)',
control: {
type: 'text',
defaultValue: {
summary: 'switch',
},
},
},
value: {
description: 'Same as the HTML value attribute - indicates the value of the switch (for use with forms). Defaults to "on".',
control: {
type: 'text',
defaultValue: {
summary: defaultProps.value,
},
},
},
required: {
description: 'Same as the HTML required attribute - for use in forms',
control: 'boolean',
defaultValue: {
summary: defaultProps.required,
},
},
},
args: defaultArgs,
parameters: {
design: {
type: 'figma',
url: '',
},
},
};
export default switchStoryMeta;
const changeAction = (event: Event) => action('change')({
checked: (event.target as HTMLInputElement).checked,
});
const Template : TemplateFunction<SwitchProps> = (props) => {
const {
aria,
checked,
defaultChecked,
disabled,
label,
labelPlacement,
name,
value,
required,
} = props;
return html`
<pie-switch
id="pie-switch"
name="${name || nothing}"
value="${value || nothing}"
label="${label || nothing}"
labelPlacement="${label && labelPlacement ? labelPlacement : nothing}"
.aria="${aria}"
?required="${required}"
?checked="${checked}"
?defaultChecked="${defaultChecked}"
?disabled="${disabled}"
@change="${changeAction}">
</pie-switch>`;
};
const FormTemplate: TemplateFunction<SwitchProps> = (props: SwitchProps) => html`
<p id="formLog" style="display: none; font-size: 2rem; color: var(--dt-color-support-positive);"></p>
<h2>Example form with a single switch</h2>
<form id="testForm">
<p>A single switch inside a form.</p>
<div style="display: flex; flex-direction: column; gap: 1rem;">
${Template({
...props,
})}
</div>
<br />
<pie-button type="submit">Submit</pie-button>
</form>
<script>
// Prevent storybook from erroring when the script is re-run
const form = document.querySelector('#testForm');
const formLog = document.querySelector('#formLog');
form.addEventListener('submit', (e) => {
e.preventDefault();
// Display a success message to the user when they submit the form
formLog.innerHTML = 'Form submitted!';
formLog.style.display = 'block';
// log out all form input values
const formData = new FormData(form);
console.log('All form elements:', form.elements);
console.log('All form element data keys and values submitted:');
for (const entry of formData.entries()) {
console.table(entry);
}
// Reset the success message after roughly 8 seconds
setTimeout(() => {
formLog.innerHTML = '';
formLog.style.display = 'none';
}, 8000);
});
</script>
`;
const MultiSwitchFormTemplate: TemplateFunction<SwitchProps> = (props: SwitchProps) => html`
<p id="formLog" style="display: none; font-size: 2rem; color: var(--dt-color-support-positive);"></p>
<h2>Example form with multiple switches</h2>
<form id="testForm">
<p>Use the <strong>Tab</strong> key to navigate between the five switches below (one is disabled and should be skipped).</p>
<div style="display: flex; flex-direction: column; gap: 1rem;">
${Template({
...props,
label: 'Bacon',
name: 'bacon',
aria: {},
value: 'bacon_yes',
checked: true,
disabled: false,
required: false,
})}
${Template({
...props,
label: 'Lettuce',
name: 'lettuce',
aria: {},
value: 'lettuce_yes',
checked: true,
disabled: true,
})}
${Template({
...props,
label: 'Tomato',
name: 'tomato',
aria: {},
value: 'tomato_yes',
checked: false,
disabled: false,
})}
${Template({
...props,
label: 'Cheese',
name: 'cheese',
aria: {},
value: 'cheese_yes',
checked: true,
disabled: false,
})}
${Template({
...props,
label: 'Onion',
name: 'onion',
aria: {
label: 'a custom label for the onion switch',
},
value: 'onion_yes',
checked: false,
disabled: false,
})}
</div>
<br />
<pie-button type="submit">Submit</pie-button>
</form>
<script>
// Prevent storybook from adding duplicate listeners on hot reloads
const form = document.querySelector('#testForm');
if (form && !form.dataset.listening) {
form.dataset.listening = 'true';
form.addEventListener('submit', (e) => {
e.preventDefault();
const formLog = document.querySelector('#formLog');
// Display a success message to the user when they submit the form
formLog.innerHTML = 'Form submitted!';
formLog.style.display = 'block';
// log out all form input values
const formData = new FormData(form);
console.log('All form elements:', form.elements);
console.log('All form element data keys and values submitted:');
for (const entry of formData.entries()) {
console.table(entry);
}
// Reset the success message after roughly 8 seconds
setTimeout(() => {
formLog.innerHTML = '';
formLog.style.display = 'none';
}, 8000);
});
}
</script>
`;
const createSwitchStory = createStory(Template, defaultArgs);
const createSwitchStoryWithForm = createStory<SwitchProps>(FormTemplate, defaultArgs);
const formIntegrationOnly = {
description: 'This prop is only used when the switch is used inside a form. You can try it out in the Form Integration story.',
table: {
readonly: true,
},
};
export const Default = createSwitchStory({}, {
argTypes: {
name: formIntegrationOnly,
required: formIntegrationOnly,
value: formIntegrationOnly,
},
});
export const FormIntegration = createSwitchStoryWithForm({ label: 'Click me' });
const createMultiSwitchStory = createStory(MultiSwitchFormTemplate, {
...defaultArgs,
// The label is set within the template for each switch, so we can clear the global one
label: '',
});
// Generate the argTypes config to disable all controls
const controlArgNames = Object.keys(switchStoryMeta.argTypes || {});
const disabledArgTypes = Object.fromEntries(controlArgNames.map((key) => [key, { control: { type: null } }]));
export const MultipleSwitchesInAForm = createMultiSwitchStory({}, {
argTypes: disabledArgTypes,
});