Skip to content

fix(datepicker): allow same start and end dates for range #19098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DefaultMatCalendarRangeStrategy<D> implements MatCalendarRangeSelec

if (start == null) {
start = date;
} else if (end == null && date && this._dateAdapter.compareDate(date, start) > 0) {
} else if (end == null && date && this._dateAdapter.compareDate(date, start) >= 0) {
end = date;
} else {
start = date;
Expand Down
38 changes: 37 additions & 1 deletion src/material/datepicker/month-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,37 @@ describe('MatMonthView', () => {
expect(testComponent.selected).toBeFalsy();
});

it('should not fire the selected change event when clicking on an already-selected ' +
'date while selecting a single date', () => {
testComponent.selected = new Date(2017, JAN, 10);
fixture.detectChanges();

expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();

const selectedCell =
monthViewNativeElement.querySelector('.mat-calendar-body-selected') as HTMLElement;
selectedCell.click();
fixture.detectChanges();

expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();
});

it('should fire the selected change event when clicking on an already-selected ' +
'date while selecting a range', () => {
const selectedDate = new Date(2017, JAN, 10);
testComponent.selected = new DateRange(selectedDate, null);
fixture.detectChanges();

expect(fixture.componentInstance.selectedChangeSpy).not.toHaveBeenCalled();

const selectedCell =
monthViewNativeElement.querySelector('.mat-calendar-body-selected') as HTMLElement;
selectedCell.click();
fixture.detectChanges();

expect(fixture.componentInstance.selectedChangeSpy).toHaveBeenCalledWith(selectedDate);
});

});
});
});
Expand Down Expand Up @@ -389,11 +420,16 @@ describe('MatMonthView', () => {


@Component({
template: `<mat-month-view [(activeDate)]="date" [(selected)]="selected"></mat-month-view>`,
template: `
<mat-month-view
[(activeDate)]="date"
[(selected)]="selected"
(selectedChange)="selectedChangeSpy($event)"></mat-month-view>`,
})
class StandardMonthView {
date = new Date(2017, JAN, 5);
selected: Date | DateRange<Date> = new Date(2017, JAN, 10);
selectedChangeSpy = jasmine.createSpy('selectedChange');
}


Expand Down