Repository navigation
Expand file tree
/
Copy pathCheckboxGroup.js
More file actions
393 lines (326 loc) · 13.6 KB
/
Copy pathCheckboxGroup.js
File metadata and controls
393 lines (326 loc) · 13.6 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/** @import {ExposedInternals, FieldPropertiesAndMethods, FieldMinMax} from "../types/helpers.js" */
/** @implements {ExposedInternals} @implements {FieldPropertiesAndMethods} @implements {FieldMinMax} */
class CheckboxGroup extends HTMLElement {
/* ------------------------------ Custom Element Settings ------------------------------ */
/** @returns {true} */
static get formAssociated() {
return true;
}
static get observedAttributes() {
return /** @type {const} */ ([
"required",
"valuemissingerror",
"min",
"rangeunderflowerror",
"max",
"rangeoverflowerror",
]);
}
/** @readonly */ #internals = this.attachInternals();
/* ------------------------------ Lifecycle Callbacks ------------------------------ */
constructor() {
super();
// NOTE: For Playwright and Browser Compatibility purposes, we'll still need to set the `role` attribute explicitly.
this.#internals.role = "group";
// NOTE: We are intentionally adding these here to monitor behavior of elements not connected to the DOM.
this.addEventListener("click", CheckboxGroup.#handleClick);
new MutationObserver(this.#watchChildren.bind(this)).observe(this, { childList: true, subtree: true });
}
/**
* @param {typeof CheckboxGroup.observedAttributes[number]} _name
* @param {string | null} _oldValue
* @param {string | null} _newValue
* @returns {void}
*/
attributeChangedCallback(_name, _oldValue, _newValue) {
// NOTE: If we're only watching constraint-related attributes, then we only need to re-run validation here.
if (!this.fieldset) return; // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- Check needed here.
this.#validateConstraints();
}
/** "On Mount" for Custom Elements @returns {void} */
connectedCallback() {
if (!this.isConnected) return;
if (!(this.lastElementChild instanceof HTMLFieldSetElement) || this.firstElementChild !== this.lastElementChild) {
throw new TypeError("A <fieldset> element must be the only direct descendant of the `CheckboxGroup`.");
}
// Transfer `<fieldset>` attributes to `CheckboxGroup`
const { fieldset } = this;
const attributeNames = fieldset.getAttributeNames();
for (let i = 0; i < attributeNames.length; i++) {
const attrName = attributeNames[i];
this.setAttribute(attrName, /** @type {string} */ (fieldset.getAttribute(attrName)));
fieldset.removeAttribute(attrName);
}
// Initialize essential attributes
this.id ||= Math.random().toString(36).slice(2);
this.setAttribute("role", "group");
this.lastElementChild.setAttribute("role", "none");
// Transfer `<fieldset>`'s label if it exists
const legend = fieldset.firstElementChild;
if (legend instanceof HTMLLegendElement && !this.manual) {
const label = document.createElement("label");
label.htmlFor = this.id;
label.append(...legend.childNodes);
legend.replaceWith(label);
}
// Initialize checkbox data
const { elements } = this.fieldset;
for (let i = 0; i < elements.length; i++) {
const checkbox = elements[i];
// TODO: Document that, at least for now, nesting `checkbox` groups is not suppoted
if (!(checkbox instanceof HTMLInputElement) || checkbox.type !== "checkbox") {
throw new TypeError("`checkbox`es are the only form controls allowed inside the `CheckboxGroup`'s <fieldset>");
}
// TODO: Document the auto-`name`-ing behavior. It's worth being aware of.
this.#value[checkbox.checked ? "add" : "delete"](checkbox.value);
if (i === 0 && checkbox.name) this.name = checkbox.name;
checkbox.removeAttribute("name");
checkbox.setAttribute("form", "");
}
this.#updateFormValue();
this.#validateConstraints();
}
/* ------------------------------ Exposed Form Properties ------------------------------ */
/** @type {Set<string>} */ #value = new Set();
// TODO: Document that for Data Integrity purposes, this getter always creates a new `Array`. So devs should be mindful.
/** Sets or retrieves the `value` of the `CheckboxGroup` @returns {string[]} */
get value() {
return Array.from(this.#value);
}
// TODO: Document why devs should be mindful of setting `value` as well.
set value(v) {
this.#value.clear();
const checkboxes = /** @type {HTMLCollectionOf<HTMLInputElement>} */ (this.fieldset.elements);
for (let i = 0; i < this.fieldset.elements.length; i++) {
const checkbox = checkboxes[i];
checkbox.checked = v.findIndex((V) => String(V) === checkbox.value) !== -1;
if (checkbox.checked) this.#value.add(checkbox.value);
}
this.#updateFormValue();
this.#validateConstraints();
}
/** Synchronizes the component's form value with its internal value @returns {void} */
#updateFormValue() {
if (!this.#value.size) return this.#internals.setFormValue(null);
const formData = new FormData();
this.#value.forEach((v) => formData.append(this.name, v));
this.#internals.setFormValue(formData);
}
/** The singular `<fieldset>` owned by this checkbox group @returns {HTMLFieldSetElement} */
get fieldset() {
return /** @type {HTMLFieldSetElement} */ (this.lastElementChild);
}
/** @returns {HTMLInputElement["name"]} */
get name() {
return this.getAttribute("name") ?? "";
}
set name(value) {
this.setAttribute("name", value);
}
/** @returns {HTMLInputElement["disabled"]} */
get disabled() {
return this.hasAttribute("disabled");
}
set disabled(value) {
this.toggleAttribute("disabled", value);
}
/** @returns {HTMLInputElement["required"]} */
get required() {
return this.hasAttribute("required");
}
set required(value) {
this.toggleAttribute("required", value);
}
/** @returns {HTMLInputElement["min"]} */
get min() {
return this.getAttribute("min") ?? "";
}
set min(value) {
this.setAttribute("min", value);
}
/** @returns {HTMLInputElement["max"]} */
get max() {
return this.getAttribute("max") ?? "";
}
set max(value) {
this.setAttribute("max", value);
}
/**
* Indicates that the `CheckboxGroup` should not replace its `<fieldset>`'s accessible label (`<legend>`)
* with its own (`<label>`) when mounted.
*
* **WARNING**: Only enable this property if you intend to perform the label replacement yourself.
* @returns {boolean}
*/
get manual() {
return this.hasAttribute("manual");
}
set manual(value) {
this.toggleAttribute("manual", value);
}
/* ------------------------------ Custom Attributes and Properties ------------------------------ */
/** The error message displayed to users when the group's `required` constraint is broken. @returns {string} */
get valueMissingError() {
return this.getAttribute("valuemissingerror") ?? "Please select one or more items.";
}
set valueMissingError(value) {
this.setAttribute("valuemissingerror", value);
}
/** The error message displayed to users when the group's `min` constraint is broken. @returns {string} */
get rangeUnderflowError() {
const items = Number(this.min) === 1 ? "item" : "items";
return this.getAttribute("rangeunderflowerror") ?? `Please select at least ${this.min} ${items}.`;
}
set rangeUnderflowError(value) {
this.setAttribute("rangeunderflowerror", value);
}
/** The error message displayed to users when the group's `max` constraint is broken. @returns {string} */
get rangeOverflowError() {
const items = Number(this.max) === 1 ? "item" : "items";
return this.getAttribute("rangeoverflowerror") ?? `Please select no more than ${this.max} ${items}.`;
}
set rangeOverflowError(value) {
this.setAttribute("rangeoverflowerror", value);
}
/* ------------------------------ Exposed `ElementInternals` ------------------------------ */
/** @returns {ElementInternals["labels"]} */
get labels() {
return this.#internals.labels;
}
/** @returns {ElementInternals["form"]} */
get form() {
return this.#internals.form;
}
/** @returns {ElementInternals["validity"]} */
get validity() {
return this.#internals.validity;
}
/** @returns {ElementInternals["validationMessage"]} */
get validationMessage() {
return this.#internals.validationMessage;
}
/** @returns {ElementInternals["willValidate"]} */
get willValidate() {
return this.#internals.willValidate;
}
/** @type {ElementInternals["checkValidity"]} */
checkValidity() {
return this.#internals.checkValidity();
}
/** @type {ElementInternals["reportValidity"]} */
reportValidity() {
return this.#internals.reportValidity();
}
/** @type {HTMLInputElement["setCustomValidity"]} */
setCustomValidity(error) {
this.#customError = error;
this.#validateConstraints();
}
#customError = "";
/** @returns {void} */
#validateConstraints() {
// TODO: Document that `radio`s should be used if `min` === `max` (a11y).
const min = Number(this.min);
const max = Number(this.max);
const count = this.#value.size;
// TODO: Document that users should provide `min/max` values that make sense. `min > max` is not logical and will hurt UX.
// TODO: Also document that `min` might be preferable to `required` since it's more flexible.
const valueMissing = this.required && !count;
const rangeUnderflow = this.hasAttribute("min") && !Number.isNaN(min) && count < min;
const rangeOverflow = this.hasAttribute("max") && !Number.isNaN(max) && count > max;
const customError = Boolean(this.#customError);
/** @type {string | undefined} */ let message;
if (customError) message = this.#customError;
else if (rangeUnderflow) message = this.rangeUnderflowError;
else if (valueMissing) message = this.valueMissingError;
else if (rangeOverflow) message = this.rangeOverflowError;
const anchor = message ? /** @type {HTMLInputElement} */ (this.fieldset.elements[0]) : undefined;
this.#internals.setValidity({ valueMissing, rangeUnderflow, rangeOverflow, customError }, message, anchor);
}
/* ------------------------------ Form Control Callbacks ------------------------------ */
/** @returns {void} */
formResetCallback() {
this.#value.clear(); // NOTE: This is done to guarantee consistent ordering during `reset`s (nice-to-have)
const checkboxes = /** @type {HTMLCollectionOf<HTMLInputElement>} */ (this.fieldset.elements);
for (let i = 0; i < checkboxes.length; i++) {
const checkbox = checkboxes[i];
checkbox.checked = checkbox.defaultChecked;
if (checkbox.checked) this.#value.add(checkbox.value);
}
this.#updateFormValue();
}
/**
* @param {FormData | null} state
* @param {"restore" | "autocomplete"} _mode
* @returns {void}
*/
formStateRestoreCallback(state, _mode) {
this.#value = new Set(/** @type {FormDataIterator<string> | undefined} */ (state?.values()));
this.#updateFormValue();
}
/**
* @param {boolean} disabled
* @returns {void}
*/
formDisabledCallback(disabled) {
this.fieldset.disabled = disabled;
}
/* ------------------------------ Event Handlers + Mutation Observers ------------------------------ */
/**
* Monitors the clicks on individual `checkbox`es, updating the {@link CheckboxGroup}'s value as needed.
* @param {MouseEvent} event
* @returns {void}
*/
static #handleClick(event) {
const input = event.target;
const checkboxGroup = /** @type {CheckboxGroup} */ (event.currentTarget);
if (!event.isTrusted || event.defaultPrevented || !(input instanceof HTMLInputElement)) return;
event.preventDefault(); // Prevent developers from receiving irrelevant `input`/`change` events
const { checked } = input;
setTimeout(() => {
input.checked = checked;
checkboxGroup.#value[checked ? "add" : "delete"](input.value);
checkboxGroup.#updateFormValue();
checkboxGroup.#validateConstraints();
checkboxGroup.dispatchEvent(new Event("input", { bubbles: true, composed: true, cancelable: false }));
checkboxGroup.dispatchEvent(new Event("change", { bubbles: true, composed: false, cancelable: false }));
});
}
/**
* Monitors the `checkbox`es that are added to or removed from the {@link CheckboxGroup}, updating its value as needed.
* @param {MutationRecord[]} mutations
* @returns {void}
*/
#watchChildren(mutations) {
if (!(this.lastElementChild instanceof HTMLFieldSetElement) || this.firstElementChild !== this.lastElementChild) {
throw new TypeError("A <fieldset> element must be the only direct descendant of the `CheckboxGroup`.");
}
for (let i = 0; i < mutations.length; i++) {
const { addedNodes, removedNodes } = mutations[i];
let workingWithRemovedNodes = Boolean(removedNodes.length);
let nodes = workingWithRemovedNodes ? removedNodes : addedNodes;
for (let j = 0; j < nodes.length; j++) {
const node = nodes[j];
if (!(node instanceof HTMLElement)) continue;
/** @type {ArrayLike<HTMLInputElement>} */
const checkboxes = node instanceof HTMLInputElement ? [node] : node.querySelectorAll(":scope input");
for (let k = 0; k < checkboxes.length; k++) {
const checkbox = checkboxes[k];
if (checkbox.checked) this.#value[workingWithRemovedNodes ? "delete" : "add"](checkbox.value);
if (!workingWithRemovedNodes) {
checkbox.removeAttribute("name");
checkbox.setAttribute("form", "");
}
}
if (workingWithRemovedNodes && j === removedNodes.length - 1) {
nodes = addedNodes;
workingWithRemovedNodes = false;
j = -1;
}
}
}
this.#updateFormValue();
this.#validateConstraints();
}
}
export default CheckboxGroup;