Project

General

Profile

1
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
2
import {Option} from "../../../utils/indicator-utils";
3
import {AbstractControl, FormControl} from "@angular/forms";
4

    
5
@Component({
6
  selector: '[dashboard-input]',
7
  template: `
8
    <mat-form-field class="uk-width-1-1 uk-padding-remove">
9
      <input *ngIf="type === 'text'" matInput [placeholder]="label"
10
             [formControl]="formControl" [required]="required">
11
      <textarea *ngIf="type === 'textarea'" [rows]="rows" matInput [placeholder]="label" [formControl]="formControl"></textarea>
12
      <mat-select *ngIf="type === 'select'" [placeholder]="label"  [formControl]="formControl" [disableOptionCentering]="true">
13
        <mat-option *ngFor="let option of options" [value]="option.value">
14
          {{option.label}}
15
        </mat-option>
16
      </mat-select>
17
    </mat-form-field>
18
  `
19
})
20
export class InputComponent implements OnInit, OnDestroy {
21
  @Input('formInput') formControl: FormControl;
22
  @Input('type') type: string = 'text';
23
  @Input('label') label: string;
24
  @Input('rows') rows: number = 3;
25
  @Input('options') options: Option[];
26

    
27
  constructor() {
28
  }
29

    
30
  ngOnInit(): void {
31
  }
32

    
33
  ngOnDestroy(): void {
34
  }
35

    
36
  public get required(): boolean {
37
    return this.formControl && this.formControl.validator
38
      && this.formControl.validator(this.formControl)
39
      && this.formControl.validator(this.formControl).required;
40
  }
41
}
(1-1/2)