@@ -230,9 +230,7 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
230
230
/** Handles when a new date is selected. */
231
231
_dateSelected ( event : MatCalendarUserEvent < number > ) {
232
232
const date = event . value ;
233
- const selectedYear = this . _dateAdapter . getYear ( this . activeDate ) ;
234
- const selectedMonth = this . _dateAdapter . getMonth ( this . activeDate ) ;
235
- const selectedDate = this . _dateAdapter . createDate ( selectedYear , selectedMonth , date ) ;
233
+ const selectedDate = this . _getDateFromDayOfMonth ( date ) ;
236
234
let rangeStartDate : number | null ;
237
235
let rangeEndDate : number | null ;
238
236
@@ -252,6 +250,19 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
252
250
this . _changeDetectorRef . markForCheck ( ) ;
253
251
}
254
252
253
+ /** Handles focus events on a cell in the calendar body. */
254
+ _handleCalendarBodyDateFocused ( event : MatCalendarUserEvent < number > ) {
255
+ const month = event . value ;
256
+ const oldActiveDate = this . _activeDate ;
257
+ this . activeDate = this . _getDateFromDayOfMonth ( month ) ;
258
+
259
+ if ( this . _dateAdapter . compareDate ( oldActiveDate , this . activeDate ) ) {
260
+ this . activeDateChange . emit ( this . _activeDate ) ;
261
+
262
+ this . _focusActiveCellAfterViewChecked ( ) ;
263
+ }
264
+ }
265
+
255
266
/** Handles keydown events on the calendar body when calendar is in month view. */
256
267
_handleCalendarBodyKeydown ( event : KeyboardEvent ) : void {
257
268
// TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
@@ -327,9 +338,10 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
327
338
328
339
if ( this . _dateAdapter . compareDate ( oldActiveDate , this . activeDate ) ) {
329
340
this . activeDateChange . emit ( this . activeDate ) ;
341
+
342
+ this . _focusActiveCellAfterViewChecked ( ) ;
330
343
}
331
344
332
- this . _focusActiveCell ( ) ;
333
345
// Prevent unexpected default actions such as form submission.
334
346
event . preventDefault ( ) ;
335
347
}
@@ -376,6 +388,11 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
376
388
this . _matCalendarBody . _focusActiveCell ( movePreview ) ;
377
389
}
378
390
391
+ /** Focuses the active cell after change detection has run and the microtask queue is empty. */
392
+ _focusActiveCellAfterViewChecked ( ) {
393
+ this . _matCalendarBody . _scheduleFocusActiveCellAfterViewChecked ( ) ;
394
+ }
395
+
379
396
/** Called when the user has activated a new cell and the preview needs to be updated. */
380
397
_previewChanged ( { event, value : cell } : MatCalendarUserEvent < MatCalendarCell < D > | null > ) {
381
398
if ( this . _rangeStrategy ) {
@@ -398,6 +415,18 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
398
415
}
399
416
}
400
417
418
+ /**
419
+ * Takes a day of the month and returns a new date in the same month and year as the currently
420
+ * active date. The returned date will have the same day of the month as the argument date.
421
+ */
422
+ private _getDateFromDayOfMonth ( dayOfMonth : number ) : D {
423
+ return this . _dateAdapter . createDate (
424
+ this . _dateAdapter . getYear ( this . activeDate ) ,
425
+ this . _dateAdapter . getMonth ( this . activeDate ) ,
426
+ dayOfMonth ,
427
+ ) ;
428
+ }
429
+
401
430
/** Initializes the weekdays. */
402
431
private _initWeekdays ( ) {
403
432
const firstDayOfWeek = this . _dateAdapter . getFirstDayOfWeek ( ) ;
0 commit comments