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

view
This commit is contained in:
mmalerba
2017-07-31 11:23:36 -07:00
committed by tinayuangao
parent 94bf5e92e2
commit 9cff8c73b5
2 changed files with 17 additions and 3 deletions
+14 -2
View File
@@ -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(() => {
@@ -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', () => {
@@ -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>;
}
+3 -1
View File
@@ -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. */