2019-12-23 14:59:56 +01:00
|
|
|
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
|
|
|
|
import {AbstractControl} from "@angular/forms";
|
|
|
|
import {HelperFunctions} from "../../../utils/HelperFunctions.class";
|
|
|
|
|
2019-12-23 16:17:35 +01:00
|
|
|
|
|
|
|
export interface Option {
|
|
|
|
icon?: string,
|
|
|
|
iconClass?: string,
|
|
|
|
value: any,
|
|
|
|
label: string
|
|
|
|
}
|
|
|
|
|
2019-12-23 14:59:56 +01:00
|
|
|
@Component({
|
|
|
|
selector: '[dashboard-input]',
|
|
|
|
template: `
|
|
|
|
<mat-form-field *ngIf="type != 'checkbox'" class="uk-width-1-1 uk-padding-remove">
|
|
|
|
<input *ngIf="type === 'text'" matInput [placeholder]="label"
|
|
|
|
[formControl]="formControl" [required]="required">
|
|
|
|
<textarea *ngIf="type === 'textarea'" [rows]="rows" matInput
|
2020-05-14 15:02:25 +02:00
|
|
|
[placeholder]="label" [formControl]="formControl" [required]="required"></textarea>
|
|
|
|
<mat-select *ngIf="type === 'select'" [placeholder]="label" [required]="required"
|
2019-12-23 14:59:56 +01:00
|
|
|
(openedChange)="stopPropagation()" [formControl]="formControl" [disableOptionCentering]="true">
|
|
|
|
<mat-option *ngFor="let option of options" [value]="option.value">
|
|
|
|
{{option.label}}
|
|
|
|
</mat-option>
|
|
|
|
</mat-select>
|
|
|
|
|
|
|
|
</mat-form-field>
|
|
|
|
<mat-checkbox *ngIf="type === 'checkbox'" [formControl]="formControl" >{{label}}</mat-checkbox>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
export class InputComponent implements OnInit, OnDestroy {
|
|
|
|
@Input('formInput') formControl: AbstractControl;
|
2020-06-05 16:48:12 +02:00
|
|
|
@Input('type') type: 'text' | 'textarea' | 'select' | 'checkbox' = 'text';
|
2019-12-23 14:59:56 +01:00
|
|
|
@Input('label') label: string;
|
|
|
|
@Input('rows') rows: number = 3;
|
|
|
|
@Input('options') options: Option[];
|
2020-06-05 16:48:12 +02:00
|
|
|
public required: boolean = false;
|
2019-12-23 14:59:56 +01:00
|
|
|
private initValue: any;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.initValue = HelperFunctions.copy(this.formControl.value);
|
2020-06-05 16:48:12 +02:00
|
|
|
if(this.formControl && this.formControl.validator) {
|
|
|
|
let validator = this.formControl.validator({} as AbstractControl);
|
|
|
|
this.required = (validator && validator.required);
|
|
|
|
}
|
2019-12-23 14:59:56 +01:00
|
|
|
this.formControl.valueChanges.subscribe(value => {
|
2020-01-14 20:49:17 +01:00
|
|
|
value = (value === '')?null:value;
|
2019-12-23 14:59:56 +01:00
|
|
|
if(this.initValue === value) {
|
|
|
|
this.formControl.markAsPristine();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
}
|
|
|
|
|
|
|
|
stopPropagation() {
|
|
|
|
if(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|