Skip to content

Commit 9cff8c7

Browse files
mmalerbatinayuangao
authored andcommitted
fix(datepicker): fix error when selecting month with fewer days in year (#6129)
view
1 parent 94bf5e9 commit 9cff8c7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/lib/datepicker/year-view.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {Component} from '@angular/core';
2+
import {Component, ViewChild} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MdYearView} from './year-view';
55
import {MdCalendarBody} from './calendar-body';
66
import {MdNativeDateModule} from '../core/datetime/index';
7-
import {FEB, JAN, MAR} from '../core/testing/month-constants';
7+
import {FEB, JAN, JUL, JUN, MAR} from '../core/testing/month-constants';
88

99
describe('MdYearView', () => {
1010
beforeEach(async(() => {
@@ -76,6 +76,16 @@ describe('MdYearView', () => {
7676
expect((cellEls[0] as HTMLElement).innerText.trim()).toBe('JAN');
7777
expect(cellEls[0].classList).toContain('mat-calendar-body-active');
7878
});
79+
80+
it('should allow selection of month with less days than current active date', () => {
81+
testComponent.date = new Date(2017, JUL, 31);
82+
fixture.detectChanges();
83+
84+
expect(testComponent.yearView._monthSelected(JUN));
85+
fixture.detectChanges();
86+
87+
expect(testComponent.selected).toEqual(new Date(2017, JUN, 30));
88+
});
7989
});
8090

8191
describe('year view with date filter', () => {
@@ -108,6 +118,8 @@ describe('MdYearView', () => {
108118
class StandardYearView {
109119
date = new Date(2017, JAN, 5);
110120
selected = new Date(2017, MAR, 10);
121+
122+
@ViewChild(MdYearView) yearView: MdYearView<Date>;
111123
}
112124

113125

src/lib/datepicker/year-view.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ export class MdYearView<D> implements AfterContentInit {
9595

9696
/** Handles when a new month is selected. */
9797
_monthSelected(month: number) {
98+
let daysInMonth = this._dateAdapter.getNumDaysInMonth(
99+
this._dateAdapter.createDate(this._dateAdapter.getYear(this.activeDate), month, 1));
98100
this.selectedChange.emit(this._dateAdapter.createDate(
99101
this._dateAdapter.getYear(this.activeDate), month,
100-
this._dateAdapter.getDate(this.activeDate)));
102+
Math.min(this._dateAdapter.getDate(this.activeDate), daysInMonth)));
101103
}
102104

103105
/** Initializes this month view. */

0 commit comments

Comments
 (0)