Skip to content

fix(datepicker): fix error when selecting month with fewer days in year view #6129

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
Jul 31, 2017
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
16 changes: 14 additions & 2 deletions src/lib/datepicker/year-view.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {Component} from '@angular/core';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdYearView} from './year-view';
import {MdCalendarBody} from './calendar-body';
import {MdNativeDateModule} from '../core/datetime/index';
import {FEB, JAN, MAR} from '../core/testing/month-constants';
import {FEB, JAN, JUL, JUN, MAR} from '../core/testing/month-constants';

describe('MdYearView', () => {
beforeEach(async(() => {
Expand Down Expand Up @@ -76,6 +76,16 @@ describe('MdYearView', () => {
expect((cellEls[0] as HTMLElement).innerText.trim()).toBe('JAN');
expect(cellEls[0].classList).toContain('mat-calendar-body-active');
});

it('should allow selection of month with less days than current active date', () => {
testComponent.date = new Date(2017, JUL, 31);
fixture.detectChanges();

expect(testComponent.yearView._monthSelected(JUN));
fixture.detectChanges();

expect(testComponent.selected).toEqual(new Date(2017, JUN, 30));
});
});

describe('year view with date filter', () => {
Expand Down Expand Up @@ -108,6 +118,8 @@ describe('MdYearView', () => {
class StandardYearView {
date = new Date(2017, JAN, 5);
selected = new Date(2017, MAR, 10);

@ViewChild(MdYearView) yearView: MdYearView<Date>;
}


Expand Down
4 changes: 3 additions & 1 deletion src/lib/datepicker/year-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ export class MdYearView<D> implements AfterContentInit {

/** Handles when a new month is selected. */
_monthSelected(month: number) {
let daysInMonth = this._dateAdapter.getNumDaysInMonth(
this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, 1));
this.selectedChange.emit(this._dateAdapter.createDate(
this._dateAdapter.getYear(this.activeDate), month,
this._dateAdapter.getDate(this.activeDate)));
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth)));
}

/** Initializes this month view. */
Expand Down