Datepicker Angular Material improved

September 10, 2021 · 1 min readLucian Ciobanu
Tags:AngularAngularMaterial

Following the implementation of Datepicker from Angular Material you will have an input field where the user can either input manually the date or click on the calendar to have a small dropdown with the dates of the respective month and the option to browse through months.

Now, for most of the projects I've implemented, we're mostly aiming to create an UX which only alows the user to click on the field to trigger the month dropdown and select the day from there and thus also creating a simple validation so that the user only inputs a date.

I'm happy to put down a way to create such a date picker:

<mat-form-field>
  <mat-label>Date</mat-label>
  <input matInput [formControl]="dateControl" [matDatepicker]="picker" (focus)="picker.open()" readonly>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

I think the awesome part is in triggering picker.open() on (focus) and the readonly attribute.

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery