Skip to content

Commit 6481164

Browse files
authored
fix(datepicker): allow same start and end dates for range (#19098)
Allows for a range to have the same start and end dates.
1 parent de98d9c commit 6481164

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/material/datepicker/calendar-range-selection-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DefaultMatCalendarRangeStrategy<D> implements MatCalendarRangeSelec
3333

3434
if (start == null) {
3535
start = date;
36-
} else if (end == null && date && this._dateAdapter.compareDate(date, start) > 0) {
36+
} else if (end == null && date && this._dateAdapter.compareDate(date, start) >= 0) {
3737
end = date;
3838
} else {
3939
start = date;

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,37 @@ describe('MatMonthView', () => {
328328
expect(testComponent.selected).toBeFalsy();
329329
});
330330

331+
it('should not fire the selected change event when clicking on an already-selected ' +
332+
'date while selecting a single date', () => {
333+
testComponent.selected = new Date(2017, JAN, 10);
334+
fixture.detectChanges();
335+
336+
expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();
337+
338+
const selectedCell =
339+
monthViewNativeElement.querySelector('.mat-calendar-body-selected') as HTMLElement;
340+
selectedCell.click();
341+
fixture.detectChanges();
342+
343+
expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();
344+
});
345+
346+
it('should fire the selected change event when clicking on an already-selected ' +
347+
'date while selecting a range', () => {
348+
const selectedDate = new Date(2017, JAN, 10);
349+
testComponent.selected = new DateRange(selectedDate, null);
350+
fixture.detectChanges();
351+
352+
expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();
353+
354+
const selectedCell =
355+
monthViewNativeElement.querySelector('.mat-calendar-body-selected') as HTMLElement;
356+
selectedCell.click();
357+
fixture.detectChanges();
358+
359+
expect(fixture.componentInstance.selectedChangeSpy).toHaveBeenCalledWith(selectedDate);
360+
});
361+
331362
});
332363
});
333364
});
@@ -389,11 +420,16 @@ describe('MatMonthView', () => {
389420

390421

391422
@Component({
392-
template: `<mat-month-view [(activeDate)]="date" [(selected)]="selected"></mat-month-view>`,
423+
template: `
424+
<mat-month-view
425+
[(activeDate)]="date"
426+
[(selected)]="selected"
427+
(selectedChange)="selectedChangeSpy($event)"></mat-month-view>`,
393428
})
394429
class StandardMonthView {
395430
date = new Date(2017, JAN, 5);
396431
selected: Date | DateRange<Date> = new Date(2017, JAN, 10);
432+
selectedChangeSpy = jasmine.createSpy('selectedChange');
397433
}
398434

399435

0 commit comments

Comments
 (0)