-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathabp-input.component.ts
More file actions
106 lines (93 loc) · 2.47 KB
/
Copy pathabp-input.component.ts
File metadata and controls
106 lines (93 loc) · 2.47 KB
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
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
DestroyRef,
forwardRef,
inject,
OnInit,
input,
Injector
} from '@angular/core';
import {
AbstractControl,
ControlValueAccessor,
FormBuilder,
FormControl,
FormControlName,
FormGroup,
FormGroupDirective,
NG_VALUE_ACCESSOR,
NgControl,
ReactiveFormsModule,
} from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { LocalizationPipe } from '@abp/ng.core';
const ABP_INPUT_CONTROL_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => AbpInputComponent),
multi: true,
};
@Component({
selector: 'abp-input',
templateUrl: './abp-input.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ReactiveFormsModule, LocalizationPipe],
exportAs: 'abpInput',
host: {
class: 'abp-input',
},
providers: [ABP_INPUT_CONTROL_VALUE_ACCESSOR],
})
export class AbpInputComponent implements OnInit, ControlValueAccessor {
label = input.required<string>();
type = input<'text' | 'number' | 'password'>('text');
id = input<string>('');
placeholder = input<string>('');
control: FormControl;
readonly formBuilder = inject(FormBuilder);
readonly changeDetectorRef = inject(ChangeDetectorRef);
readonly destroyRef = inject(DestroyRef);
readonly injector = inject(Injector);
abpInputFormGroup: FormGroup;
ngOnInit() {
const ngControl = this.injector.get(NgControl, null);
if (ngControl) {
this.control = this.injector.get(FormGroupDirective).getControl(ngControl as FormControlName);
}
this.abpInputFormGroup = this.formBuilder.group({
value: [''],
});
this.value.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(val => {
this.onChange(val);
});
}
writeValue(value: any[]): void {
this.value.setValue(value);
this.changeDetectorRef.markForCheck();
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
if (isDisabled) {
this.value.disable();
} else {
this.value.enable();
}
}
get errors(): string[] {
if (this.control && this.control.errors) {
return []
}
return []
}
get value(): AbstractControl<any> {
return this.abpInputFormGroup.get('value');
}
private onChange: (value: any) => void = () => {};
private onTouched: () => void = () => {};
}