This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtext-input.component.ts
96 lines (81 loc) · 2.43 KB
/
text-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
import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";
import {ɵgetDOM as getDOM} from '@angular/common';
export type Flavor = 'primary' | 'success' | 'warning' | 'danger' | 'info';
/**
* We must check whether the agent is Android because composition events
* behave differently between iOS and Android.
*/
function isAndroid(): boolean {
const userAgent = getDOM() ? getDOM().getUserAgent() : '';
return /android (\d+)/.test(userAgent.toLowerCase());
}
@Component({
selector: 'design-text-input',
templateUrl: './text-input.component.html',
styleUrls: ['./text-input.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
multi: true,
useExisting: TextInputComponent
}
]
})
export class TextInputComponent implements ControlValueAccessor {
public disabled = false;
@Input() placeholder: string = "";
@Input() hintText: string = "";
@Input() label: string = "";
@Input() hintColored: boolean = false;
@Input() public flavor: Flavor = 'primary';
@ViewChild(HTMLInputElement) private input?: HTMLInputElement;
// eslint-disable-next-line @angular-eslint/no-output-on-prefix
@Output() public onEnter = new EventEmitter<InputEvent>();
onChanged: ((_: any) => void) | null = null;
onTouched: (() => void) | null = null;
composing: boolean = false;
private readonly compositionMode: boolean | null;
constructor() {
if (this.compositionMode === null) {
this.compositionMode = !isAndroid();
}
}
writeValue(obj: any): void {
if(this.input) {
this.input.value = obj
}
}
registerOnChange(fn: (_: any) => void): void {
this.onChanged = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean) {
this.disabled = isDisabled;
}
getLabelColor() {
if (this.hintColored && !this.disabled && this.flavor != "primary") {
return this.flavor;
} else {
return "";
}
}
onInput(target: EventTarget | null) {
if (!this.compositionMode || (this.compositionMode && !this.composing)) {
// @ts-ignore
this.onChanged?.(target.value)
}
}
compositionStart() {
this.composing = true;
}
compositionEnd(target: EventTarget | null) {
this.composing = false;
if (this.compositionMode) {
// @ts-ignore
this.onChanged?.(target.value);
}
}
}