-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.ts
358 lines (323 loc) · 8.67 KB
/
input.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
import { html } from 'lit';
import { property } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import { IgcInputBaseComponent } from './input-base.js';
import { alternateName } from '../common/decorators/alternateName.js';
import { blazorSuppress } from '../common/decorators/blazorSuppress.js';
import { blazorTwoWayBind } from '../common/decorators/blazorTwoWayBind.js';
import { watch } from '../common/decorators/watch.js';
import { registerComponent } from '../common/definitions/register.js';
import { partNameMap } from '../common/util.js';
import {
Validator,
emailValidator,
maxLengthValidator,
maxValidator,
minLengthValidator,
minValidator,
patternValidator,
requiredNumberValidator,
requiredValidator,
stepValidator,
urlValidator,
} from '../common/validators.js';
/**
* @element igc-input
*
* @slot prefix - Renders content before the input.
* @slot suffix - Renders content after input.
* @slot helper-text - Renders content below the input.
*
* @fires igcInput - Emitted when the control input receives user input.
* @fires igcChange - Emitted when the control's checked state changes.
* @fires igcFocus - Emitted when the control gains focus.
* @fires igcBlur - Emitted when the control loses focus.
*
* @csspart container - The main wrapper that holds all main input elements.
* @csspart input - The native input element.
* @csspart label - The native label element.
* @csspart prefix - The prefix wrapper.
* @csspart suffix - The suffix wrapper.
* @csspart helper-text - The helper text wrapper.
*/
export default class IgcInputComponent extends IgcInputBaseComponent {
public static readonly tagName = 'igc-input';
/* blazorSuppress */
public static register() {
registerComponent(this);
}
private get isStringType() {
return this.type !== 'number';
}
protected override validators: Validator<this>[] = [
{
...requiredValidator,
isValid: () =>
this.isStringType
? requiredValidator.isValid(this)
: requiredNumberValidator.isValid(this),
},
{
...minLengthValidator,
isValid: () =>
this.isStringType ? minLengthValidator.isValid(this) : true,
},
{
...maxLengthValidator,
isValid: () =>
this.isStringType ? maxLengthValidator.isValid(this) : true,
},
{
...minValidator,
isValid: () => (this.isStringType ? true : minValidator.isValid(this)),
},
{
...maxValidator,
isValid: () => (this.isStringType ? true : maxValidator.isValid(this)),
},
{
...stepValidator,
isValid: () => (this.isStringType ? true : stepValidator.isValid(this)),
},
{
...patternValidator,
isValid: () =>
this.isStringType ? patternValidator.isValid(this) : true,
},
{
key: 'typeMismatch',
isValid: () => {
switch (this.type) {
case 'email':
return emailValidator.isValid(this);
case 'url':
return urlValidator.isValid(this);
default:
return true;
}
},
message: () =>
(this.type === 'email'
? emailValidator.message
: urlValidator.message) as string,
},
];
protected _value = '';
/**
* The value of the control.
* @attr
*/
@property()
@blazorTwoWayBind('igcChange', 'detail')
public set value(value: string) {
this._value = value;
this.setFormValue(value ? value : null);
this.updateValidity();
this.setInvalidState();
}
public get value() {
return this._value;
}
/**
* The type attribute of the control.
* @attr
*/
@alternateName('displayType')
@property({ reflect: true })
public type:
| 'email'
| 'number'
| 'password'
| 'search'
| 'tel'
| 'text'
| 'url' = 'text';
// TODO: Deprecate
/**
* The input mode attribute of the control.
* @attr
*/
@property()
public inputmode!:
| 'none'
| 'txt'
| 'decimal'
| 'numeric'
| 'tel'
| 'search'
| 'email'
| 'url';
/**
* The pattern attribute of the control.
* @attr
*/
@property()
public pattern!: string;
/**
* The minimum string length required by the control.
* @attr minlength
*/
@property({ type: Number, attribute: 'minlength' })
public minLength!: number;
/* blazorCSSuppress */
/**
* The minlength attribute of the control.
* @prop
*
* @deprecated since v4.4.0. Use the `minLength` property instead.
*/
@property({ attribute: false })
public set minlength(value: number) {
this.minLength = value;
}
public get minlength() {
return this.minLength;
}
/**
* The maximum string length of the control.
* @attr maxlength
*/
@property({ type: Number, attribute: 'maxlength' })
public maxLength!: number;
/* blazorCSSuppress */
/**
* The maxlength attribute of the control.
* @prop
*
* @deprecated since v4.4.0. Use the `maxLength` property instead.
*/
@property({ attribute: false })
public set maxlength(value: number) {
this.maxLength = value;
}
public get maxlength() {
return this.maxLength;
}
/**
* The min attribute of the control.
* @attr
*/
@property()
public min!: number | string;
/**
* The max attribute of the control.
* @attr
*/
@property()
public max!: number | string;
/**
* The step attribute of the control.
* @attr
*/
@property({ type: Number })
public step!: number;
/**
* The autofocus attribute of the control.
* @attr
*/
@property({ type: Boolean })
public override autofocus!: boolean;
/**
* The autocomplete attribute of the control.
* @attr
*/
@property()
public autocomplete!: string;
/**
* @internal
*/
@property({ type: Number })
public override tabIndex = 0;
@watch('min', { waitUntilFirstUpdate: true })
@watch('max', { waitUntilFirstUpdate: true })
@watch('minLength', { waitUntilFirstUpdate: true })
@watch('maxLength', { waitUntilFirstUpdate: true })
@watch('pattern', { waitUntilFirstUpdate: true })
@watch('step', { waitUntilFirstUpdate: true })
protected constraintsChanged() {
this.updateValidity();
}
/** @hidden */
public override connectedCallback() {
super.connectedCallback();
this.setFormValue(this._value ? this._value : null);
this.updateValidity();
}
/** Replaces the selected text in the input. */
@blazorSuppress()
public override setRangeText(
replacement: string,
start: number,
end: number,
selectMode: 'select' | 'start' | 'end' | 'preserve' = 'preserve'
) {
super.setRangeText(replacement, start, end, selectMode);
this.value = this.input.value;
}
/** Selects all text within the input. */
public select() {
return this.input.select();
}
/** Increments the numeric value of the input by one or more steps. */
public stepUp(n?: number) {
this.input.stepUp(n);
this.value = this.input.value;
}
/** Decrements the numeric value of the input by one or more steps. */
public stepDown(n?: number) {
this.input.stepDown(n);
this.value = this.input.value;
}
private handleInput() {
this.value = this.input.value;
this.emitEvent('igcInput', { detail: this.value });
}
private handleChange() {
this.value = this.input.value;
this.emitEvent('igcChange', { detail: this.value });
}
protected override handleFocus(): void {
this._dirty = true;
super.handleFocus();
}
protected override handleBlur(): void {
this.checkValidity();
super.handleBlur();
}
protected renderInput() {
return html`
<input
id=${this.inputId}
part=${partNameMap(this.resolvePartNames('input'))}
name=${ifDefined(this.name)}
type=${ifDefined(this.type)}
pattern=${ifDefined(this.pattern)}
placeholder=${ifDefined(this.placeholder)}
.value=${live(this.value)}
?readonly=${this.readOnly}
?disabled=${this.disabled}
?required=${this.required}
?autofocus=${this.autofocus}
tabindex=${this.tabIndex}
autocomplete=${ifDefined(this.autocomplete as any)}
inputmode=${ifDefined(this.inputmode)}
min=${ifDefined(this.min)}
max=${ifDefined(this.max)}
minlength=${ifDefined(this.minLength)}
maxlength=${ifDefined(this.maxLength)}
step=${ifDefined(this.step)}
aria-invalid=${this.invalid ? 'true' : 'false'}
@change=${this.handleChange}
@input=${this.handleInput}
@focus=${this.handleFocus}
@blur=${this.handleBlur}
/>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-input': IgcInputComponent;
}
}