import { Component, Input, OnInit } from '@angular/core'; import { AbstractControl } from '@angular/forms'; import { JsonSchemaFormService } from '../../json-schema-form.service'; import { dateToString, stringToDate } from '../../shared'; import { MatDatepickerInputEvent } from '@angular/material/datepicker'; @Component({ // tslint:disable-next-line:component-selector selector: 'material-datepicker-widget', template: ` `, styles: [` mat-error { font-size: 75%; margin-top: -1rem; margin-bottom: 0.5rem; } ::ng-deep json-schema-form mat-form-field .mat-form-field-wrapper .mat-form-field-flex .mat-form-field-infix { width: initial; } `], }) export class MaterialDatepickerComponent implements OnInit { formControl: AbstractControl; controlName: string; controlValue: any; dateValue: any; controlDisabled = false; boundControl = false; options: any; autoCompleteList: string[] = []; @Input() layoutNode: any; @Input() layoutIndex: number[]; @Input() dataIndex: number[]; constructor( private jsf: JsonSchemaFormService ) { } ngOnInit() { this.options = this.layoutNode.options || {}; this.jsf.initializeControl(this, !this.options.readonly); if (this.controlValue) { this.setDate(dateToString(new Date(this.controlValue))); } if (!this.options.notitle && !this.options.description && this.options.placeholder) { this.options.description = this.options.placeholder; } } updateValue(event: MatDatepickerInputEvent ) { this.options.showErrors = true; if (event.value) { this.setDate(dateToString(event.value)); } } setDate(date: string) { this.formControl.setValue(date, this.options); } }