-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxCharacters.js
60 lines (49 loc) · 1.29 KB
/
maxCharacters.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
const template = document.createElement('template');
template.innerHTML = `
<style>
.max-characters {
color: #9cc8f0;
}
</style>
<div class="max-characters">
<small></small>
</div>
`;
class MaxCharacters extends HTMLElement {
constructor() {
super();
console.log('constructor, version 1.0.44');
this._maximum = '100';
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
static get observedAttributes() {
return ['maximum'];
}
attributeChangedCallback(name, oldValue, newValue) {
console.log(`${name}'s value has been changed from ${oldValue} to ${newValue}`);
switch (name) {
case 'maximum':
this._maximum = newValue;
this.render();
break;
}
}
get text() {
return this._text;
}
set text(value) {
this._text = value;
this.render();
}
connectedCallback() {
console.log('connectedCallback');
}
render() {
if (this._text) {
const msg = `(${this._text.length}/${this._maximum})`
this.shadowRoot.querySelector('small').innerText = msg;
}
}
}
customElements.define("max-characters", MaxCharacters);