-
Notifications
You must be signed in to change notification settings - Fork 905
/
text-field.ts
794 lines (701 loc) · 22.9 KB
/
text-field.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {LitElement, PropertyValues, html, nothing} from 'lit';
import {property, query, queryAssignedElements, state} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';
import {live} from 'lit/directives/live.js';
import {StyleInfo, styleMap} from 'lit/directives/style-map.js';
import {StaticValue, html as staticHtml} from 'lit/static-html.js';
import {Field} from '../../field/internal/field.js';
import {ARIAMixinStrict} from '../../internal/aria/aria.js';
import {mixinDelegatesAria} from '../../internal/aria/delegate.js';
import {stringConverter} from '../../internal/controller/string-converter.js';
import {redispatchEvent} from '../../internal/events/redispatch-event.js';
import {
createValidator,
getValidityAnchor,
mixinConstraintValidation,
} from '../../labs/behaviors/constraint-validation.js';
import {mixinElementInternals} from '../../labs/behaviors/element-internals.js';
import {
getFormValue,
mixinFormAssociated,
} from '../../labs/behaviors/form-associated.js';
import {
mixinOnReportValidity,
onReportValidity,
} from '../../labs/behaviors/on-report-validity.js';
import {TextFieldValidator} from '../../labs/behaviors/validators/text-field-validator.js';
import {Validator} from '../../labs/behaviors/validators/validator.js';
/**
* Input types that are compatible with the text field.
*/
export type TextFieldType =
| 'email'
| 'number'
| 'password'
| 'search'
| 'tel'
| 'text'
| 'url'
| 'textarea';
/**
* Input types that are not fully supported for the text field.
*/
export type UnsupportedTextFieldType =
| 'color'
| 'date'
| 'datetime-local'
| 'file'
| 'month'
| 'time'
| 'week';
/**
* Input types that are incompatible with the text field.
*/
export type InvalidTextFieldType =
| 'button'
| 'checkbox'
| 'hidden'
| 'image'
| 'radio'
| 'range'
| 'reset'
| 'submit';
// Separate variable needed for closure.
const textFieldBaseClass = mixinDelegatesAria(
mixinOnReportValidity(
mixinConstraintValidation(
mixinFormAssociated(mixinElementInternals(LitElement)),
),
),
);
/**
* A text field component.
*
* @fires select {Event} The native `select` event on
* [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select_event)
* --bubbles
* @fires change {Event} The native `change` event on
* [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event)
* --bubbles
* @fires input {InputEvent} The native `input` event on
* [`<input>`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event)
* --bubbles --composed
*/
export abstract class TextField extends textFieldBaseClass {
/** @nocollapse */
static override shadowRootOptions: ShadowRootInit = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
};
/**
* Gets or sets whether or not the text field is in a visually invalid state.
*
* This error state overrides the error state controlled by
* `reportValidity()`.
*/
@property({type: Boolean, reflect: true}) error = false;
/**
* The error message that replaces supporting text when `error` is true. If
* `errorText` is an empty string, then the supporting text will continue to
* show.
*
* This error message overrides the error message displayed by
* `reportValidity()`.
*/
@property({attribute: 'error-text'}) errorText = '';
/**
* The floating Material label of the textfield component. It informs the user
* about what information is requested for a text field. It is aligned with
* the input text, is always visible, and it floats when focused or when text
* is entered into the textfield. This label also sets accessibilty labels,
* but the accessible label is overriden by `aria-label`.
*
* Learn more about floating labels from the Material Design guidelines:
* https://m3.material.io/components/text-fields/guidelines
*/
@property() label = '';
/**
* Disables the asterisk on the floating label, when the text field is
* required.
*/
@property({type: Boolean, attribute: 'no-asterisk'}) noAsterisk = false;
/**
* Indicates that the user must specify a value for the input before the
* owning form can be submitted and will render an error state when
* `reportValidity()` is invoked when value is empty. Additionally the
* floating label will render an asterisk `"*"` when true.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/required
*/
@property({type: Boolean, reflect: true}) required = false;
/**
* The current value of the text field. It is always a string.
*/
@property() value = '';
/**
* An optional prefix to display before the input value.
*/
@property({attribute: 'prefix-text'}) prefixText = '';
/**
* An optional suffix to display after the input value.
*/
@property({attribute: 'suffix-text'}) suffixText = '';
/**
* Whether or not the text field has a leading icon. Used for SSR.
*/
@property({type: Boolean, attribute: 'has-leading-icon'})
hasLeadingIcon = false;
/**
* Whether or not the text field has a trailing icon. Used for SSR.
*/
@property({type: Boolean, attribute: 'has-trailing-icon'})
hasTrailingIcon = false;
/**
* Conveys additional information below the text field, such as how it should
* be used.
*/
@property({attribute: 'supporting-text'}) supportingText = '';
/**
* Override the input text CSS `direction`. Useful for RTL languages that use
* LTR notation for fractions.
*/
@property({attribute: 'text-direction'}) textDirection = '';
/**
* The number of rows to display for a `type="textarea"` text field.
* Defaults to 2.
*/
@property({type: Number}) rows = 2;
/**
* The number of cols to display for a `type="textarea"` text field.
* Defaults to 20.
*/
@property({type: Number}) cols = 20;
// <input> properties
@property({reflect: true}) override inputMode = '';
/**
* Defines the greatest value in the range of permitted values.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#max
*/
@property() max = '';
/**
* The maximum number of characters a user can enter into the text field. Set
* to -1 for none.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#maxlength
*/
@property({type: Number}) maxLength = -1;
/**
* Defines the most negative value in the range of permitted values.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#min
*/
@property() min = '';
/**
* The minimum number of characters a user can enter into the text field. Set
* to -1 for none.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#minlength
*/
@property({type: Number}) minLength = -1;
/**
* When true, hide the spinner for `type="number"` text fields.
*/
@property({type: Boolean, attribute: 'no-spinner'}) noSpinner = false;
/**
* A regular expression that the text field's value must match to pass
* constraint validation.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#pattern
*/
@property() pattern = '';
/**
* Defines the text displayed in the textfield when it has no value. Provides
* a brief hint to the user as to the expected type of data that should be
* entered into the control. Unlike `label`, the placeholder is not visible
* and does not float when the textfield has a value.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/placeholder
*/
@property({reflect: true, converter: stringConverter}) placeholder = '';
/**
* Indicates whether or not a user should be able to edit the text field's
* value.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#readonly
*/
@property({type: Boolean, reflect: true}) readOnly = false;
/**
* Indicates that input accepts multiple email addresses.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#multiple
*/
@property({type: Boolean, reflect: true}) multiple = false;
/**
* Gets or sets the direction in which selection occurred.
*/
get selectionDirection() {
return this.getInputOrTextarea().selectionDirection;
}
set selectionDirection(value: 'forward' | 'backward' | 'none' | null) {
this.getInputOrTextarea().selectionDirection = value;
}
/**
* Gets or sets the end position or offset of a text selection.
*/
get selectionEnd() {
return this.getInputOrTextarea().selectionEnd;
}
set selectionEnd(value: number | null) {
this.getInputOrTextarea().selectionEnd = value;
}
/**
* Gets or sets the starting position or offset of a text selection.
*/
get selectionStart() {
return this.getInputOrTextarea().selectionStart;
}
set selectionStart(value: number | null) {
this.getInputOrTextarea().selectionStart = value;
}
/**
* Returns or sets the element's step attribute, which works with min and max
* to limit the increments at which a numeric or date-time value can be set.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#step
*/
@property() step = '';
/**
* The `<input>` type to use, defaults to "text". The type greatly changes how
* the text field behaves.
*
* Text fields support a limited number of `<input>` types:
*
* - text
* - textarea
* - email
* - number
* - password
* - search
* - tel
* - url
*
* See
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types
* for more details on each input type.
*/
@property({reflect: true})
type: TextFieldType | UnsupportedTextFieldType = 'text';
/**
* Describes what, if any, type of autocomplete functionality the input
* should provide.
*
* https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
*/
@property({reflect: true}) autocomplete = '';
/**
* The text field's value as a number.
*/
get valueAsNumber() {
const input = this.getInput();
if (!input) {
return NaN;
}
return input.valueAsNumber;
}
set valueAsNumber(value: number) {
const input = this.getInput();
if (!input) {
return;
}
input.valueAsNumber = value;
this.value = input.value;
}
/**
* The text field's value as a Date.
*/
get valueAsDate() {
const input = this.getInput();
if (!input) {
return null;
}
return input.valueAsDate;
}
set valueAsDate(value: Date | null) {
const input = this.getInput();
if (!input) {
return;
}
input.valueAsDate = value;
this.value = input.value;
}
protected abstract readonly fieldTag: StaticValue;
/**
* Returns true when the text field has been interacted with. Native
* validation errors only display in response to user interactions.
*/
@state() private dirty = false;
@state() private focused = false;
/**
* Whether or not a native error has been reported via `reportValidity()`.
*/
@state() private nativeError = false;
/**
* The validation message displayed from a native error via
* `reportValidity()`.
*/
@state() private nativeErrorText = '';
private get hasError() {
return this.error || this.nativeError;
}
@query('.input')
private readonly inputOrTextarea!:
| HTMLInputElement
| HTMLTextAreaElement
| null;
@query('.field') private readonly field!: Field | null;
@queryAssignedElements({slot: 'leading-icon'})
private readonly leadingIcons!: Element[];
@queryAssignedElements({slot: 'trailing-icon'})
private readonly trailingIcons!: Element[];
/**
* Selects all the text in the text field.
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select
*/
select() {
this.getInputOrTextarea().select();
}
/**
* Replaces a range of text with a new string.
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setRangeText
*/
setRangeText(replacement: string): void;
setRangeText(
replacement: string,
start: number,
end: number,
selectionMode?: SelectionMode,
): void;
setRangeText(...args: unknown[]) {
// Calling setRangeText with 1 vs 3-4 arguments has different behavior.
// Use spread syntax and type casting to ensure correct usage.
this.getInputOrTextarea().setRangeText(
...(args as Parameters<HTMLInputElement['setRangeText']>),
);
this.value = this.getInputOrTextarea().value;
}
/**
* Sets the start and end positions of a selection in the text field.
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
*
* @param start The offset into the text field for the start of the selection.
* @param end The offset into the text field for the end of the selection.
* @param direction The direction in which the selection is performed.
*/
setSelectionRange(
start: number | null,
end: number | null,
direction?: 'forward' | 'backward' | 'none',
) {
this.getInputOrTextarea().setSelectionRange(start, end, direction);
}
/**
* Decrements the value of a numeric type text field by `step` or `n` `step`
* number of times.
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/stepDown
*
* @param stepDecrement The number of steps to decrement, defaults to 1.
*/
stepDown(stepDecrement?: number) {
const input = this.getInput();
if (!input) {
return;
}
input.stepDown(stepDecrement);
this.value = input.value;
}
/**
* Increments the value of a numeric type text field by `step` or `n` `step`
* number of times.
*
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/stepUp
*
* @param stepIncrement The number of steps to increment, defaults to 1.
*/
stepUp(stepIncrement?: number) {
const input = this.getInput();
if (!input) {
return;
}
input.stepUp(stepIncrement);
this.value = input.value;
}
/**
* Reset the text field to its default value.
*/
reset() {
this.dirty = false;
this.value = this.getAttribute('value') ?? '';
this.nativeError = false;
this.nativeErrorText = '';
}
override attributeChangedCallback(
attribute: string,
newValue: string | null,
oldValue: string | null,
) {
if (attribute === 'value' && this.dirty) {
// After user input, changing the value attribute no longer updates the
// text field's value (until reset). This matches native <input> behavior.
return;
}
super.attributeChangedCallback(attribute, newValue, oldValue);
}
protected override render() {
const classes = {
'disabled': this.disabled,
'error': !this.disabled && this.hasError,
'textarea': this.type === 'textarea',
'no-spinner': this.noSpinner,
};
return html`
<span class="text-field ${classMap(classes)}">
${this.renderField()}
</span>
`;
}
protected override updated(changedProperties: PropertyValues) {
// Keep changedProperties arg so that subclasses may call it
// If a property such as `type` changes and causes the internal <input>
// value to change without dispatching an event, re-sync it.
const value = this.getInputOrTextarea().value;
if (this.value !== value) {
// Note this is typically inefficient in updated() since it schedules
// another update. However, it is needed for the <input> to fully render
// before checking its value.
this.value = value;
}
}
private renderField() {
return staticHtml`<${this.fieldTag}
class="field"
count=${this.value.length}
?disabled=${this.disabled}
?error=${this.hasError}
error-text=${this.getErrorText()}
?focused=${this.focused}
?has-end=${this.hasTrailingIcon}
?has-start=${this.hasLeadingIcon}
label=${this.label}
?no-asterisk=${this.noAsterisk}
max=${this.maxLength}
?populated=${!!this.value}
?required=${this.required}
?resizable=${this.type === 'textarea'}
supporting-text=${this.supportingText}
>
${this.renderLeadingIcon()}
${this.renderInputOrTextarea()}
${this.renderTrailingIcon()}
<div id="description" slot="aria-describedby"></div>
<slot name="container" slot="container"></slot>
</${this.fieldTag}>`;
}
private renderLeadingIcon() {
return html`
<span class="icon leading" slot="start">
<slot name="leading-icon" @slotchange=${this.handleIconChange}></slot>
</span>
`;
}
private renderTrailingIcon() {
return html`
<span class="icon trailing" slot="end">
<slot name="trailing-icon" @slotchange=${this.handleIconChange}></slot>
</span>
`;
}
private renderInputOrTextarea() {
const style: StyleInfo = {'direction': this.textDirection};
const ariaLabel =
(this as ARIAMixinStrict).ariaLabel || this.label || nothing;
// lit-anaylzer `autocomplete` types are too strict
// tslint:disable-next-line:no-any
const autocomplete = this.autocomplete as any;
// These properties may be set to null if the attribute is removed, and
// `null > -1` is incorrectly `true`.
const hasMaxLength = (this.maxLength ?? -1) > -1;
const hasMinLength = (this.minLength ?? -1) > -1;
if (this.type === 'textarea') {
return html`
<textarea
class="input"
style=${styleMap(style)}
aria-describedby="description"
aria-invalid=${this.hasError}
aria-label=${ariaLabel}
autocomplete=${autocomplete || nothing}
name=${this.name || nothing}
?disabled=${this.disabled}
maxlength=${hasMaxLength ? this.maxLength : nothing}
minlength=${hasMinLength ? this.minLength : nothing}
placeholder=${this.placeholder || nothing}
?readonly=${this.readOnly}
?required=${this.required}
rows=${this.rows}
cols=${this.cols}
.value=${live(this.value)}
@change=${this.redispatchEvent}
@focus=${this.handleFocusChange}
@blur=${this.handleFocusChange}
@input=${this.handleInput}
@select=${this.redispatchEvent}></textarea>
`;
}
const prefix = this.renderPrefix();
const suffix = this.renderSuffix();
// TODO(b/243805848): remove `as unknown as number` and `as any` once lit
// analyzer is fixed
// tslint:disable-next-line:no-any
const inputMode = this.inputMode as any;
return html`
<div class="input-wrapper">
${prefix}
<input
class="input"
style=${styleMap(style)}
aria-describedby="description"
aria-invalid=${this.hasError}
aria-label=${ariaLabel}
autocomplete=${autocomplete || nothing}
name=${this.name || nothing}
?disabled=${this.disabled}
inputmode=${inputMode || nothing}
max=${(this.max || nothing) as unknown as number}
maxlength=${hasMaxLength ? this.maxLength : nothing}
min=${(this.min || nothing) as unknown as number}
minlength=${hasMinLength ? this.minLength : nothing}
pattern=${this.pattern || nothing}
placeholder=${this.placeholder || nothing}
?readonly=${this.readOnly}
?required=${this.required}
?multiple=${this.multiple}
step=${(this.step || nothing) as unknown as number}
type=${this.type}
.value=${live(this.value)}
@change=${this.redispatchEvent}
@focus=${this.handleFocusChange}
@blur=${this.handleFocusChange}
@input=${this.handleInput}
@select=${this.redispatchEvent} />
${suffix}
</div>
`;
}
private renderPrefix() {
return this.renderAffix(this.prefixText, /* isSuffix */ false);
}
private renderSuffix() {
return this.renderAffix(this.suffixText, /* isSuffix */ true);
}
private renderAffix(text: string, isSuffix: boolean) {
if (!text) {
return nothing;
}
const classes = {
'suffix': isSuffix,
'prefix': !isSuffix,
};
return html`<span class="${classMap(classes)}">${text}</span>`;
}
private getErrorText() {
return this.error ? this.errorText : this.nativeErrorText;
}
private handleFocusChange() {
// When calling focus() or reportValidity() during change, it's possible
// for blur to be called after the new focus event. Rather than set
// `this.focused` to true/false on focus/blur, we always set it to whether
// or not the input itself is focused.
this.focused = this.inputOrTextarea?.matches(':focus') ?? false;
}
private handleInput(event: InputEvent) {
this.dirty = true;
this.value = (event.target as HTMLInputElement).value;
}
private redispatchEvent(event: Event) {
redispatchEvent(this, event);
}
private getInputOrTextarea() {
if (!this.inputOrTextarea) {
// If the input is not yet defined, synchronously render.
// e.g.
// const textField = document.createElement('md-outlined-text-field');
// document.body.appendChild(textField);
// textField.focus(); // synchronously render
this.connectedCallback();
this.scheduleUpdate();
}
if (this.isUpdatePending) {
// If there are pending updates, synchronously perform them. This ensures
// that constraint validation properties (like `required`) are synced
// before interacting with input APIs that depend on them.
this.scheduleUpdate();
}
return this.inputOrTextarea!;
}
private getInput() {
if (this.type === 'textarea') {
return null;
}
return this.getInputOrTextarea() as HTMLInputElement;
}
private handleIconChange() {
this.hasLeadingIcon = this.leadingIcons.length > 0;
this.hasTrailingIcon = this.trailingIcons.length > 0;
}
// Writable mixin properties for lit-html binding, needed for lit-analyzer
declare disabled: boolean;
declare name: string;
override [getFormValue]() {
return this.value;
}
override formResetCallback() {
this.reset();
}
override formStateRestoreCallback(state: string) {
this.value = state;
}
override focus() {
// Required for the case that the user slots a focusable element into the
// leading icon slot such as an iconbutton due to how delegatesFocus works.
this.getInputOrTextarea().focus();
}
override [createValidator](): Validator<unknown> {
return new TextFieldValidator(() => ({
state: this,
renderedControl: this.inputOrTextarea,
}));
}
override [getValidityAnchor](): HTMLElement | null {
return this.inputOrTextarea;
}
override [onReportValidity](invalidEvent: Event | null) {
// Prevent default pop-up behavior.
invalidEvent?.preventDefault();
const prevMessage = this.getErrorText();
this.nativeError = !!invalidEvent;
this.nativeErrorText = this.validationMessage;
if (prevMessage === this.getErrorText()) {
this.field?.reannounceError();
}
}
}