Skip to content

Commit 3fc0d36

Browse files
mnahkiesvivian-hu-zz
authored andcommitted
feat(datepicker): add ng-content to datepicker header (#13236)
* feat(datepicker): add ng-content to datepicker header - add ng-content to datepicker header to simplify customization of this component - fix tests using calendarHeaderComponent property to correctly reference the custom component - add test to prove that custom component was rendered Closes #13212 * test(datepicker): code review fixes - remove unnecessary tests - simplify demo code
1 parent 4f43f5c commit 3fc0d36

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

src/demo-app/datepicker/datepicker-demo.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,13 @@ <h2>Datepicker with custom header</h2>
154154
<mat-datepicker #customHeaderPicker [calendarHeaderComponent]="customHeader"></mat-datepicker>
155155
</mat-form-field>
156156
</p>
157+
158+
<h2>Datepicker with custom header extending the default header</h2>
159+
<p>
160+
<mat-form-field>
161+
<mat-label>Custom calendar header extending default</mat-label>
162+
<input matInput [matDatepicker]="customHeaderNgContentPicker">
163+
<mat-datepicker-toggle matSuffix [for]="customHeaderNgContentPicker"></mat-datepicker-toggle>
164+
<mat-datepicker #customHeaderNgContentPicker [calendarHeaderComponent]="customHeaderNgContent"></mat-datepicker>
165+
</mat-form-field>
166+
</p>

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import {
1212
Component,
1313
Host,
1414
Inject,
15+
ViewChild,
16+
Optional,
1517
OnDestroy
1618
} from '@angular/core';
1719
import {FormControl} from '@angular/forms';
18-
import {MatCalendar} from '@angular/material';
20+
import {MatCalendar, MatCalendarHeader} from '@angular/material';
1921
import {DateAdapter, MAT_DATE_FORMATS, MatDateFormats, ThemePalette} from '@angular/material/core';
2022
import {MatDatepickerInputEvent} from '@angular/material/datepicker';
2123
import {Subject} from 'rxjs';
@@ -53,6 +55,7 @@ export class DatepickerDemo {
5355

5456
// pass custom header component type as input
5557
customHeader = CustomHeader;
58+
customHeaderNgContent = CustomHeaderNgContent;
5659
}
5760

5861
// Custom header component for datepicker
@@ -98,3 +101,27 @@ export class CustomHeader<D> implements OnDestroy {
98101
this._dateAdapter.addCalendarYears(this._calendar.activeDate, 1);
99102
}
100103
}
104+
105+
@Component({
106+
moduleId: module.id,
107+
selector: 'customer-header-ng-content',
108+
template: `
109+
<mat-calendar-header #header>
110+
<button mat-button type="button" (click)="todayClicked()">TODAY</button>
111+
</mat-calendar-header>
112+
`
113+
})
114+
export class CustomHeaderNgContent<D> {
115+
116+
@ViewChild(MatCalendarHeader)
117+
header: MatCalendarHeader<D>;
118+
119+
constructor(@Optional() private _dateAdapter: DateAdapter<D>) {}
120+
121+
todayClicked() {
122+
let calendar = this.header.calendar;
123+
124+
calendar.activeDate = this._dateAdapter.today();
125+
calendar.currentView = 'month';
126+
}
127+
}

src/demo-app/demo-app/demo-module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {CardDemo} from '../card/card-demo';
2525
import {CheckboxDemo, MatCheckboxDemoNestedChecklist} from '../checkbox/checkbox-demo';
2626
import {ChipsDemo} from '../chips/chips-demo';
2727
import {ConnectedOverlayDemo} from '../connected-overlay/connected-overlay-demo';
28-
import {CustomHeader, DatepickerDemo} from '../datepicker/datepicker-demo';
28+
import {CustomHeader, CustomHeaderNgContent, DatepickerDemo} from '../datepicker/datepicker-demo';
2929
import {DemoMaterialModule} from '../demo-material-module';
3030
import {ContentElementDialog, DialogDemo, IFrameDialog, JazzDialog} from '../dialog/dialog-demo';
3131
import {DrawerDemo} from '../drawer/drawer-demo';
@@ -92,6 +92,7 @@ import {DragAndDropDemo} from '../drag-drop/drag-drop-demo';
9292
ConnectedOverlayDemo,
9393
ContentElementDialog,
9494
CustomHeader,
95+
CustomHeaderNgContent,
9596
DatepickerDemo,
9697
DemoApp,
9798
DialogDemo,
@@ -138,6 +139,7 @@ import {DragAndDropDemo} from '../drag-drop/drag-drop-demo';
138139
entryComponents: [
139140
ContentElementDialog,
140141
CustomHeader,
142+
CustomHeaderNgContent,
141143
DemoApp,
142144
ExampleBottomSheet,
143145
IFrameDialog,

src/lib/datepicker/calendar-header.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<div class="mat-calendar-spacer"></div>
1212

13+
<ng-content></ng-content>
14+
1315
<button mat-icon-button type="button" class="mat-calendar-previous-button"
1416
[disabled]="!previousEnabled()" (click)="previousClicked()"
1517
[attr.aria-label]="prevButtonLabel">

src/lib/datepicker/datepicker.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,17 @@ describe('MatDatepicker', () => {
16321632

16331633
expect(document.querySelector('mat-calendar-header')).toBeTruthy();
16341634
}));
1635+
1636+
it('should find the custom element', fakeAsync(() => {
1637+
testComponent.datepicker.open();
1638+
fixture.detectChanges();
1639+
flush();
1640+
fixture.detectChanges();
1641+
1642+
expect(document.querySelector('.custom-element')).toBeTruthy();
1643+
}));
16351644
});
1645+
16361646
});
16371647

16381648

@@ -1885,23 +1895,22 @@ class DatepickerOpeningOnFocus {
18851895
@Component({
18861896
template: `
18871897
<input [matDatepicker]="ch">
1888-
<mat-datepicker #ch [calendarHeaderComponent]="CustomHeaderForDatepicker"></mat-datepicker>
1898+
<mat-datepicker #ch [calendarHeaderComponent]="customHeaderForDatePicker"></mat-datepicker>
18891899
`,
18901900
})
18911901
class DatepickerWithCustomHeader {
18921902
@ViewChild('ch') datepicker: MatDatepicker<Date>;
1903+
customHeaderForDatePicker = CustomHeaderForDatepicker;
18931904
}
18941905

18951906
@Component({
18961907
template: `
1897-
<div>Custom element</div>
1908+
<div class="custom-element">Custom element</div>
18981909
<mat-calendar-header></mat-calendar-header>
18991910
`,
19001911
})
19011912
class CustomHeaderForDatepicker {}
19021913

1903-
1904-
19051914
@Component({
19061915
template: `
19071916
<input [matDatepicker]="assignedDatepicker" [value]="date">

0 commit comments

Comments
 (0)