openaire-library/dashboard/sharedComponents/input/input.component.ts

118 lines
4.0 KiB
TypeScript
Raw Normal View History

import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild} from "@angular/core";
import {AbstractControl} from "@angular/forms";
import {HelperFunctions} from "../../../utils/HelperFunctions.class";
import {Subscription} from "rxjs";
import {MatSelect} from "@angular/material/select";
export interface Option {
icon?: string,
iconClass?: string,
value: any,
label: string
}
@Component({
selector: '[dashboard-input]',
template: `
<div *ngIf="label"
class="uk-text-bold uk-form-label uk-margin-small-bottom">{{label + (required ? ' *' : '')}}</div>
<div *ngIf="hint" class="uk-margin-bottom uk-text-small uk-form-hint">{{hint}}</div>
<div class="uk-grid" uk-grid>
<ng-content></ng-content>
<div class="uk-width-expand@m" [class.uk-flex-first]="!extraLeft">
<ng-template [ngIf]="type === 'text'">
<input class="uk-input input-box uk-text-small" [placeholder]="placeholder" [formControl]="formControl"
[class.uk-form-danger]="formControl.invalid && formControl.touched">
</ng-template>
<ng-template [ngIf]="type === 'textarea'">
<textarea class="uk-textarea input-box uk-text-small" [rows]="rows" [placeholder]="placeholder"
[formControl]="formControl" [class.uk-form-danger]="formControl.invalid && formControl.touched">
</textarea>
</ng-template>
<ng-template [ngIf]="type === 'select'">
<div class="input-box clickable" [class.uk-form-danger]="formControl.invalid && formControl.touched" (click)="openSelect()">
<mat-form-field class="uk-width-1-1">
<mat-select #select *ngIf="type === 'select'" [required]="required" [value]="null"
(openedChange)="stopPropagation()" [formControl]="formControl" [disableOptionCentering]="true">
<mat-option *ngIf="placeholder && formControl.value === ''" [value]="''">{{placeholder}}</mat-option>
<mat-option *ngFor="let option of options" [value]="option.value">
{{option.label}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</ng-template>
</div>
</div>
<mat-checkbox *ngIf="type === 'checkbox'" [formControl]="formControl">{{label}}</mat-checkbox>
`
})
export class InputComponent implements OnInit, OnDestroy, OnChanges {
@Input('formInput') formControl: AbstractControl;
@Input('type') type: 'text' | 'textarea' | 'select' | 'checkbox' = 'text';
@Input('label') label: string;
@Input('rows') rows: number = 3;
@Input('options') options: Option[];
@Input('hint') hint = null;
@Input('placeholder') placeholder = '';
@ViewChild('select') select: MatSelect;
@Input() extraLeft: boolean = true;
public required: boolean = false;
private initValue: any;
private subscriptions: any[] = [];
constructor() {
}
ngOnInit(): void {
this.reset();
}
ngOnChanges(changes: SimpleChanges) {
if(changes.formControl) {
this.reset();
}
}
reset() {
this.unsubscribe();
this.initValue = HelperFunctions.copy(this.formControl.value);
if (this.formControl && this.formControl.validator) {
let validator = this.formControl.validator({} as AbstractControl);
this.required = (validator && validator.required);
}
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
value = (value === '')?null:value;
if(this.initValue === value) {
this.formControl.markAsPristine();
}
}));
if (!this.formControl.value) {
this.formControl.setValue('');
}
}
unsubscribe() {
this.subscriptions.forEach(subscription => {
if(subscription instanceof Subscription) {
subscription.unsubscribe();
}
});
}
openSelect() {
if(this.select) {
this.select.open();
}
}
ngOnDestroy(): void {
this.unsubscribe();
}
stopPropagation() {
event.stopPropagation();
}
}