This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecked-mixin.js
118 lines (98 loc) · 2.49 KB
/
checked-mixin.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
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
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
import { InputMixin } from './input-mixin.js';
const CheckedMixinImplementation = (superclass) =>
class CheckedMixinClass extends InputMixin(superclass) {
static get properties() {
return {
/**
* True if the element is checked.
* @type {boolean}
*/
checked: {
type: Boolean,
value: false,
notify: true,
observer: '_checkedChanged',
reflectToAttribute: true
},
/**
* Name of the element.
*/
name: String,
/**
* Value of the element.
*/
value: String
};
}
get name() {
return this.checked ? (this._inputNode && this._inputNode.name) || this.__name || '' : '';
}
set name(value) {
if (this._inputNode) {
this._inputNode.name = value;
this.__name = undefined;
} else {
this.__name = value;
}
}
get value() {
return (this._inputNode && this._inputNode.value) || this.__value || '';
}
set value(value) {
if (this._inputNode) {
this._inputNode.value = value;
this.__value = undefined;
} else {
this.__value = value;
}
}
constructor() {
super();
this._handleClick = this._handleClick.bind(this);
}
/** @protected */
connectedCallback() {
super.connectedCallback();
this.addEventListener('click', this._handleClick);
}
/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('click', this._handleClick);
}
/** @private */
_checkedChanged(checked) {
if (checked && this._inputNode) {
this._inputNode.checked = checked;
}
}
/** @private */
__interactionsAllowed(e) {
if (this.disabled) {
return false;
}
if (e.target.localName === 'a') {
return false;
}
return true;
}
/** @private */
_handleClick(e) {
if (this.__interactionsAllowed(e)) {
this._toggleChecked();
} else {
e.preventDefault();
}
}
/** @protected */
_toggleChecked() {
this.checked = !this.checked;
this._toggleAriaChecked();
}
/** @protected */
_toggleAriaChecked() {
this.setAttribute('aria-checked', Boolean(this.checked));
}
};
export const CheckedMixin = dedupingMixin(CheckedMixinImplementation);