-
Notifications
You must be signed in to change notification settings - Fork 191
/
Textfield.ts
367 lines (319 loc) · 11.5 KB
/
Textfield.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
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import {
CSSResultArray,
html,
nothing,
PropertyValues,
SizedMixin,
TemplateResult,
} from '@spectrum-web-components/base';
import {
ifDefined,
live,
} from '@spectrum-web-components/base/src/directives.js';
import {
property,
query,
state,
} from '@spectrum-web-components/base/src/decorators.js';
import { ManageHelpText } from '@spectrum-web-components/help-text/src/manage-help-text.js';
import { Focusable } from '@spectrum-web-components/shared/src/focusable.js';
import '@spectrum-web-components/icons-ui/icons/sp-icon-checkmark100.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-alert.js';
import textfieldStyles from './textfield.css.js';
import checkmarkStyles from '@spectrum-web-components/icon/src/spectrum-icon-checkmark.css.js';
const textfieldTypes = ['text', 'url', 'tel', 'email', 'password'] as const;
export type TextfieldType = typeof textfieldTypes[number];
/**
* @fires input - The value of the element has changed.
* @fires change - An alteration to the value of the element has been committed by the user.
*/
export class TextfieldBase extends ManageHelpText(
SizedMixin(Focusable, {
noDefaultSize: true,
})
) {
public static override get styles(): CSSResultArray {
return [textfieldStyles, checkmarkStyles];
}
@state()
appliedLabel?: string;
@property({ attribute: 'allowed-keys' })
allowedKeys = '';
@property({ type: Boolean, reflect: true })
public focused = false;
@query('.input:not(#sizer)')
protected inputElement!: HTMLInputElement | HTMLTextAreaElement;
@property({ type: Boolean, reflect: true })
public invalid = false;
@property()
public label = '';
@property()
public placeholder = '';
@property({ attribute: 'type', reflect: true })
private _type: TextfieldType = 'text';
@state()
get type(): TextfieldType {
return textfieldTypes.find((t) => t === this._type) ?? 'text';
}
set type(val: TextfieldType) {
const prev = this._type;
this._type = val;
this.requestUpdate('type', prev);
}
@property()
public pattern?: string;
@property({ type: Boolean, reflect: true })
public grows = false;
@property({ type: Number })
public maxlength = -1;
@property({ type: Number })
public minlength = -1;
@property({ type: Boolean, reflect: true })
public multiline = false;
@property({ type: Boolean, reflect: true })
public readonly = false;
@property({ type: Number })
public rows = -1;
@property({ type: Boolean, reflect: true })
public valid = false;
@property({ type: String })
public set value(value: string | number) {
if (value === this.value) {
return;
}
const oldValue = this._value;
this._value = value;
this.requestUpdate('value', oldValue);
}
public get value(): string | number {
return this._value;
}
protected _value: string | number = '';
@property({ type: Boolean, reflect: true })
public quiet = false;
@property({ type: Boolean, reflect: true })
public required = false;
@property({ type: String, reflect: true })
public autocomplete?:
| HTMLInputElement['autocomplete']
| HTMLTextAreaElement['autocomplete'];
public override get focusElement(): HTMLInputElement | HTMLTextAreaElement {
return this.inputElement;
}
/**
* Sets the start and end positions of the current selection.
*
* @param selectionStart The 0-based index of the first selected character. An index greater than the length of the
* element's value is treated as pointing to the end of the value.
* @param selectionEnd The 0-based index of the character after the last selected character. An index greater than
* the length of the element's value is treated as pointing to the end of the value.
* @param [selectionDirection="none"] A string indicating the direction in which the selection is considered to
* have been performed.
*/
public setSelectionRange(
selectionStart: number,
selectionEnd: number,
selectionDirection: 'forward' | 'backward' | 'none' = 'none'
): void {
this.inputElement.setSelectionRange(
selectionStart,
selectionEnd,
selectionDirection
);
}
/**
* Selects all the text.
*/
public select(): void {
this.inputElement.select();
}
protected handleInput(): void {
if (this.allowedKeys && this.inputElement.value) {
const regExp = new RegExp(`^[${this.allowedKeys}]*$`, 'u');
if (!regExp.test(this.inputElement.value)) {
const selectionStart = this.inputElement
.selectionStart as number;
const nextSelectStart = selectionStart - 1;
this.inputElement.value = this.value.toString();
this.inputElement.setSelectionRange(
nextSelectStart,
nextSelectStart
);
return;
}
}
this.value = this.inputElement.value;
}
protected handleChange(): void {
this.dispatchEvent(
new Event('change', {
bubbles: true,
composed: true,
})
);
}
protected onFocus(): void {
this.focused = !this.readonly && true;
}
protected onBlur(): void {
this.focused = !this.readonly && false;
}
protected renderStateIcons(): TemplateResult | typeof nothing {
if (this.invalid) {
return html`
<sp-icon-alert id="invalid" class="icon"></sp-icon-alert>
`;
} else if (this.valid) {
return html`
<sp-icon-checkmark100
id="valid"
class="icon spectrum-UIIcon-Checkmark100"
></sp-icon-checkmark100>
`;
}
return nothing;
}
protected get displayValue(): string {
return this.value.toString();
}
private get renderMultiline(): TemplateResult {
return html`
${this.grows && this.rows === -1
? html`
<div id="sizer" class="input" aria-hidden="true">
${this.value}​
</div>
`
: nothing}
<!-- @ts-ignore -->
<textarea
aria-describedby=${this.helpTextId}
aria-label=${this.label ||
this.appliedLabel ||
this.placeholder}
aria-invalid=${ifDefined(this.invalid || undefined)}
class="input"
maxlength=${ifDefined(
this.maxlength > -1 ? this.maxlength : undefined
)}
minlength=${ifDefined(
this.minlength > -1 ? this.minlength : undefined
)}
title=${this.invalid ? '' : nothing}
pattern=${ifDefined(this.pattern)}
placeholder=${this.placeholder}
.value=${this.displayValue}
@change=${this.handleChange}
@input=${this.handleInput}
@focus=${this.onFocus}
@blur=${this.onBlur}
?disabled=${this.disabled}
?required=${this.required}
?readonly=${this.readonly}
rows=${ifDefined(this.rows > -1 ? this.rows : undefined)}
autocomplete=${ifDefined(this.autocomplete)}
></textarea>
`;
}
private get renderInput(): TemplateResult {
return html`
<!-- @ts-ignore -->
<input
type=${this.type}
aria-describedby=${this.helpTextId}
aria-label=${this.label ||
this.appliedLabel ||
this.placeholder}
aria-invalid=${ifDefined(this.invalid || undefined)}
class="input"
title=${this.invalid ? '' : nothing}
maxlength=${ifDefined(
this.maxlength > -1 ? this.maxlength : undefined
)}
minlength=${ifDefined(
this.minlength > -1 ? this.minlength : undefined
)}
pattern=${ifDefined(this.pattern)}
placeholder=${this.placeholder}
.value=${live(this.displayValue)}
@change=${this.handleChange}
@input=${this.handleInput}
@focus=${this.onFocus}
@blur=${this.onBlur}
?disabled=${this.disabled}
?required=${this.required}
?readonly=${this.readonly}
autocomplete=${ifDefined(this.autocomplete)}
/>
`;
}
protected renderField(): TemplateResult {
return html`
${this.renderStateIcons()}
${this.multiline ? this.renderMultiline : this.renderInput}
`;
}
protected override render(): TemplateResult {
return html`
<div id="textfield">${this.renderField()}</div>
${this.renderHelpText(this.invalid)}
`;
}
protected override update(changedProperties: PropertyValues): void {
if (
changedProperties.has('value') ||
(changedProperties.has('required') && this.required)
) {
this.updateComplete.then(() => {
this.checkValidity();
});
}
super.update(changedProperties);
}
public checkValidity(): boolean {
let validity = this.inputElement.checkValidity();
if (this.required || (this.value && this.pattern)) {
if ((this.disabled || this.multiline) && this.pattern) {
const regex = new RegExp(`^${this.pattern}$`, 'u');
validity = regex.test(this.value.toString());
}
if (typeof this.minlength !== 'undefined') {
validity =
validity && this.value.toString().length >= this.minlength;
}
this.valid = validity;
this.invalid = !validity;
}
return validity;
}
}
/**
* @element sp-textfield
* @slot help-text - default or non-negative help text to associate to your form element
* @slot negative-help-text - negative help text to associate to your form element when `invalid`
*/
export class Textfield extends TextfieldBase {
@property({ type: String })
public override set value(value: string) {
if (value === this.value) {
return;
}
const oldValue = this._value;
this._value = value;
this.requestUpdate('value', oldValue);
}
public override get value(): string {
return this._value;
}
protected override _value = '';
}