Skip to content

Commit 9ba5d84

Browse files
hwesol13andrewseguin
authored andcommitted
feat(datepicker): close calendar after choose the same date again (#6323)
* feat: close calendar after choose the same date again * feat: close calendar by userSelection event * addressing pr comments * fix for tslint errors * addressing pr comments
1 parent 66b1ff5 commit 9ba5d84

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

src/lib/datepicker/calendar.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
[activeDate]="_activeDate"
4949
[selected]="selected"
5050
[dateFilter]="_dateFilterForViews"
51-
(selectedChange)="_dateSelected($event)">
51+
(selectedChange)="_dateSelected($event)"
52+
(userSelection)="_userSelected()">
5253
</md-month-view>
5354

5455
<md-year-view

src/lib/datepicker/calendar.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
8080
/** Emits when the currently selected date changes. */
8181
@Output() selectedChange = new EventEmitter<D>();
8282

83+
/** Emits when any date is selected. */
84+
@Output() userSelection = new EventEmitter<void>();
85+
8386
/** Date filter for the month and year views. */
8487
_dateFilterForViews = (date: D) => {
8588
return !!date &&
@@ -159,6 +162,10 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
159162
}
160163
}
161164

165+
_userSelected(): void {
166+
this.userSelection.emit();
167+
}
168+
162169
/** Handles month selection in the year view. */
163170
_monthSelected(month: D): void {
164171
this._activeDate = month;

src/lib/datepicker/datepicker-content.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
[maxDate]="datepicker._maxDate"
77
[dateFilter]="datepicker._dateFilter"
88
[selected]="datepicker._selected"
9-
(selectedChange)="datepicker._selectAndClose($event)">
9+
(selectedChange)="datepicker._select($event)"
10+
(userSelection)="datepicker.close()">
1011
</md-calendar>

src/lib/datepicker/datepicker.spec.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ describe('MdDatepicker', () => {
189189
});
190190
});
191191

192+
it('clicking the currently selected date should close the calendar ' +
193+
'without firing selectedChanged', () => {
194+
const selectedChangedSpy =
195+
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
196+
for (let changeCount = 1; changeCount < 3; changeCount++) {
197+
const currentDay = changeCount;
198+
testComponent.datepicker.open();
199+
fixture.detectChanges();
200+
201+
expect(document.querySelector('md-datepicker-content')).not.toBeNull();
202+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, currentDay));
203+
204+
let cells = document.querySelectorAll('.mat-calendar-body-cell');
205+
dispatchMouseEvent(cells[1], 'click');
206+
fixture.detectChanges();
207+
}
208+
209+
fixture.whenStable().then(() => {
210+
expect(selectedChangedSpy.calls.count()).toEqual(1);
211+
expect(document.querySelector('md-dialog-container')).toBeNull();
212+
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2));
213+
});
214+
});
215+
192216
it('startAt should fallback to input value', () => {
193217
expect(testComponent.datepicker.startAt).toEqual(new Date(2020, JAN, 1));
194218
});
@@ -361,7 +385,7 @@ describe('MdDatepicker', () => {
361385
expect(testComponent.datepickerInput.value).toBeNull();
362386

363387
let selected = new Date(2017, JAN, 1);
364-
testComponent.datepicker._selectAndClose(selected);
388+
testComponent.datepicker._select(selected);
365389
fixture.detectChanges();
366390

367391
fixture.whenStable().then(() => {
@@ -388,7 +412,7 @@ describe('MdDatepicker', () => {
388412

389413
expect(inputEl.classList).toContain('ng-pristine');
390414

391-
testComponent.datepicker._selectAndClose(new Date(2017, JAN, 1));
415+
testComponent.datepicker._select(new Date(2017, JAN, 1));
392416
fixture.detectChanges();
393417

394418
fixture.whenStable().then(() => {
@@ -434,7 +458,7 @@ describe('MdDatepicker', () => {
434458

435459
expect(inputEl.classList).toContain('ng-untouched');
436460

437-
testComponent.datepicker._selectAndClose(new Date(2017, JAN, 1));
461+
testComponent.datepicker._select(new Date(2017, JAN, 1));
438462
fixture.detectChanges();
439463

440464
fixture.whenStable().then(() => {
@@ -478,7 +502,7 @@ describe('MdDatepicker', () => {
478502
expect(testComponent.datepickerInput.value).toBeNull();
479503

480504
let selected = new Date(2017, JAN, 1);
481-
testComponent.datepicker._selectAndClose(selected);
505+
testComponent.datepicker._select(selected);
482506
fixture.detectChanges();
483507

484508
expect(testComponent.formControl.value).toEqual(selected);

src/lib/datepicker/datepicker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,13 @@ export class MdDatepicker<D> implements OnDestroy {
224224
}
225225
}
226226

227-
/** Selects the given date and closes the currently open popup or dialog. */
228-
_selectAndClose(date: D): void {
227+
/** Selects the given date */
228+
_select(date: D): void {
229229
let oldValue = this._selected;
230230
this._selected = date;
231231
if (!this._dateAdapter.sameDate(oldValue, this._selected)) {
232232
this.selectedChanged.emit(date);
233233
}
234-
this.close();
235234
}
236235

237236
/**

src/lib/datepicker/month-view.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export class MdMonthView<D> implements AfterContentInit {
6767
/** Emits when a new date is selected. */
6868
@Output() selectedChange = new EventEmitter<D | null>();
6969

70+
/** Emits when any date is selected. */
71+
@Output() userSelection = new EventEmitter<void>();
72+
7073
/** The label for this month (e.g. "January 2017"). */
7174
_monthLabel: string;
7275

@@ -116,12 +119,15 @@ export class MdMonthView<D> implements AfterContentInit {
116119

117120
/** Handles when a new date is selected. */
118121
_dateSelected(date: number) {
119-
if (this._selectedDate == date) {
120-
return;
122+
if (this._selectedDate != date) {
123+
const selectedYear = this._dateAdapter.getYear(this.activeDate);
124+
const selectedMonth = this._dateAdapter.getMonth(this.activeDate);
125+
const selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date);
126+
127+
this.selectedChange.emit(selectedDate);
121128
}
122-
this.selectedChange.emit(this._dateAdapter.createDate(
123-
this._dateAdapter.getYear(this.activeDate), this._dateAdapter.getMonth(this.activeDate),
124-
date));
129+
130+
this.userSelection.emit();
125131
}
126132

127133
/** Initializes this month view. */

0 commit comments

Comments
 (0)