@@ -18,6 +18,7 @@ import {
18
18
OnChanges ,
19
19
SimpleChanges ,
20
20
OnDestroy ,
21
+ AfterViewChecked ,
21
22
} from '@angular/core' ;
22
23
import { take } from 'rxjs/operators' ;
23
24
@@ -67,13 +68,18 @@ export interface MatCalendarUserEvent<D> {
67
68
encapsulation : ViewEncapsulation . None ,
68
69
changeDetection : ChangeDetectionStrategy . OnPush ,
69
70
} )
70
- export class MatCalendarBody implements OnChanges , OnDestroy {
71
+ export class MatCalendarBody implements OnChanges , OnDestroy , AfterViewChecked {
71
72
/**
72
73
* Used to skip the next focus event when rendering the preview range.
73
74
* We need a flag like this, because some browsers fire focus events asynchronously.
74
75
*/
75
76
private _skipNextFocus : boolean ;
76
77
78
+ /**
79
+ * Used to focus the active cell after change detection has run.
80
+ */
81
+ private _focusActiveCellAfterViewChecked = false ;
82
+
77
83
/** The label for the table. (e.g. "Jan 2017"). */
78
84
@Input ( ) label : string ;
79
85
@@ -96,7 +102,20 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
96
102
@Input ( ) numCols : number = 7 ;
97
103
98
104
/** The cell number of the active cell in the table. */
99
- @Input ( ) activeCell : number = 0 ;
105
+ @Input ( ) get activeCell ( ) : number {
106
+ return this . _activeCell ;
107
+ }
108
+ set activeCell ( activeCell : number ) {
109
+ this . _activeCell = activeCell ;
110
+ }
111
+ private _activeCell : number = 0 ;
112
+
113
+ ngAfterViewChecked ( ) {
114
+ if ( this . _focusActiveCellAfterViewChecked ) {
115
+ this . _focusActiveCell ( ) ;
116
+ this . _focusActiveCellAfterViewChecked = false ;
117
+ }
118
+ }
100
119
101
120
/** Whether a range is being selected. */
102
121
@Input ( ) isRange : boolean = false ;
@@ -153,6 +172,12 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
153
172
}
154
173
}
155
174
175
+ _cellFocused ( cell : MatCalendarCell , event : FocusEvent ) : void {
176
+ if ( cell . enabled ) {
177
+ // TODO: make argument cell the active date
178
+ }
179
+ }
180
+
156
181
/** Returns whether a cell should be marked as selected. */
157
182
_isSelected ( value : number ) {
158
183
return this . startValue === value || this . endValue === value ;
@@ -214,6 +239,11 @@ export class MatCalendarBody implements OnChanges, OnDestroy {
214
239
} ) ;
215
240
}
216
241
242
+ /** Focuses the active cell after change detection has run and the microtask queue is empty. */
243
+ _scheduleFocusActiveCellAfterViewChecked ( ) {
244
+ this . _focusActiveCellAfterViewChecked = true ;
245
+ }
246
+
217
247
/** Gets whether a value is the start of the main range. */
218
248
_isRangeStart ( value : number ) {
219
249
return isStart ( value , this . startValue , this . endValue ) ;
0 commit comments