Skip to content

Commit c8d9125

Browse files
authored
docs: fix bug in datepicker-views-selection demo (#24638)
Fix issue #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 #24230
1 parent 0163ad2 commit c8d9125

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

src/components-examples/material/datepicker/datepicker-views-selection/datepicker-views-selection-example.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
55
<mat-datepicker #dp
66
startView="multi-year"
7-
(yearSelected)="chosenYearHandler($event)"
8-
(monthSelected)="chosenMonthHandler($event, dp)"
7+
(monthSelected)="setMonthAndYear($event, dp)"
98
panelClass="example-month-picker">
109
</mat-datepicker>
1110
</mat-form-field>

src/components-examples/material/datepicker/datepicker-views-selection/datepicker-views-selection-example.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,10 @@ export const MY_FORMATS = {
4949
export class DatepickerViewsSelectionExample {
5050
date = new FormControl(moment());
5151

52-
chosenYearHandler(normalizedYear: Moment) {
52+
setMonthAndYear(normalizedMonthAndYear: Moment, datepicker: MatDatepicker<Moment>) {
5353
const ctrlValue = this.date.value;
54-
ctrlValue.year(normalizedYear.year());
55-
this.date.setValue(ctrlValue);
56-
}
57-
58-
chosenMonthHandler(normalizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
59-
const ctrlValue = this.date.value;
60-
ctrlValue.month(normalizedMonth.month());
54+
ctrlValue.month(normalizedMonthAndYear.month());
55+
ctrlValue.year(normalizedMonthAndYear.year());
6156
this.date.setValue(ctrlValue);
6257
datepicker.close();
6358
}

0 commit comments

Comments
 (0)