forked from material-components/material-components-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
foundation.ts
490 lines (436 loc) · 16.1 KB
/
foundation.ts
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
* @license
* Copyright 2016 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import {MDCFoundation} from '@material/base/foundation';
import {SpecificEventListener} from '@material/base/types';
import {MDCTextFieldAdapter} from './adapter';
import {MDCTextFieldCharacterCounterFoundation} from './character-counter/foundation';
import {ALWAYS_FLOAT_TYPES, cssClasses, numbers, strings, VALIDATION_ATTR_WHITELIST} from './constants';
import {MDCTextFieldHelperTextFoundation} from './helper-text/foundation';
import {MDCTextFieldIconFoundation} from './icon/foundation';
import {MDCTextFieldFoundationMap, MDCTextFieldNativeInputElement} from './types';
type PointerDownEventType = 'mousedown' | 'touchstart';
type InteractionEventType = 'click' | 'keydown';
const POINTERDOWN_EVENTS: PointerDownEventType[] = ['mousedown', 'touchstart'];
const INTERACTION_EVENTS: InteractionEventType[] = ['click', 'keydown'];
export class MDCTextFieldFoundation extends MDCFoundation<MDCTextFieldAdapter> {
static get cssClasses() {
return cssClasses;
}
static get strings() {
return strings;
}
static get numbers() {
return numbers;
}
private get shouldAlwaysFloat_(): boolean {
const type = this.getNativeInput_().type;
return ALWAYS_FLOAT_TYPES.indexOf(type) >= 0;
}
get shouldFloat(): boolean {
return this.shouldAlwaysFloat_ || this.isFocused_ || Boolean(this.getValue()) || this.isBadInput_();
}
get shouldShake(): boolean {
return !this.isFocused_ && !this.isValid() && Boolean(this.getValue());
}
/**
* See {@link MDCTextFieldAdapter} for typing information on parameters and return types.
*/
static get defaultAdapter(): MDCTextFieldAdapter {
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
return {
addClass: () => undefined,
removeClass: () => undefined,
hasClass: () => true,
registerTextFieldInteractionHandler: () => undefined,
deregisterTextFieldInteractionHandler: () => undefined,
registerInputInteractionHandler: () => undefined,
deregisterInputInteractionHandler: () => undefined,
registerValidationAttributeChangeHandler: () => new MutationObserver(() => undefined),
deregisterValidationAttributeChangeHandler: () => undefined,
getNativeInput: () => null,
isFocused: () => false,
activateLineRipple: () => undefined,
deactivateLineRipple: () => undefined,
setLineRippleTransformOrigin: () => undefined,
shakeLabel: () => undefined,
floatLabel: () => undefined,
hasLabel: () => false,
getLabelWidth: () => 0,
hasOutline: () => false,
notchOutline: () => undefined,
closeOutline: () => undefined,
};
// tslint:enable:object-literal-sort-keys
}
private isFocused_ = false;
private receivedUserInput_ = false;
private isValid_ = true;
private useNativeValidation_ = true;
private readonly inputFocusHandler_: () => void;
private readonly inputBlurHandler_: SpecificEventListener<'blur'>;
private readonly inputInputHandler_: SpecificEventListener<'input'>;
private readonly setPointerXOffset_: SpecificEventListener<PointerDownEventType>;
private readonly textFieldInteractionHandler_: SpecificEventListener<InteractionEventType>;
private readonly validationAttributeChangeHandler_: (attributesList: string[]) => void;
private validationObserver_!: MutationObserver; // assigned in init()
private readonly helperText_?: MDCTextFieldHelperTextFoundation;
private readonly characterCounter_?: MDCTextFieldCharacterCounterFoundation;
private readonly leadingIcon_?: MDCTextFieldIconFoundation;
private readonly trailingIcon_?: MDCTextFieldIconFoundation;
/**
* @param adapter
* @param foundationMap Map from subcomponent names to their subfoundations.
*/
constructor(adapter?: Partial<MDCTextFieldAdapter>, foundationMap: Partial<MDCTextFieldFoundationMap> = {}) {
super({...MDCTextFieldFoundation.defaultAdapter, ...adapter});
this.helperText_ = foundationMap.helperText;
this.characterCounter_ = foundationMap.characterCounter;
this.leadingIcon_ = foundationMap.leadingIcon;
this.trailingIcon_ = foundationMap.trailingIcon;
this.inputFocusHandler_ = () => this.activateFocus();
this.inputBlurHandler_ = () => this.deactivateFocus();
this.inputInputHandler_ = () => this.handleInput();
this.setPointerXOffset_ = (evt) => this.setTransformOrigin(evt);
this.textFieldInteractionHandler_ = () => this.handleTextFieldInteraction();
this.validationAttributeChangeHandler_ = (attributesList) => this.handleValidationAttributeChange(attributesList);
}
init() {
if (this.adapter_.isFocused()) {
this.inputFocusHandler_();
} else if (this.adapter_.hasLabel() && this.shouldFloat) {
this.notchOutline(true);
this.adapter_.floatLabel(true);
}
this.adapter_.registerInputInteractionHandler('focus', this.inputFocusHandler_);
this.adapter_.registerInputInteractionHandler('blur', this.inputBlurHandler_);
this.adapter_.registerInputInteractionHandler('input', this.inputInputHandler_);
POINTERDOWN_EVENTS.forEach((evtType) => {
this.adapter_.registerInputInteractionHandler(evtType, this.setPointerXOffset_);
});
INTERACTION_EVENTS.forEach((evtType) => {
this.adapter_.registerTextFieldInteractionHandler(evtType, this.textFieldInteractionHandler_);
});
this.validationObserver_ =
this.adapter_.registerValidationAttributeChangeHandler(this.validationAttributeChangeHandler_);
this.setCharacterCounter_(this.getValue().length);
}
destroy() {
this.adapter_.deregisterInputInteractionHandler('focus', this.inputFocusHandler_);
this.adapter_.deregisterInputInteractionHandler('blur', this.inputBlurHandler_);
this.adapter_.deregisterInputInteractionHandler('input', this.inputInputHandler_);
POINTERDOWN_EVENTS.forEach((evtType) => {
this.adapter_.deregisterInputInteractionHandler(evtType, this.setPointerXOffset_);
});
INTERACTION_EVENTS.forEach((evtType) => {
this.adapter_.deregisterTextFieldInteractionHandler(evtType, this.textFieldInteractionHandler_);
});
this.adapter_.deregisterValidationAttributeChangeHandler(this.validationObserver_);
}
/**
* Handles user interactions with the Text Field.
*/
handleTextFieldInteraction() {
const nativeInput = this.adapter_.getNativeInput();
if (nativeInput && nativeInput.disabled) {
return;
}
this.receivedUserInput_ = true;
}
/**
* Handles validation attribute changes
*/
handleValidationAttributeChange(attributesList: string[]): void {
attributesList.some((attributeName) => {
if (VALIDATION_ATTR_WHITELIST.indexOf(attributeName) > -1) {
this.styleValidity_(true);
return true;
}
return false;
});
if (attributesList.indexOf('maxlength') > -1) {
this.setCharacterCounter_(this.getValue().length);
}
}
/**
* Opens/closes the notched outline.
*/
notchOutline(openNotch: boolean) {
if (!this.adapter_.hasOutline()) {
return;
}
if (openNotch) {
const isDense = this.adapter_.hasClass(cssClasses.DENSE);
const labelScale = isDense ? numbers.DENSE_LABEL_SCALE : numbers.LABEL_SCALE;
const labelWidth = this.adapter_.getLabelWidth() * labelScale;
this.adapter_.notchOutline(labelWidth);
} else {
this.adapter_.closeOutline();
}
}
/**
* Activates the text field focus state.
*/
activateFocus() {
this.isFocused_ = true;
this.styleFocused_(this.isFocused_);
this.adapter_.activateLineRipple();
if (this.adapter_.hasLabel()) {
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
if (this.helperText_) {
this.helperText_.showToScreenReader();
}
}
/**
* Sets the line ripple's transform origin, so that the line ripple activate
* animation will animate out from the user's click location.
*/
setTransformOrigin(evt: TouchEvent | MouseEvent): void {
const touches = (evt as TouchEvent).touches;
const targetEvent = touches ? touches[0] : evt;
const targetClientRect = (targetEvent.target as Element).getBoundingClientRect();
const normalizedX = (targetEvent as MouseEvent).clientX - targetClientRect.left;
this.adapter_.setLineRippleTransformOrigin(normalizedX);
}
/**
* Handles input change of text input and text area.
*/
handleInput() {
this.autoCompleteFocus();
this.setCharacterCounter_(this.getValue().length);
}
/**
* Activates the Text Field's focus state in cases when the input value
* changes without user input (e.g. programmatically).
*/
autoCompleteFocus() {
if (!this.receivedUserInput_) {
this.activateFocus();
}
}
/**
* Deactivates the Text Field's focus state.
*/
deactivateFocus() {
this.isFocused_ = false;
this.adapter_.deactivateLineRipple();
const isValid = this.isValid();
this.styleValidity_(isValid);
this.styleFocused_(this.isFocused_);
if (this.adapter_.hasLabel()) {
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
if (!this.shouldFloat) {
this.receivedUserInput_ = false;
}
}
getValue(): string {
return this.getNativeInput_().value;
}
/**
* @param value The value to set on the input Element.
*/
setValue(value: string): void {
// Prevent Safari from moving the caret to the end of the input when the value has not changed.
if (this.getValue() !== value) {
this.getNativeInput_().value = value;
}
this.setCharacterCounter_(value.length);
const isValid = this.isValid();
this.styleValidity_(isValid);
if (this.adapter_.hasLabel()) {
this.notchOutline(this.shouldFloat);
this.adapter_.floatLabel(this.shouldFloat);
this.adapter_.shakeLabel(this.shouldShake);
}
}
/**
* @return The custom validity state, if set; otherwise, the result of a native validity check.
*/
isValid(): boolean {
return this.useNativeValidation_
? this.isNativeInputValid_() : this.isValid_;
}
/**
* @param isValid Sets the custom validity state of the Text Field.
*/
setValid(isValid: boolean): void {
this.isValid_ = isValid;
this.styleValidity_(isValid);
const shouldShake = !isValid && !this.isFocused_;
if (this.adapter_.hasLabel()) {
this.adapter_.shakeLabel(shouldShake);
}
}
/**
* Enables or disables the use of native validation. Use this for custom validation.
* @param useNativeValidation Set this to false to ignore native input validation.
*/
setUseNativeValidation(useNativeValidation: boolean): void {
this.useNativeValidation_ = useNativeValidation;
}
isDisabled(): boolean {
return this.getNativeInput_().disabled;
}
/**
* @param disabled Sets the text-field disabled or enabled.
*/
setDisabled(disabled: boolean): void {
this.getNativeInput_().disabled = disabled;
this.styleDisabled_(disabled);
}
/**
* @param content Sets the content of the helper text.
*/
setHelperTextContent(content: string): void {
if (this.helperText_) {
this.helperText_.setContent(content);
}
}
/**
* Sets the aria label of the leading icon.
*/
setLeadingIconAriaLabel(label: string): void {
if (this.leadingIcon_) {
this.leadingIcon_.setAriaLabel(label);
}
}
/**
* Sets the text content of the leading icon.
*/
setLeadingIconContent(content: string): void {
if (this.leadingIcon_) {
this.leadingIcon_.setContent(content);
}
}
/**
* Sets the aria label of the trailing icon.
*/
setTrailingIconAriaLabel(label: string): void {
if (this.trailingIcon_) {
this.trailingIcon_.setAriaLabel(label);
}
}
/**
* Sets the text content of the trailing icon.
*/
setTrailingIconContent(content: string): void {
if (this.trailingIcon_) {
this.trailingIcon_.setContent(content);
}
}
/**
* Sets character counter values that shows characters used and the total character limit.
*/
private setCharacterCounter_(currentLength: number): void {
if (!this.characterCounter_) {
return;
}
const maxLength = this.getNativeInput_().maxLength;
if (maxLength === -1) {
throw new Error('MDCTextFieldFoundation: Expected maxlength html property on text input or textarea.');
}
this.characterCounter_.setCounterValue(currentLength, maxLength);
}
/**
* @return True if the Text Field input fails in converting the user-supplied value.
*/
private isBadInput_(): boolean {
// The badInput property is not supported in IE 11 💩.
return this.getNativeInput_().validity.badInput || false;
}
/**
* @return The result of native validity checking (ValidityState.valid).
*/
private isNativeInputValid_(): boolean {
return this.getNativeInput_().validity.valid;
}
/**
* Styles the component based on the validity state.
*/
private styleValidity_(isValid: boolean): void {
const {INVALID} = MDCTextFieldFoundation.cssClasses;
if (isValid) {
this.adapter_.removeClass(INVALID);
} else {
this.adapter_.addClass(INVALID);
}
if (this.helperText_) {
this.helperText_.setValidity(isValid);
}
}
/**
* Styles the component based on the focused state.
*/
private styleFocused_(isFocused: boolean): void {
const {FOCUSED} = MDCTextFieldFoundation.cssClasses;
if (isFocused) {
this.adapter_.addClass(FOCUSED);
} else {
this.adapter_.removeClass(FOCUSED);
}
}
/**
* Styles the component based on the disabled state.
*/
private styleDisabled_(isDisabled: boolean): void {
const {DISABLED, INVALID} = MDCTextFieldFoundation.cssClasses;
if (isDisabled) {
this.adapter_.addClass(DISABLED);
this.adapter_.removeClass(INVALID);
} else {
this.adapter_.removeClass(DISABLED);
}
if (this.leadingIcon_) {
this.leadingIcon_.setDisabled(isDisabled);
}
if (this.trailingIcon_) {
this.trailingIcon_.setDisabled(isDisabled);
}
}
/**
* @return The native text input element from the host environment, or an object with the same shape for unit tests.
*/
private getNativeInput_(): MDCTextFieldNativeInputElement {
// this.adapter_ may be undefined in foundation unit tests. This happens when testdouble is creating a mock object
// and invokes the shouldShake/shouldFloat getters (which in turn call getValue(), which calls this method) before
// init() has been called from the MDCTextField constructor. To work around that issue, we return a dummy object.
const nativeInput = this.adapter_ ? this.adapter_.getNativeInput() : null;
return nativeInput || {
disabled: false,
maxLength: -1,
type: 'input',
validity: {
badInput: false,
valid: true,
},
value: '',
};
}
}
// tslint:disable-next-line:no-default-export Needed for backward compatibility with MDC Web v0.44.0 and earlier.
export default MDCTextFieldFoundation;