Skip to content

Commit 90e63ed

Browse files
committed
wip - safe work
1 parent 25fb01d commit 90e63ed

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/material/datepicker/calendar-body.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
111111
private _activeCell: number = 0;
112112

113113
ngAfterViewChecked() {
114+
console.log('ngAfterViewChecked', this._focusActiveCellAfterViewChecked);
114115
if (this._focusActiveCellAfterViewChecked) {
115116
this._focusActiveCell();
116117
this._focusActiveCellAfterViewChecked = false;
@@ -146,6 +147,8 @@ export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
146147
MatCalendarUserEvent<MatCalendarCell | null>
147148
>();
148149

150+
@Output() readonly activeCellChange = new EventEmitter<MatCalendarUserEvent<number>>();
151+
149152
/** The number of blank cells to put at the beginning for the first row. */
150153
_firstRowOffset: number;
151154

@@ -173,8 +176,9 @@ export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
173176
}
174177

175178
_cellFocused(cell: MatCalendarCell, event: FocusEvent): void {
179+
console.log('cellFocused', cell.value);
176180
if (cell.enabled) {
177-
// TODO: make argument cell the active date
181+
this.activeCellChange.emit({value: cell.value, event});
178182
}
179183
}
180184

@@ -222,11 +226,13 @@ export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
222226

223227
/** Focuses the active cell after the microtask queue is empty. */
224228
_focusActiveCell(movePreview = true) {
229+
console.log('_focusActiveCell', this.label);
225230
this._ngZone.runOutsideAngular(() => {
226231
this._ngZone.onStable.pipe(take(1)).subscribe(() => {
227232
const activeCell: HTMLElement | null = this._elementRef.nativeElement.querySelector(
228233
'.mat-calendar-body-active',
229234
);
235+
console.log('_focusActiveCell cb', activeCell?.innerText, this.label);
230236

231237
if (activeCell) {
232238
if (!movePreview) {
@@ -241,6 +247,7 @@ export class MatCalendarBody implements OnChanges, OnDestroy, AfterViewChecked {
241247

242248
/** Focuses the active cell after change detection has run and the microtask queue is empty. */
243249
_scheduleFocusActiveCellAfterViewChecked() {
250+
console.log('_scheduleFocusActiveCellAfterViewChecked');
244251
this._focusActiveCellAfterViewChecked = true;
245252
}
246253

src/material/datepicker/month-view.html

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
[activeCell]="_dateAdapter.getDate(activeDate) - 1"
2626
(selectedValueChange)="_dateSelected($event)"
2727
(previewChange)="_previewChanged($event)"
28+
(activeCellChange)="_dateBecomesActive($event)"
2829
(keyup)="_handleCalendarBodyKeyup($event)"
2930
(keydown)="_handleCalendarBodyKeydown($event)">
3031
</tbody>

src/material/datepicker/month-view.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
230230
/** Handles when a new date is selected. */
231231
_dateSelected(event: MatCalendarUserEvent<number>) {
232232
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._normalizeDate(date);
236234
let rangeStartDate: number | null;
237235
let rangeEndDate: number | null;
238236

@@ -252,8 +250,30 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
252250
this._changeDetectorRef.markForCheck();
253251
}
254252

253+
private _normalizeDate(date: number): D {
254+
const normalizedYear = this._dateAdapter.getYear(this.activeDate);
255+
const normalizedMonth = this._dateAdapter.getMonth(this.activeDate);
256+
const normalizedDate = this._dateAdapter.createDate(normalizedYear, normalizedMonth, date);
257+
return normalizedDate;
258+
}
259+
260+
/** Handles when a new date becomes active. */
261+
_dateBecomesActive(event: MatCalendarUserEvent<number>) {
262+
console.log('_dateBecomesActive', event.value);
263+
const date = event.value;
264+
const oldActiveDate = this.activeDate;
265+
this.activeDate = this._normalizeDate(date);
266+
267+
if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
268+
this.activeDateChange.emit(this._activeDate);
269+
console.log('schedule in _dateBecomesActive', this._activeDate);
270+
this._focusActiveCellAfterViewChecked();
271+
}
272+
}
273+
255274
/** Handles keydown events on the calendar body when calendar is in month view. */
256275
_handleCalendarBodyKeydown(event: KeyboardEvent): void {
276+
console.log('_handleCalendarBodyKeydown', event.key);
257277
// TODO(mmalerba): We currently allow keyboard navigation to disabled dates, but just prevent
258278
// disabled ones from being selected. This may not be ideal, we should look into whether
259279
// navigation should skip over disabled dates, and if so, how to implement that efficiently.
@@ -327,9 +347,10 @@ export class MatMonthView<D> implements AfterContentInit, OnChanges, OnDestroy {
327347

328348
if (this._dateAdapter.compareDate(oldActiveDate, this.activeDate)) {
329349
this.activeDateChange.emit(this.activeDate);
350+
console.log('scheduling in keydown handler', this._activeDate);
351+
this._focusActiveCellAfterViewChecked();
330352
}
331353

332-
this._focusActiveCellAfterViewChecked();
333354
// Prevent unexpected default actions such as form submission.
334355
event.preventDefault();
335356
}

0 commit comments

Comments
 (0)