I am using flatpickr module for date selection in my input forms. This input form is within a Angular mat-dialog.
Facing following issues with it:
1. Dropdown calendar opens up itself when mat-dialog is opened. It should only open when mouse click event occurs inside the form-element in mat-dialog. Mat-Dialog-GUI
2. On date selection, value binded to the angular variable is not correct. It takes the last selected value instead of the current one. When calendar opens by itself on first time loading of dialog, on date selection "3rd Sep", the value of element is binded as "null" instead of the selected calendar date. Then, again when date "5th Sep" is selected, value is binded as "3rd Sep" instead of 5th. Then, when "7th Sep" is selected, value is binded as 5th and so on.
Browser Console Logs
Here are the snippets of HTML and TS code:
`<div class="container" *ngIf="(this.data.tabletype=='holidays')">
<h3 mat-dialog-title>Add new holiday</h3>
<form class="mat-dialog-content" (ngSubmit)="confirmAdd()" #formControl="ngForm">
<!--Contains mat-hint for characters count and has maxLength set-->
<div class="form">
<mat-form-field color="accent" appearance="outline">
<mat-label>Holiday Date</mat-label>
<!--<input matInput #holidayfield1 (change)="onDateSelect($event)" class="form-control" type="date" placeholder="Holiday Date" [(ngModel)]="holiday.holidayDate" name="holidayDate" required>-->
<input matInput id="datepicker"
class="form-control"
type="text"
name="holidayDate"
mwlFlatpickr
[(ngModel)]="holiday.holidayDate"
[altInput]="true"
[convertModelValue]="true"
[enableTime]="false"
(change)="onDateSelect()"
dateFormat="YYYY-mm-dd"
altFormat="F j, Y"
placeholder="Holiday Date"
required
>
<mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
</div>
<div class="form">
<mat-form-field color="accent" appearance="outline">
<mat-label>Week Day</mat-label>
<input matInput #holidayfield2 class="form-control" type="text" placeholder="Week Day" [(ngModel)]="holiday.weekDayName" name="weekDayName" required>
<mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
</mat-form-field>
</div>
<div class="form">
<mat-form-field color="accent" appearance="outline">
<mat-label>Description</mat-label>
<input matInput #holidayfield3 class="form-control" type="text" placeholder="Description" [(ngModel)]="holiday.description" name="description" required maxlength="256">
<mat-error *ngIf="formControl.invalid">{{getErrorMessage()}}</mat-error>
<mat-hint align="end">{{holidayfield3.value?.length || 0}}/256</mat-hint>
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-raised-button color="primary" [type]="submit" [disabled]="!formControl.valid" [mat-dialog-close]="1">Save</button>
<button mat-raised-button color="primary" type="button" (click)="onNoClick()" tabindex="-1">Cancel</button>
</div>
onDateSelect() {
let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
console.log(this.holiday.holidayDate);
let d = new Date(this.holiday.holidayDate);
console.log(d);
console.log(d.getDay());
let dayName = days[d.getDay()];
this.holiday.weekDayName = dayName;
}
Issue solved. Problem was that with the order in which events were triggered for <input>
tag. (click)
event was triggered before [(ngModel)]
binding. As a result, values were not available in onDateSelect()
event handler as model binding did not happen till then.
Replaced (click)
with (ngModelChange)
and it worked completely fine.
<input matInput id="datepicker"
class="form-control"
type="text"
name="holidayDate"
mwlFlatpickr
[(ngModel)]="holiday.holidayDate"
(ngModelChange)="onDateSelect()"
[altInput]="false"
[convertModelValue]="false"
[enableTime]="false"
defaultDate="2016-10-20"
dateFormat="Y-m-d"
altFormat="F j, Y"
placeholder="Holiday Date"
required
>