Skip to content

Commit 37228e5

Browse files
tobiasschweizerjosephperrott
authored andcommitted
Add public method to MatCalendar to update today's date (#11821)
1 parent d54a75a commit 37228e5

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

src/lib/datepicker/calendar.spec.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import {
2-
ENTER,
3-
RIGHT_ARROW,
4-
} from '@angular/cdk/keycodes';
1+
import {Direction, Directionality} from '@angular/cdk/bidi';
2+
import {ENTER, RIGHT_ARROW} from '@angular/cdk/keycodes';
53
import {
64
dispatchFakeEvent,
75
dispatchKeyboardEvent,
86
dispatchMouseEvent,
97
MockNgZone,
108
} from '@angular/cdk/testing';
119
import {Component, NgZone} from '@angular/core';
12-
import {ComponentFixture, TestBed, async, inject} from '@angular/core/testing';
13-
import {DEC, FEB, JAN, MatNativeDateModule, NOV, JUL} from '@angular/material/core';
10+
import {async, ComponentFixture, inject, TestBed} from '@angular/core/testing';
11+
import {DateAdapter, DEC, FEB, JAN, JUL, MatNativeDateModule, NOV} from '@angular/material/core';
1412
import {By} from '@angular/platform-browser';
15-
import {Direction, Directionality} from '@angular/cdk/bidi';
1613
import {MatCalendar} from './calendar';
1714
import {MatDatepickerIntl} from './datepicker-intl';
1815
import {MatDatepickerModule} from './datepicker-module';
@@ -63,6 +60,27 @@ describe('MatCalendar', () => {
6360
testComponent = fixture.componentInstance;
6461
});
6562

63+
it(`should update today's date`, inject([DateAdapter], (adapter: DateAdapter<Date>) => {
64+
let fakeToday = new Date(2018, 0, 1);
65+
spyOn(adapter, 'today').and.callFake(() => fakeToday);
66+
67+
calendarInstance.activeDate = fakeToday;
68+
calendarInstance.updateTodaysDate();
69+
fixture.detectChanges();
70+
71+
let todayCell = calendarElement.querySelector('.mat-calendar-body-today')!;
72+
expect(todayCell).not.toBeNull();
73+
expect(todayCell.innerHTML.trim()).toBe('1');
74+
75+
fakeToday = new Date(2018, 0, 10);
76+
calendarInstance.updateTodaysDate();
77+
fixture.detectChanges();
78+
79+
todayCell = calendarElement.querySelector('.mat-calendar-body-today')!;
80+
expect(todayCell).not.toBeNull();
81+
expect(todayCell.innerHTML.trim()).toBe('10');
82+
}));
83+
6684
it('should be in month view with specified month active', () => {
6785
expect(calendarInstance.currentView).toBe('month');
6886
expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31));

src/lib/datepicker/calendar.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ export class MatCalendar<D> implements AfterContentInit, AfterViewChecked, OnDes
334334
this._getCurrentViewComponent()._focusActiveCell();
335335
}
336336

337+
/** Updates today's date after an update of the active date */
338+
updateTodaysDate() {
339+
let view = this.currentView == 'month' ? this.monthView :
340+
(this.currentView == 'year' ? this.yearView : this.multiYearView);
341+
342+
view.ngAfterContentInit();
343+
}
344+
337345
/** Handles date selection in the month view. */
338346
_dateSelected(date: D): void {
339347
if (!this._dateAdapter.sameDate(date, this.selected)) {

src/lib/datepicker/datepicker-content.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[headerComponent]="datepicker.calendarHeaderComponent"
1010
[selected]="datepicker._selected"
1111
[@fadeInCalendar]="'enter'"
12-
(selectedChange)="datepicker._select($event)"
12+
(selectedChange)="datepicker.select($event)"
1313
(yearSelected)="datepicker._selectYear($event)"
1414
(monthSelected)="datepicker._selectMonth($event)"
1515
(_userSelection)="datepicker.close()">

src/lib/datepicker/datepicker.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ describe('MatDatepicker', () => {
623623
expect(testComponent.datepickerInput.value).toBeNull();
624624

625625
let selected = new Date(2017, JAN, 1);
626-
testComponent.datepicker._select(selected);
626+
testComponent.datepicker.select(selected);
627627
fixture.detectChanges();
628628
flush();
629629
fixture.detectChanges();
@@ -649,7 +649,7 @@ describe('MatDatepicker', () => {
649649

650650
expect(inputEl.classList).toContain('ng-pristine');
651651

652-
testComponent.datepicker._select(new Date(2017, JAN, 1));
652+
testComponent.datepicker.select(new Date(2017, JAN, 1));
653653
fixture.detectChanges();
654654
flush();
655655
fixture.detectChanges();
@@ -723,7 +723,7 @@ describe('MatDatepicker', () => {
723723

724724
expect(inputEl.classList).toContain('ng-untouched');
725725

726-
testComponent.datepicker._select(new Date(2017, JAN, 1));
726+
testComponent.datepicker.select(new Date(2017, JAN, 1));
727727
fixture.detectChanges();
728728
flush();
729729
fixture.detectChanges();
@@ -765,7 +765,7 @@ describe('MatDatepicker', () => {
765765
expect(testComponent.datepickerInput.value).toBeNull();
766766

767767
let selected = new Date(2017, JAN, 1);
768-
testComponent.datepicker._select(selected);
768+
testComponent.datepicker.select(selected);
769769
fixture.detectChanges();
770770

771771
expect(testComponent.formControl.value).toEqual(selected);

src/lib/datepicker/datepicker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
290290
}
291291

292292
/** Selects the given date */
293-
_select(date: D): void {
293+
select(date: D): void {
294294
let oldValue = this._selected;
295295
this._selected = date;
296296
if (!this._dateAdapter.sameDate(oldValue, this._selected)) {

0 commit comments

Comments
 (0)