1
|
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
|
2
|
import {AbstractControl} from "@angular/forms";
|
3
|
import {HelperFunctions} from "../../../utils/HelperFunctions.class";
|
4
|
|
5
|
|
6
|
export interface Option {
|
7
|
icon?: string,
|
8
|
iconClass?: string,
|
9
|
value: any,
|
10
|
label: string
|
11
|
}
|
12
|
|
13
|
@Component({
|
14
|
selector: '[dashboard-input]',
|
15
|
template: `
|
16
|
<mat-form-field *ngIf="type != 'checkbox'" class="uk-width-1-1 uk-padding-remove">
|
17
|
<input *ngIf="type === 'text'" matInput [placeholder]="label"
|
18
|
[formControl]="formControl" [required]="required">
|
19
|
<textarea *ngIf="type === 'textarea'" [rows]="rows" matInput
|
20
|
[placeholder]="label" [formControl]="formControl" [required]="required"></textarea>
|
21
|
<mat-select *ngIf="type === 'select'" [placeholder]="label" [required]="required"
|
22
|
(openedChange)="stopPropagation()" [formControl]="formControl" [disableOptionCentering]="true">
|
23
|
<mat-option *ngFor="let option of options" [value]="option.value">
|
24
|
{{option.label}}
|
25
|
</mat-option>
|
26
|
</mat-select>
|
27
|
|
28
|
</mat-form-field>
|
29
|
<mat-checkbox *ngIf="type === 'checkbox'" [formControl]="formControl" >{{label}}</mat-checkbox>
|
30
|
`
|
31
|
})
|
32
|
export class InputComponent implements OnInit, OnDestroy {
|
33
|
@Input('formInput') formControl: AbstractControl;
|
34
|
@Input('type') type: string = 'text';
|
35
|
@Input('label') label: string;
|
36
|
@Input('rows') rows: number = 3;
|
37
|
@Input('options') options: Option[];
|
38
|
private initValue: any;
|
39
|
|
40
|
constructor() {
|
41
|
}
|
42
|
|
43
|
ngOnInit(): void {
|
44
|
this.initValue = HelperFunctions.copy(this.formControl.value);
|
45
|
this.formControl.valueChanges.subscribe(value => {
|
46
|
value = (value === '')?null:value;
|
47
|
if(this.initValue === value) {
|
48
|
this.formControl.markAsPristine();
|
49
|
}
|
50
|
});
|
51
|
}
|
52
|
|
53
|
ngOnDestroy(): void {
|
54
|
}
|
55
|
|
56
|
public get required(): boolean {
|
57
|
return this.formControl && this.formControl.validator
|
58
|
&& this.formControl.validator(this.formControl)
|
59
|
&& this.formControl.validator(this.formControl).required;
|
60
|
}
|
61
|
|
62
|
stopPropagation() {
|
63
|
if(event) {
|
64
|
event.stopPropagation();
|
65
|
}
|
66
|
}
|
67
|
}
|