Skip to content
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

feat(datepicker): close calendar after choose the same date again #6323

Merged
merged 6 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/datepicker/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
[activeDate]="_activeDate"
[selected]="selected"
[dateFilter]="_dateFilterForViews"
(selectedChange)="_dateSelected($event)">
(selectedChange)="_dateSelected($event)"
(userSelection)="_userSelected()">
</md-month-view>

<md-year-view
Expand Down
7 changes: 7 additions & 0 deletions src/lib/datepicker/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
/** Emits when the currently selected date changes. */
@Output() selectedChange = new EventEmitter<D>();

/** Emits when any date is selected. */
@Output() userSelection = new EventEmitter<void>();

/** Date filter for the month and year views. */
_dateFilterForViews = (date: D) => {
return !!date &&
Expand Down Expand Up @@ -159,6 +162,10 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
}
}

_userSelected(): void {
this.userSelection.emit();
}

/** Handles month selection in the year view. */
_monthSelected(month: D): void {
this._activeDate = month;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/datepicker/datepicker-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
[maxDate]="datepicker._maxDate"
[dateFilter]="datepicker._dateFilter"
[selected]="datepicker._selected"
(selectedChange)="datepicker._selectAndClose($event)">
(selectedChange)="datepicker._select($event)"
(userSelection)="datepicker.close()">
</md-calendar>
2 changes: 1 addition & 1 deletion src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
ngAfterContentInit() {
if (this._datepicker) {
this._datepickerSubscription =
this._datepicker.selectedChanged.subscribe((selected: D) => {
this._datepicker.selectedChanged.subscribe((selected: D) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Expand Down
32 changes: 28 additions & 4 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ describe('MdDatepicker', () => {
});
});

it('clicking the currently selected date should close the calendar' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before close of string so result doesn't read: "calendarwithout"

'without firing selectedChanged', () => {
const selectedChangedSpy =
spyOn(testComponent.datepicker.selectedChanged, 'emit').and.callThrough();
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify that selectedChanged only emits the first time

fixture.detectChanges();
}

fixture.whenStable().then(() => {
expect(selectedChangedSpy.calls.count()).toEqual(1);
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));
});
Expand Down Expand Up @@ -362,7 +386,7 @@ describe('MdDatepicker', () => {
expect(testComponent.datepickerInput.value).toBeNull();

let selected = new Date(2017, JAN, 1);
testComponent.datepicker._selectAndClose(selected);
testComponent.datepicker._select(selected);
fixture.detectChanges();

fixture.whenStable().then(() => {
Expand All @@ -389,7 +413,7 @@ describe('MdDatepicker', () => {

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

testComponent.datepicker._selectAndClose(new Date(2017, JAN, 1));
testComponent.datepicker._select(new Date(2017, JAN, 1));
fixture.detectChanges();

fixture.whenStable().then(() => {
Expand Down Expand Up @@ -435,7 +459,7 @@ describe('MdDatepicker', () => {

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

testComponent.datepicker._selectAndClose(new Date(2017, JAN, 1));
testComponent.datepicker._select(new Date(2017, JAN, 1));
fixture.detectChanges();

fixture.whenStable().then(() => {
Expand Down Expand Up @@ -479,7 +503,7 @@ describe('MdDatepicker', () => {
expect(testComponent.datepickerInput.value).toBeNull();

let selected = new Date(2017, JAN, 1);
testComponent.datepicker._selectAndClose(selected);
testComponent.datepicker._select(selected);
fixture.detectChanges();

expect(testComponent.formControl.value).toEqual(selected);
Expand Down
3 changes: 1 addition & 2 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,12 @@ export class MdDatepicker<D> implements OnDestroy {
}

/** Selects the given date and closes the currently open popup or dialog. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you update this comment too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh... sorry, i missed it

_selectAndClose(date: D): void {
_select(date: D): void {
let oldValue = this._selected;
this._selected = date;
if (!this._dateAdapter.sameDate(oldValue, this._selected)) {
this.selectedChanged.emit(date);
}
this.close();
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/lib/datepicker/month-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 any date is selected. */
@Output() userSelection = new EventEmitter<void>();

/** The label for this month (e.g. "January 2017"). */
_monthLabel: string;

Expand Down Expand Up @@ -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. */
Expand Down