generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbox.js
56 lines (46 loc) · 1.32 KB
/
checkbox.js
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
import { AegisInput } from './input.js';
import { SYMBOLS } from './consts.js';
const protectedData = new WeakMap();
function updateChecked(el) {
const internals = protectedData.get(el).internals;
el.value = el.getAttribute('value') ?? '';
if (el.checked) {
internals.ariaChecked = 'true';
} else {
internals.ariaChecked = 'false';
if (el.required) {
internals.setValidity({
valueMissing: true,
}, 'This input is required');
}
}
}
export class AegisCheckbox extends AegisInput {
constructor(opts = {}) {
super({ ...opts, role: 'checkbox' });
console.warn(new Error('Aegis Checkbox components are still in development and should not be used.'));
}
async [SYMBOLS.initialize](opts) {
const { internals } = await super[SYMBOLS.initialize](opts);
protectedData.set(this, { internals });
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'checked') {
updateChecked(this);
} else if (name === 'required') {
updateChecked(this);
super.attributeChangedCallback(name, oldValue, newValue);
} else {
super.attributeChangedCallback(name, oldValue, newValue);
}
}
get checked() {
return this.hasAttribute('checked');
}
set checked(val) {
this.toggleAttribute('checked', val);
}
static get observedAttributes() {
return [...AegisInput.observedAttributes, 'checked'];
}
}