Skip to content

Commit

Permalink
docs: fix bug in datepicker-views-selection demo
Browse files Browse the repository at this point in the history
Fix issue angular#24230 in the Datepicker Views Selection code demo. When selecting a month, set the year
on the form value to the active year on the datepicker.

Previously, this demo would only set the year when clicking on a year in the datepicker, but this
did not account for changing the year with the next/previous year buttons.

Fixes angular#24230
  • Loading branch information
zarend committed Mar 21, 2022
1 parent 9371606 commit 8cb9828
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
<mat-datepicker #dp
startView="multi-year"
(yearSelected)="chosenYearHandler($event)"
(monthSelected)="chosenMonthHandler($event, dp)"
(monthSelected)="setMonthAndYear($event, dp)"
panelClass="example-month-picker">
</mat-datepicker>
</mat-form-field>
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,10 @@ export const MY_FORMATS = {
export class DatepickerViewsSelectionExample {
date = new FormControl(moment());

chosenYearHandler(normalizedYear: Moment) {
setMonthAndYear(normalizedMonthAndYear: Moment, datepicker: MatDatepicker<Moment>) {
const ctrlValue = this.date.value;
ctrlValue.year(normalizedYear.year());
this.date.setValue(ctrlValue);
}

chosenMonthHandler(normalizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
const ctrlValue = this.date.value;
ctrlValue.month(normalizedMonth.month());
ctrlValue.month(normalizedMonthAndYear.month());
ctrlValue.year(normalizedMonthAndYear.year());
this.date.setValue(ctrlValue);
datepicker.close();
}
Expand Down

0 comments on commit 8cb9828

Please sign in to comment.