-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathuui-input.element.ts
477 lines (424 loc) · 12.8 KB
/
uui-input.element.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
import {
UUIFormControlMixin,
LabelMixin,
} from '@umbraco-ui/uui-base/lib/mixins';
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
import { css, html, LitElement } from 'lit';
import { property, query } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { UUIInputEvent } from './UUIInputEvent';
export type InputType =
| 'text'
| 'tel'
| 'url'
| 'email'
| 'password'
| 'search'
| 'month'
| 'week'
| 'time'
| 'date'
| 'datetime-local'
| 'number'
| 'color';
/**
* Custom element wrapping the native input element.This is a formAssociated element, meaning it can participate in a native HTMLForm. A name:value pair will be submitted.
* @element uui-input
* @slot prepend - for components to render to the left of the input.
* @slot append - for components to render to the right of the input.
* @property {boolean} spellcheck - get/set native spellcheck attribute
* @attribute spellcheck - native spellcheck attribute
* @property {string} value - get/set the value of the input
* @attribute value - get/set the value of the input
* @property {string} name - get/set the name of the input
* @attribute name - get/set the name of the input
* @fires UUIInputEvent#change on change
* @fires InputEvent#input on input
* @fires KeyboardEvent#keyup on keyup
* @cssprop --uui-input-height - Height of the element
* @cssprop --uui-input-background-color - Background color of the element
* @cssprop --uui-input-background-color-disabled - Background color when disabled
* @cssprop --uui-input-background-color-readonly - Background color when readonly
* @cssprop --uui-input-border-width - Border width
* @cssprop --uui-input-border-color - Border color
* @cssprop --uui-input-border-color-hover - Border color on hover
* @cssprop --uui-input-border-color-focus - Border color on focus
* @cssprop --uui-input-border-color-disabled - Border color when disabled
* @cssprop --uui-input-border-color-readonly - Border color when readonly
*/
@defineElement('uui-input')
export class UUIInputElement extends UUIFormControlMixin(
LabelMixin('', LitElement),
'',
) {
/**
* This is a static class field indicating that the element is can be used inside a native form and participate in its events. It may require a polyfill, check support here https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attachInternals. Read more about form controls here https://web.dev/more-capable-form-controls/
* @type {boolean}
*/
static readonly formAssociated = true;
/**
* Sets the min value of the input.
* Examples: the first date the user may pick in date and datetime-local, or the min numeric value the user can pick in a number input.
* @type {number | string}
* @attr
* @default undefined
*/
@property()
min?: number | string;
/**
* Sets the minimum length of the value of the input.
* @type {number}
* @attr
* @default undefined
*/
@property({ type: Number })
minlength?: number;
/**
* Minlength validation message.
* @attr minlength-message
* @default
*/
@property({ type: String, attribute: 'minlength-message' })
minlengthMessage = 'This field need more characters';
/**
* Sets the max value of the input.
* Examples: the last date the user may pick in date and datetime-local, or the max numeric value the user can pick in a number input.
* @type {number | string}
* @attr
* @default undefined
*/
@property()
max?: number | string;
/**
* Sets the maximum length of the value of the input.
* @type {number}
* @attr
* @default undefined
*/
@property({ type: Number })
maxlength?: number;
/**
* Maxlength validation message.
* @attr maxlength-message
* @default
*/
@property({ type: String, attribute: 'maxlength-message' })
maxlengthMessage = 'This field exceeds the allowed amount of characters';
/**
* Specifies the interval between legal numbers of the input
* @type {number}
* @attr
* @default undefined
*/
@property({ type: Number })
step?: number;
/**
* Disables the input.
* @type {boolean}
* @attr
* @default false
*/
@property({ type: Boolean, reflect: true })
disabled = false;
/**
* Sets the input to readonly mode, meaning value cannot be changed but still able to read and select its content.
* @type {boolean}
* @attr
* @default false
*/
@property({ type: Boolean, reflect: true })
readonly = false;
/**
* Defines the input placeholder.
* @type {string}
* @attr
* @default ''
*/
@property()
placeholder = '';
/**
* Defines the input autocomplete.
* @type {string}
* @attr
* @default undefined
*/
@property()
autocomplete?: string;
/**
* Sets the input width to fit the value or placeholder if empty
* @type {boolean}
* @attr auto-width
*/
@property({ type: Boolean, reflect: true, attribute: 'auto-width' })
autoWidth = false;
/**
* This property specifies the type of input that will be rendered.
* @type {'text' | 'tel'| 'url'| 'email'| 'password'| 'date'| 'month'| 'week'| 'time'| 'datetime-local'| 'number'| 'color'}
* @attr
* @default text
*/
@property({ type: String })
get type(): InputType {
return this._type;
}
set type(value: InputType) {
this._type = value;
}
/**
* Validates the input based on the Regex pattern
* @type {string}
* @attr
*/
@property({ type: String })
pattern?: string;
/**
* The inputmode global attribute is an enumerated attribute that hints at the type of data that might be entered by the user while editing the element or its contents. This allows a browser to display an appropriate virtual keyboard.
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode|MDN} for further information
* @type {string}
* @attr
*/
@property({ type: String })
inputMode = '';
@query('#input')
_input!: HTMLInputElement;
private _type: InputType = 'text';
constructor() {
super();
this.addEventListener('mousedown', () => {
this.style.setProperty('--uui-show-focus-outline', '0');
});
this.addEventListener('blur', () => {
this.style.setProperty('--uui-show-focus-outline', '');
});
this.addEventListener('keydown', this.#onKeyDown);
this.addValidator(
'tooShort',
() => this.minlengthMessage,
() => !!this.minlength && String(this.value).length < this.minlength,
);
this.addValidator(
'tooLong',
() => this.maxlengthMessage,
() => !!this.maxlength && String(this.value).length > this.maxlength,
);
this.updateComplete.then(() => {
this.addFormControlElement(this._input);
});
}
#onKeyDown(e: KeyboardEvent): void {
if (this.type !== 'color' && e.key == 'Enter') {
this.submit();
}
}
/**
* Removes focus from the input.
*/
async blur() {
await this.updateComplete;
this._input.blur();
}
/**
* This method enables <label for="..."> to focus the input
*/
async focus() {
await this.updateComplete;
this._input.focus();
}
/**
* Selects all the text in the input.
*/
async select() {
await this.updateComplete;
this._input.select();
}
protected getFormElement(): HTMLElement {
return this.shadowRoot?.querySelector('input') as HTMLElement;
}
protected onInput(e: Event) {
e.stopPropagation();
this.value = (e.target as HTMLInputElement).value;
this.dispatchEvent(new UUIInputEvent(UUIInputEvent.INPUT));
}
protected onChange(e: Event) {
e.stopPropagation();
this.pristine = false;
this.dispatchEvent(new UUIInputEvent(UUIInputEvent.CHANGE));
}
protected renderPrepend() {
return html`<slot name="prepend"></slot>`;
}
protected renderAppend() {
return html`<slot name="append"></slot>`;
}
render() {
return html`
${this.renderPrepend()}
${this.autoWidth ? this.renderInputWithAutoWidth() : this.renderInput()}
${this.renderAppend()}
`;
}
private renderInputWithAutoWidth() {
return html`<div id="control">
${this.renderInput()}${this.renderAutoWidthBackground()}
</div>`;
}
renderInput() {
return html`<input
id="input"
.type=${this.type}
.value=${this.value as string}
.name=${this.name}
pattern=${ifDefined(this.pattern)}
min=${ifDefined(this.min)}
max=${ifDefined(this.max)}
step=${ifDefined(this.step)}
spellcheck=${this.spellcheck}
autocomplete=${ifDefined(this.autocomplete as any)}
placeholder=${ifDefined(this.placeholder)}
aria-label=${ifDefined(this.label)}
inputmode=${ifDefined(this.inputMode)}
?disabled=${this.disabled}
?autofocus=${this.autofocus}
?required=${this.required}
?readonly=${this.readonly}
@input=${this.onInput}
@change=${this.onChange} />`;
}
private renderAutoWidthBackground() {
return html` <div id="auto" aria-hidden="true">${this.renderText()}</div>`;
}
private renderText() {
return html`${(this.value as string).length > 0
? this.value
: this.placeholder}`;
}
static styles = [
css`
:host {
position: relative;
display: inline-flex;
align-items: stretch;
height: var(--uui-input-height, var(--uui-size-11));
text-align: left;
box-sizing: border-box;
background-color: var(
--uui-input-background-color,
var(--uui-color-surface)
);
border: var(--uui-input-border-width, 1px) solid
var(--uui-input-border-color, var(--uui-color-border));
border-radius: var(--uui-border-radius);
--uui-button-height: 100%;
--auto-width-text-margin-right: 0;
--auto-width-text-margin-left: 0;
}
#control {
position: relative;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: center;
flex-grow: 1;
}
#auto {
border: 0 1px solid transparent;
visibility: hidden;
white-space: pre;
z-index: -1;
height: 0px;
padding: 0 var(--uui-size-space-3);
margin: 0 var(--auto-width-text-margin-right) 0
var(--auto-width-text-margin-left);
}
:host([auto-width]) #input {
width: 10px;
min-width: 100%;
}
:host(:hover) {
border-color: var(
--uui-input-border-color-hover,
var(--uui-color-border-standalone)
);
}
/* TODO: Fix so we dont get double outline when there is focus on things in the slot. */
:host(:focus-within) {
border-color: var(
--uui-input-border-color-focus,
var(--uui-color-border-emphasis)
);
outline: calc(2px * var(--uui-show-focus-outline, 1)) solid
var(--uui-color-focus);
}
:host(:focus) {
border-color: var(
--uui-input-border-color-focus,
var(--uui-color-border-emphasis)
);
}
:host([disabled]) {
background-color: var(
--uui-input-background-color-disabled,
var(--uui-color-disabled)
);
border-color: var(
--uui-input-border-color-disabled,
var(--uui-color-disabled)
);
color: var(--uui-color-disabled-contrast);
}
:host([disabled]) input {
-webkit-text-fill-color: var(
--uui-color-disabled-contrast
); /* required on Safari and IOS */
}
:host([readonly]) {
background-color: var(
--uui-input-background-color-readonly,
var(--uui-color-disabled)
);
border-color: var(
--uui-input-border-color-readonly,
var(--uui-color-disabled-standalone)
);
}
:host(:not([pristine]):invalid),
/* polyfill support */
:host(:not([pristine])[internals-invalid]) {
border-color: var(--uui-color-danger);
}
input {
font-family: inherit;
padding: var(--uui-size-1) var(--uui-size-space-3);
font-size: inherit;
color: inherit;
border-radius: var(--uui-border-radius);
box-sizing: border-box;
border: none;
background: none;
width: 100%;
height: inherit;
text-align: inherit;
outline: none;
}
input[type='password']::-ms-reveal {
display: none;
}
/* TODO: make sure color looks good, or remove it as an option as we want to provide color-picker component */
input[type='color'] {
width: 30px;
padding: 0;
border: none;
}
::slotted(uui-input),
::slotted(uui-input-lock) {
height: 100%;
--uui-input-border-width: 0;
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
'uui-input': UUIInputElement;
}
}