Skip to content

Commit 715e413

Browse files
author
Tobias Schweizer
committed
refactor (MatCalendarHeader): use type parameter for header (generics)
1 parent c7f21bf commit 715e413

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/demo-app/datepicker/datepicker-demo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export class DatepickerDemo {
5959
</div>
6060
`
6161
})
62-
export class CustomHeader {
63-
constructor(@Host() private _calendar: MatCalendar<any>,
64-
private _dateAdapter: DateAdapter<any>) {}
62+
export class CustomHeader<D> {
63+
constructor(@Host() private _calendar: MatCalendar<D>,
64+
private _dateAdapter: DateAdapter<D>) {}
6565

6666
get periodLabel() {
6767
let year = this._dateAdapter.getYearName(this._calendar.activeDate);

src/lib/datepicker/calendar.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ import {takeUntil} from 'rxjs/operators/takeUntil';
4343
preserveWhitespaces: false,
4444
changeDetection: ChangeDetectionStrategy.OnPush,
4545
})
46-
export class MatCalendarHeader implements OnDestroy {
46+
export class MatCalendarHeader<D> implements OnDestroy {
4747
/** Subject that emits when the component has been destroyed. */
4848
private _destroyed = new Subject<void>();
4949

5050
constructor(private _intl: MatDatepickerIntl,
51-
@Host() public calendar: MatCalendar<any>,
52-
@Optional() private _dateAdapter: DateAdapter<any>,
51+
@Host() public calendar: MatCalendar<D>,
52+
@Optional() private _dateAdapter: DateAdapter<D>,
5353
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
5454
changeDetectorRef: ChangeDetectorRef) {
5555
_intl.changes.pipe(takeUntil(this._destroyed))
@@ -127,13 +127,27 @@ export class MatCalendarHeader implements OnDestroy {
127127
return true;
128128
}
129129
return !this.calendar.minDate ||
130-
!this.calendar.isSameView(this.calendar.activeDate, this.calendar.minDate);
130+
!this._isSameView(this.calendar.activeDate, this.calendar.minDate);
131131
}
132132

133133
/** Whether the next period button is enabled. */
134134
nextEnabled(): boolean {
135135
return !this.calendar.maxDate ||
136-
!this.calendar.isSameView(this.calendar.activeDate, this.calendar.maxDate);
136+
!this._isSameView(this.calendar.activeDate, this.calendar.maxDate);
137+
}
138+
139+
/** Whether the two dates represent the same view in the current view mode (month or year). */
140+
private _isSameView(date1: D, date2: D): boolean {
141+
if (this.calendar.currentView == 'month') {
142+
return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2) &&
143+
this._dateAdapter.getMonth(date1) == this._dateAdapter.getMonth(date2);
144+
}
145+
if (this.calendar.currentView == 'year') {
146+
return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2);
147+
}
148+
// Otherwise we are in 'multi-year' view.
149+
return Math.floor(this._dateAdapter.getYear(date1) / yearsPerPage) ==
150+
Math.floor(this._dateAdapter.getYear(date2) / yearsPerPage);
137151
}
138152

139153
ngOnDestroy() {
@@ -311,20 +325,6 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
311325
this.currentView = view;
312326
}
313327

314-
/** Whether the two dates represent the same view in the current view mode (month or year). */
315-
isSameView(date1: D, date2: D): boolean {
316-
if (this.currentView == 'month') {
317-
return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2) &&
318-
this._dateAdapter.getMonth(date1) == this._dateAdapter.getMonth(date2);
319-
}
320-
if (this.currentView == 'year') {
321-
return this._dateAdapter.getYear(date1) == this._dateAdapter.getYear(date2);
322-
}
323-
// Otherwise we are in 'multi-year' view.
324-
return Math.floor(this._dateAdapter.getYear(date1) / yearsPerPage) ==
325-
Math.floor(this._dateAdapter.getYear(date2) / yearsPerPage);
326-
}
327-
328328
/**
329329
* @param obj The object to check.
330330
* @returns The given object if it is both a date instance and valid, otherwise null.

0 commit comments

Comments
 (0)