angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.32k stars 6.73k forks source link

bug(mat-datepicker): calendar with startAt attribute will not update on manual input #28814

Open SerjaSnow opened 6 months ago

SerjaSnow commented 6 months ago

Is this a regression?

The previous version in which this bug was not present was

No response

Description

I built a mat-datepicker with startAt property (e.g. 01/01/2024). Calendar starts as described in documentation at demanded calendar page. If input value is changed, calendar will not update to desired page. (e.g. date is changed to 05/05/2024, calendar page is still on January 2024 instead of May 2024)

Reproduction

StackBlitz link: https://stackblitz.com/edit/datepicker-property-error Steps to reproduce:

  1. Change input to any other date (e.g. 5/5/2024) either by typing or selection on calendar.
  2. (Re-)Open calendar.

Expected Behavior

Calendar should switch to page of desired month (e.g. May 2024). startAt property should only set initial date and react accordingly to changes.

Actual Behavior

Calendar does not change its page according to the input date. It remains on startAt date.

Environment

lsamboretrorabbit commented 6 months ago

To ensure that the calendar page updates to the desired month when the input value changes, you can utilize Angular's two-way data binding with the [(ngModel)] directive. Here's how you can modify your code to achieve this:

<input matInput [(ngModel)]="selectedDate" [matDatepicker]="datepicker" />

Your TS file for the html page will include a variable

export class DatepickerExampleComponent { // Define a variable to hold the selected date value selectedDate: Date = new Date('2024-01-01'); }

SerjaSnow commented 5 months ago

That doesn't make any difference... Apart from not fixing the problem, I already use reactive forms. Why should I use template-driven here?

jeandat commented 3 weeks ago

This is not related to the "startAt" property nor limited to angular 17. I'm also interested by a fix for this bug. I can reproduce it with ng 15 very easily like demonstrated here.

Regarding the P4 classification, it seems to me too low. Wanting the view to be uptodate after a change looks somewhat normal and basic.

In the meantime, is there a smart workaround by chance? For the moment, I'm changing the "activeDate" property on the calendar manually as a workaround.