Skip to content

Commit 533739e

Browse files
author
Tobias Schweizer
committed
feature (MatCalendarHeader): add sample custom header controls to demo page
1 parent 70ff16f commit 533739e

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {DateAdapter} from '@angular/material/core';
1313
import {MatCalendar} from '@angular/material';
1414
import {ThemePalette} from '@angular/material/core';
1515

16-
1716
@Component({
1817
moduleId: module.id,
1918
selector: 'datepicker-demo',
@@ -50,10 +49,41 @@ export class DatepickerDemo {
5049
// Custom header component for datepicker
5150
@Component({
5251
selector: 'custom-header',
53-
template: 'custom header'
52+
template: `
53+
<div>
54+
<button (click)="previousClicked('year')">&lt;&lt;</button>
55+
<button (click)="previousClicked('month')">&lt;</button>
56+
{{periodLabel}}
57+
<button (click)="nextClicked('month')">&gt;</button>
58+
<button (click)="nextClicked('year')">&gt;&gt;</button>
59+
</div>
60+
`
5461
})
5562
export class CustomHeader {
5663
constructor(@Host() public calendar: MatCalendar<any>,
57-
public adapter: DateAdapter<any>) {
64+
private _dateAdapter: DateAdapter<any>) {}
65+
66+
get periodLabel() {
67+
let year = this._dateAdapter.getYearName(this.calendar.activeDate);
68+
let month = (this._dateAdapter.getMonth(this.calendar.activeDate) + 1);
69+
return `${month}/${year}`;
70+
}
71+
72+
previousClicked(mode: 'month' | 'year') {
73+
this.calendar.activeDate = mode == 'month' ?
74+
this._dateAdapter.addCalendarMonths(this.calendar.activeDate, -1) :
75+
this._dateAdapter.addCalendarYears(
76+
this.calendar.activeDate,
77+
-1
78+
);
79+
}
80+
81+
nextClicked(mode: 'month' | 'year') {
82+
this.calendar.activeDate = mode == 'month' ?
83+
this._dateAdapter.addCalendarMonths(this.calendar.activeDate, 1) :
84+
this._dateAdapter.addCalendarYears(
85+
this.calendar.activeDate,
86+
1
87+
);
5888
}
5989
}

0 commit comments

Comments
 (0)