@@ -179,23 +179,29 @@ export class MatYearView<D> implements AfterContentInit, OnDestroy {
179
179
/** Handles when a new month is selected. */
180
180
_monthSelected ( event : MatCalendarUserEvent < number > ) {
181
181
const month = event . value ;
182
- const normalizedDate = this . _dateAdapter . createDate (
182
+
183
+ const selectedMonth = this . _dateAdapter . createDate (
183
184
this . _dateAdapter . getYear ( this . activeDate ) ,
184
185
month ,
185
186
1 ,
186
187
) ;
188
+ this . monthSelected . emit ( selectedMonth ) ;
187
189
188
- this . monthSelected . emit ( normalizedDate ) ;
190
+ const selectedDate = this . _normalizeMonthToActiveDate ( month ) ;
191
+ this . selectedChange . emit ( selectedDate ) ;
192
+ }
189
193
190
- const daysInMonth = this . _dateAdapter . getNumDaysInMonth ( normalizedDate ) ;
194
+ /** Handles when a new month becomes active. */
195
+ _monthBecomesActive ( event : MatCalendarUserEvent < number > ) {
196
+ const month = event . value ;
197
+ const oldActiveDate = this . _activeDate ;
191
198
192
- this . selectedChange . emit (
193
- this . _dateAdapter . createDate (
194
- this . _dateAdapter . getYear ( this . activeDate ) ,
195
- month ,
196
- Math . min ( this . _dateAdapter . getDate ( this . activeDate ) , daysInMonth ) ,
197
- ) ,
198
- ) ;
199
+ this . activeDate = this . _normalizeMonthToActiveDate ( month ) ;
200
+
201
+ if ( this . _dateAdapter . compareDate ( oldActiveDate , this . activeDate ) ) {
202
+ this . activeDateChange . emit ( this . activeDate ) ;
203
+ this . _focusActiveCellAfterViewChecked ( ) ;
204
+ }
199
205
}
200
206
201
207
/** Handles keydown events on the calendar body when calendar is in year view. */
@@ -259,9 +265,9 @@ export class MatYearView<D> implements AfterContentInit, OnDestroy {
259
265
260
266
if ( this . _dateAdapter . compareDate ( oldActiveDate , this . activeDate ) ) {
261
267
this . activeDateChange . emit ( this . activeDate ) ;
268
+ this . _focusActiveCellAfterViewChecked ( ) ;
262
269
}
263
270
264
- this . _focusActiveCell ( ) ;
265
271
// Prevent unexpected default actions such as form submission.
266
272
event . preventDefault ( ) ;
267
273
}
@@ -298,6 +304,11 @@ export class MatYearView<D> implements AfterContentInit, OnDestroy {
298
304
this . _matCalendarBody . _focusActiveCell ( ) ;
299
305
}
300
306
307
+ /** Schedules the matCalendarBody to focus the active cell after change detection has run */
308
+ _focusActiveCellAfterViewChecked ( ) {
309
+ this . _matCalendarBody . _scheduleFocusActiveCellAfterViewChecked ( ) ;
310
+ }
311
+
301
312
/**
302
313
* Gets the month in this year that the given Date falls on.
303
314
* Returns null if the given Date is in another year.
@@ -308,6 +319,23 @@ export class MatYearView<D> implements AfterContentInit, OnDestroy {
308
319
: null ;
309
320
}
310
321
322
+ /** Used for turning a cell number into a date. */
323
+ private _normalizeMonthToActiveDate ( month : number ) {
324
+ const normalizedDate = this . _dateAdapter . createDate (
325
+ this . _dateAdapter . getYear ( this . activeDate ) ,
326
+ month ,
327
+ 1 ,
328
+ ) ;
329
+
330
+ const daysInMonth = this . _dateAdapter . getNumDaysInMonth ( normalizedDate ) ;
331
+
332
+ return this . _dateAdapter . createDate (
333
+ this . _dateAdapter . getYear ( this . activeDate ) ,
334
+ month ,
335
+ Math . min ( this . _dateAdapter . getDate ( this . activeDate ) , daysInMonth ) ,
336
+ ) ;
337
+ }
338
+
311
339
/** Creates an MatCalendarCell for the given month. */
312
340
private _createCellForMonth ( month : number , monthName : string ) {
313
341
const date = this . _dateAdapter . createDate ( this . _dateAdapter . getYear ( this . activeDate ) , month , 1 ) ;
0 commit comments