-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(datepicker): close calendar after choose the same date again #6323
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
Changes from 3 commits
9034f7c
c839ac6
64301f0
b3cec45
878e109
0ae408d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
[maxDate]="datepicker._maxDate" | ||
[dateFilter]="datepicker._dateFilter" | ||
[selected]="datepicker._selected" | ||
(selectedChange)="datepicker._selectAndClose($event)"> | ||
(selectedChange)="datepicker._selectAndClose($event)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
(userSelection)="datepicker.close()"> | ||
</md-calendar> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,13 +224,13 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces | |
ngAfterContentInit() { | ||
if (this._datepicker) { | ||
this._datepickerSubscription = | ||
this._datepicker.selectedChanged.subscribe((selected: D) => { | ||
this.value = selected; | ||
this._cvaOnChange(selected); | ||
this._onTouched(); | ||
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); | ||
this.dateChange.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); | ||
}); | ||
this._datepicker.selectedChanged.subscribe((selected: D) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this should stay the same too. They indent 2 for function blocks and 4 for line breaks |
||
this.value = selected; | ||
this._cvaOnChange(selected); | ||
this._onTouched(); | ||
this.dateInput.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); | ||
this.dateChange.emit(new MdDatepickerInputEvent(this, this._elementRef.nativeElement)); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they actually prefer line breaks like this to stay 4 spaces |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,26 @@ describe('MdDatepicker', () => { | |
}); | ||
}); | ||
|
||
it('setting twice to the same selected value should update input and close calendar', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'clicking the currently selected date should close the calendar without firing selectedChanged' |
||
for (let changeCount = 1; changeCount < 3; changeCount++) { | ||
const currentDay = changeCount; | ||
testComponent.datepicker.open(); | ||
fixture.detectChanges(); | ||
|
||
expect(document.querySelector('md-datepicker-content')).not.toBeNull(); | ||
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, currentDay)); | ||
|
||
let cells = document.querySelectorAll('.mat-calendar-body-cell'); | ||
dispatchMouseEvent(cells[1], 'click'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. verify that |
||
fixture.detectChanges(); | ||
} | ||
|
||
fixture.whenStable().then(() => { | ||
expect(document.querySelector('md-dialog-container')).toBeNull(); | ||
expect(testComponent.datepickerInput.value).toEqual(new Date(2020, JAN, 2)); | ||
}); | ||
}); | ||
|
||
it('startAt should fallback to input value', () => { | ||
expect(testComponent.datepicker.startAt).toEqual(new Date(2020, JAN, 1)); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,9 @@ export class MdMonthView<D> implements AfterContentInit { | |
/** Emits when a new date is selected. */ | ||
@Output() selectedChange = new EventEmitter<D | null>(); | ||
|
||
/** Emits when date is picked */ | ||
@Output() userSelection = new EventEmitter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment you had above seems more appropriate for this as well:
Nit: Add a period to both to match other comments There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I think it should have a <void> type? new EventEmitter<void>(); |
||
|
||
/** The label for this month (e.g. "January 2017"). */ | ||
_monthLabel: string; | ||
|
||
|
@@ -116,12 +119,15 @@ export class MdMonthView<D> implements AfterContentInit { | |
|
||
/** Handles when a new date is selected. */ | ||
_dateSelected(date: number) { | ||
if (this._selectedDate == date) { | ||
return; | ||
if (this._selectedDate != date) { | ||
const selectedYear = this._dateAdapter.getYear(this.activeDate); | ||
const selectedMonth = this._dateAdapter.getMonth(this.activeDate); | ||
const selectedDate = this._dateAdapter.createDate(selectedYear, selectedMonth, date); | ||
|
||
this.selectedChange.emit(selectedDate); | ||
} | ||
this.selectedChange.emit(this._dateAdapter.createDate( | ||
this._dateAdapter.getYear(this.activeDate), this._dateAdapter.getMonth(this.activeDate), | ||
date)); | ||
|
||
this.userSelection.emit(); | ||
} | ||
|
||
/** Initializes this month view. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closing bracket should be on line above