-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
288 lines (263 loc) · 8.64 KB
/
index.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
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
import { html } from 'lit';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { classMap } from 'lit/directives/class-map.js';
import { defineVersioned } from '../../../utils/component-versioning';
import NoShadowDOM from '../../../utils/no-shadow';
import applyDefaults from '../../../utils/apply-defaults';
import styles from './index.scss';
import createRefId from '../../../utils/create-ref-id';
// icon isolated from others, because it's a component specific icon
import CheckmarkHardEdgesSvg from './icon';
const CHECKMARK_ICON = unsafeHTML(CheckmarkHardEdgesSvg);
const REQUIRED_SYMBOL = '*';
class AXACheckbox extends NoShadowDOM {
static get tagName() {
return 'axa-checkbox';
}
static get properties() {
return {
refId: { type: String, defaultValue: `checkbox-${createRefId()}` },
value: { type: String },
name: { type: String, reflect: true },
label: { type: String },
styled: { type: Boolean, reflect: true },
variant: { type: String, defaultValue: 'square' },
required: { type: Boolean },
checked: {
type: Boolean,
reflect: true,
defaultValue: undefined, // proper default for controlled-mode under React
},
defaultChecked: {
type: Boolean,
},
disabled: { type: Boolean, reflect: true },
error: {
type: String,
reflect: true,
converter: {
toAttribute(value) {
return value ? String(value) : null;
},
},
},
onChange: { type: Function, attribute: false },
onBlur: { type: Function, attribute: false },
onFocus: { type: Function, attribute: false },
isReact: { type: Boolean },
};
}
static get styles() {
return styles;
}
constructor() {
super();
// initialize model state
this.state = {
isControlled: false,
firstUpdate: true,
timer: null,
checked: false,
native: false,
};
// detect absence of touch capabilities (to condition :hover styles)
if (!('ontouchstart' in document.documentElement)) {
this._noTouch = true;
}
// initialize properties
applyDefaults(this);
}
// custom setter
set checked(value) {
const {
state: { isControlled, checked, firstUpdate },
} = this;
// first incoming value indicates controlledness?
if (!isControlled && value !== undefined && firstUpdate) {
// yes, remember in model state
this.state.isControlled = true;
}
this.state.firstUpdate = false;
const oldValue = checked;
// remember value in model state
this.state.checked = value;
this.state.native = value;
// request re-render (custom setters need to do this themselves!)
this.requestUpdate('checked', oldValue);
}
// custom getter
get checked() {
const {
state: { isControlled, checked, native },
} = this;
return isControlled ? checked : native;
}
handleChange(event) {
// initialize
const { onChange } = this;
// *schedule* a UI update from model state in the near future
// (if no changed prop value is seen, triggering re-rendering,
// that scheduled update actually happens!)
this.state.timer = setTimeout(() => this.updated('via-handleChange'), 0);
// set *uncontrolled* model state from native checkbox behaviour
this.state.native = event.target.checked;
// invoke event callback
onChange(event);
}
handleSpacebar(e) {
// Key: Space
if (e.keyCode === 32) {
this.checked = !this.checked;
this.state.native = this.checked;
}
}
// throttle re-rendering to once per animation frame
// (helps e.g. with static websites using this component together with static children
// (like <axa-checkbox><span>my label</span></axa-checkbox>),
// where those children (here, <span>) may not be DOM-constructed yet when using lit-element's default microtask timing!)
performUpdate() {
new Promise(resolve =>
// eslint-disable-next-line no-promise-executor-return
window.requestAnimationFrame(() => resolve())
).then(() => super.performUpdate());
}
render() {
// extract props and state
const {
refId,
value,
name,
label = '',
variant,
checked,
disabled,
error = '',
required,
isReact,
state: { isControlled, timer },
_childRoot = this.firstElementChild,
} = this;
const isVariantInverted = variant.includes('inverted');
const isVariantCheckmark = variant.includes('checkmark');
const classes = classMap({
'a-checkbox__icon': true,
'js-checkbox__icon': true,
'a-checkbox__icon--checkmark': isVariantCheckmark,
'a-checkbox__icon--inverted': isVariantInverted,
});
const checkboxContentClasses = classMap({
'a-checkbox__content': true,
'a-checkbox__content--inverted': isVariantInverted,
});
// now that we have the 'isReact' prop, determine if this
// component is a 'controlled input' in the *React* sense
const _isControlled = isControlled && isReact;
this.state.isControlled = _isControlled;
if (_isControlled) {
// cancel any scheduled UI update, since there is a real,
// changed prop value somewhere (likely 'checked')
clearTimeout(timer);
}
const inputElements = html`
<input
id="${refId}"
class="a-checkbox__input"
type="checkbox"
name="${name}"
value="${value}"
tabindex="-1"
aria-required="${required}"
?checked="${checked}"
?disabled="${disabled}"
?error="${!!error}"
@focus="${this.onFocus}"
@blur="${this.onBlur}"
@change=${this.handleChange}
/>
<span class="${classes}">
${isVariantCheckmark
? html`
<span class="a-checkbox__icon-checkmark">${CHECKMARK_ICON}</span>
`
: ``}
</span>
`;
const errorElement =
error && !disabled
? html` <span class="a-checkbox__error">${unsafeHTML(error)}</span> `
: html``;
if (_childRoot) {
// 1. harvest child content as live DOM (lit-html has documented support for text-content binding type 'DOM node')
// N.B. Using live DOM node here is crucial for React's DOM updating mechanism, e.g. for a <p>{updatedHere}<p> child root.
// 2. wrap it in <p>(aragraph tag), but with identical styling to <axa-text size="3">
// prettier-ignore
this._label = html`<p class="a-checkbox__children-inline">${_childRoot}</p>`;
}
const renderLabel = this._label || (label ? unsafeHTML(label) : null);
return renderLabel
? html`
<label
for="${refId}"
class="a-checkbox__wrapper"
tabindex="0"
@keydown="${this.handleSpacebar}"
>
${inputElements}
<span class="${checkboxContentClasses}">
${renderLabel} ${required ? REQUIRED_SYMBOL : ''}
</span>
${errorElement}
</label>
`
: html`
<label
for="${refId}"
class="a-checkbox__wrapper"
tabindex="0"
@keydown="${this.handleSpacebar}"
>
${inputElements} ${errorElement}
</label>
`;
}
firstUpdated() {
const { isReact, defaultChecked, _noTouch } = this;
if (isReact && defaultChecked) {
this.querySelector('input').checked = true;
this.state.native = true;
}
// first render has overwritten the child root - so prevent future child-root harvesting now
this._childRoot = null;
// mark non-touch devices (as detected in constructor) via hard-to-accidentallt-overwrite
// data- attribute, so that CSS can react to it
// (can't use a class because React users like to overwrite 'className', can't set in constructor
// because React expects attribute-free elements at construction time)
if (_noTouch) {
this.dataset.axaCheckboxNoTouch = '';
}
}
// this lifecycle method will regularly be called after render() -
// but also *indirectly* via the handleChange event handler!
updated(why) {
if (why === 'via-handleChange') {
this.state.firstUpdate = false;
}
const {
state: { isControlled, checked, native },
} = this;
// coerce UI to conform with model state,
// no matter its native UI state
if (isControlled) {
this.querySelector('input').checked = checked;
return;
}
// correct reflection to attribute
if (native) {
this.setAttribute('checked', '');
} else {
this.removeAttribute('checked');
}
}
}
defineVersioned([AXACheckbox], __VERSION_INFO__);
export default AXACheckbox;