generated from mirisuzanne/web-component-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
input-control-raw.js
72 lines (56 loc) · 1.49 KB
/
input-control-raw.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class InputControl extends GroundControl {
static register(tagName) {
if ('customElements' in window) {
customElements.define(
tagName || 'input-control',
InputControl
);
}
}
static rejectInputTypes = [
'file',
'submit', 'reset', 'button', 'image',
'checkbox', 'radio'
];
#input;
get listenFor () {
return this.dataset.event || 'change';
}
constructor() {
super();
GroundControl.blockDisplay(this);
}
connectedCallback() {
super.connectedCallback();
this.#input = this.#findInput();
if (!this.#input) {
console.error('No input found for input-control');
return;
}
this.hasInput = true;
if (this.#input.id) this.inputId = this.#input.id;
this.initialValue = this.#input.value;
this.value = this.storedValue || this.initialValue;
this.addEventListener(this.listenFor, this.onInputChange);
}
disconnectedCallback() {
this.removeEventListener(this.listenFor, this.onInputChange);
}
onInputChange = () => {
this.value = this.#input.value;
}
onValueChange = () => {
if (this.#input.value === this.value) return;
this.#input.value = this.value;
}
#findInput = () => {
const inputSelect = InputControl.rejectInputTypes.reduce(
(all, type) => {
const item = `[type=${type}]`;
return all ? `${all}, ${item}` : item;
}
);
return this.querySelector(`select, input:not(${inputSelect})`);
}
}
InputControl.register();