-
Notifications
You must be signed in to change notification settings - Fork 8
/
input.component.ts
292 lines (253 loc) · 8.31 KB
/
input.component.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
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
forwardRef,
HostListener,
Input,
OnChanges,
OnInit,
Output,
Renderer2,
SimpleChanges,
ViewChild
} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
const GTX_INPUT_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => InputField),
multi: true
};
/**
* E-mail validator regex from Angular 8
* @todo Implement with validators
* @see https://github.com/angular/angular/blob/8.2.9/packages/forms/src/validators.ts#L60
*/
const EMAIL_REGEXP =
"^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$";
/**
* Telephone number validator regex
* @todo Implement with validators
* @see https://stackoverflow.com/a/26516985
*/
const TEL_REGEXP = "^([()\\- x+]*\d[()\\- x+]*){4,16}$";
/**
* URL validator regex
* @todo Implement with validators
* @see https://stackoverflow.com/a/52017332
*/
const URL_REGEXP = "(^|\\s)((https?:\\/\\/)?[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?)";
const ACTIVE_CLASS = 'active';
/**
* The InputField wraps the native `<input>` form element but should only be used for
* text, number, password, tel, email or url types. Other types (date, range, file) should have dedicated components.
*
*
* Note that the class is named `InputField` since `Input` is used by the Angular framework to denote
* component inputs.
*
* ```html
* <gtx-input label="Text Input Label"></gtx-input>
* <gtx-input placeholder="Number Input Placeholder"
* type="number" min="0" max="100" step="5"></gtx-input>
* ```
*/
@Component({
selector: 'gtx-input',
templateUrl: './input.tpl.html',
providers: [GTX_INPUT_VALUE_ACCESSOR]
})
export class InputField implements AfterViewInit, ControlValueAccessor, OnInit, OnChanges {
/**
* Sets autocomplete attribute on the input field
*/
@Input() autocomplete: string;
/**
* Sets the input field to be auto-focused. Handled by `AutofocusDirective`.
*/
@Input() autofocus: boolean = false;
/**
* Sets the disabled state
*/
@Input() disabled: boolean = false;
/**
* Input field id
*/
@Input() id: string;
/**
* A label for the input
*/
@Input() label: string = '';
/**
* Max allowed value (applies when type = "number")
*/
@Input() max: number;
/**
* Min allowed value (applies when type = "number")
*/
@Input() min: number;
/**
* Max allowed length in characters
*/
@Input() maxlength: number;
/**
* Input field name
*/
@Input() name: string;
/**
* Regex pattern for complex validation
*/
@Input() pattern: string;
/**
* Placeholder text to display when the field is empty
*/
@Input() placeholder: string;
/**
* Sets the readonly state of the input
*/
@Input() readonly: boolean = false;
/**
* Sets the required state of the input
*/
@Input() required: boolean = false;
/**
* Increment step (applies when type = "number")
*/
@Input() step: number;
/**
* Can be "text", "number", "password", "tel", "email" or "url".
*/
@Input() type: 'text' | 'number' | 'password' | 'tel' | 'email' | 'url' = 'text';
/**
* Sets the value of the input.
*/
@Input() value: string|number = '';
/**
* Fires when the input loses focus.
*/
@Output() blur = new EventEmitter<string|number>();
/**
* Fires when the input gains focus.
*/
@Output() focus = new EventEmitter<string|number>();
/**
* Fires whenever a char is entered into the field.
*/
@Output() change = new EventEmitter<string|number>();
@ViewChild('inputElement', { static: true }) private inputElement: ElementRef;
@ViewChild('labelElement', { static: true }) private labelElement: ElementRef;
private currentValue: string | number;
constructor(private renderer: Renderer2,
private changeDetector: ChangeDetectorRef) { }
ngOnInit(): void {
/**
* Set default regex patterns for specific field types if not set
*/
if (!this.pattern) {
switch (this.type) {
case 'email':
this.pattern = EMAIL_REGEXP;
break;
case 'tel':
this.pattern = TEL_REGEXP;
break;
case 'url':
this.pattern = URL_REGEXP;
break;
default:
}
}
}
/**
* The Materialize input includes a dynamic label that changes position depending on the state of the input.
* When the label has the "active" class, it moves above the input, otherwise it resides inside the input
* itself.
*
* The Materialize "forms.js" script normally takes care of adding/removing the active class on page load,
* but this does not work in a SPA setting where new views with inputs can be created without a page load
* event to trigger the `Materialize.updateTextFields()` method. Therefore we need to handle it ourselves
* when the input component is created.
*/
ngAfterViewInit(): void {
const input: HTMLInputElement = this.inputElement.nativeElement;
const label: HTMLLabelElement = this.labelElement.nativeElement;
if (input && label) {
if (String(this.value).length > 0 || this.placeholder) {
this.renderer.addClass(label, ACTIVE_CLASS);
} else {
this.renderer.removeClass(label, ACTIVE_CLASS);
}
}
}
ngOnChanges(changes: SimpleChanges): void {
const valueChange = changes['value'];
if (valueChange) {
this.writeValue(valueChange.currentValue);
}
}
@HostListener('keydown', ['$event'])
public onKeyDown(event) {
if (this.type === 'number') {
const keyboardEvent = event as KeyboardEvent;
if ((keyboardEvent.key === '-' && event.target.value) || keyboardEvent.key === '+') {
keyboardEvent.preventDefault();
} else if (keyboardEvent.key === '-' && !event.target.value) {
event.target.value = '-';
}
}
}
onBlur(e: Event): void {
e.stopPropagation();
const target = e.target as HTMLInputElement;
this.blur.emit(this.normalizeValue(target.value));
this.onTouched();
}
onFocus(e: Event): void {
const target = e.target as HTMLInputElement;
this.focus.emit(this.normalizeValue(target.value));
}
onInput(e: Event): void {
const target = e.target as HTMLInputElement;
this.updateValue(target);
const value = this.currentValue = this.normalizeValue(target.value);
this.onChange(value);
this.change.emit(value);
}
writeValue(valueToWrite: any): void {
const value = this.normalizeValue(valueToWrite);
if (value !== this.currentValue) {
this.renderer.setProperty(this.inputElement.nativeElement, 'value', this.currentValue = value);
}
}
// ValueAccessor members
registerOnChange(fn: (newValue: string | number) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(disabled: boolean): void {
this.disabled = disabled;
this.changeDetector.markForCheck();
}
private onChange = (newValue: string | number): void => {};
private onTouched = (): void => {};
private normalizeValue(val: any): string|number {
if (this.type === 'number') {
return val == null ? 0 : Number(val);
} else {
return val == null ? '' : String(val);
}
}
private updateValue(target: HTMLInputElement): void {
if (this.type === 'number') {
if (this.max && Number(target.value) > this.max) {
target.value = String(this.max);
} else if (this.min && Number(target.value) < this.min) {
target.value = String(this.min);
}
}
}
}