Skip to content

Commit 125475f

Browse files
authored
fix(datepicker): user selection event not emitting value when clicking on the selected date (#19211)
If the user clicks on the selected date, we were emitting null instead of the actual value.
1 parent 5dd582c commit 125475f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/material/datepicker/month-view.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ describe('MatMonthView', () => {
384384
expect(fixture.componentInstance.selectedChangeSpy).toHaveBeenCalledWith(selectedDate);
385385
});
386386

387+
it('should fire the _userSelection event with the correct value when clicking ' +
388+
'on a selected date', () => {
389+
const date = new Date(2017, JAN, 10);
390+
testComponent.selected = date;
391+
fixture.detectChanges();
392+
393+
expect(fixture.componentInstance.userSelectionSpy).not.toHaveBeenCalled();
394+
395+
const selectedCell =
396+
monthViewNativeElement.querySelector('.mat-calendar-body-selected') as HTMLElement;
397+
selectedCell.click();
398+
fixture.detectChanges();
399+
400+
expect(fixture.componentInstance.userSelectionSpy).toHaveBeenCalledWith(
401+
jasmine.objectContaining({value: date}));
402+
});
387403
});
388404
});
389405
});
@@ -449,12 +465,14 @@ describe('MatMonthView', () => {
449465
<mat-month-view
450466
[(activeDate)]="date"
451467
[(selected)]="selected"
452-
(selectedChange)="selectedChangeSpy($event)"></mat-month-view>`,
468+
(selectedChange)="selectedChangeSpy($event)"
469+
(_userSelection)="userSelectionSpy($event)"></mat-month-view>`,
453470
})
454471
class StandardMonthView {
455472
date = new Date(2017, JAN, 5);
456473
selected: Date | DateRange<Date> = new Date(2017, JAN, 10);
457474
selectedChangeSpy = jasmine.createSpy('selectedChange');
475+
userSelectionSpy = jasmine.createSpy('userSelection');
458476
}
459477

460478

src/material/datepicker/month-view.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
204204
/** Handles when a new date is selected. */
205205
_dateSelected(event: MatCalendarUserEvent<number>) {
206206
const date = event.value;
207-
let selectedDate: D | null = null;
207+
const selectedYear = this._dateAdapter.getYear(this.activeDate);
208+
const selectedMonth = this._dateAdapter.getMonth(this.activeDate);
209+
const selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date);
208210
let rangeStartDate: number | null;
209211
let rangeEndDate: number | null;
210212

@@ -216,9 +218,6 @@ export class MatMonthView<D> implements AfterContentInit, OnDestroy {
216218
}
217219

218220
if (rangeStartDate !== date || rangeEndDate !== date) {
219-
const selectedYear = this._dateAdapter.getYear(this.activeDate);
220-
const selectedMonth = this._dateAdapter.getMonth(this.activeDate);
221-
selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date);
222221
this.selectedChange.emit(selectedDate);
223222
}
224223

0 commit comments

Comments
 (0)