Skip to content

Commit 153dfb8

Browse files
tobiasschweizermmalerba
authored andcommitted
fix(datepicker): allow MatCalendarHeader to be re-used inside a custom header (#10856)
* refactor (MatCalendarHeader): remove Host decorator * fix (remove unused import) * feature (MatCalendarHeader): add custom header using the standard header to demo * fix (ts lint) * test (MatCalendarHeader): add a unit test for custom header input (ongoing) * refactor (styling) * refactor (styling) * test (MatCalendarHeader): pass header component to datepicker * test (MatCalendarHeader): remove header component to fix test (temporary) * test (MatCalendarHeader): provide entryComponents * test (MatCalendarHeader): add custom header component to declarations * fix (MatCalendarHeader): fix issue with rollup-globals * test (MatCalendarHeader): get rid of declarations param * test (MatCalendarHeader): add test to check for standard calendar header selector * test (MatCalendarHeader): rename test * test (MatCalendarHeader): type annotations
1 parent d0a4e9f commit 153dfb8

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/lib/datepicker/calendar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
Component,
1515
EventEmitter,
1616
forwardRef,
17-
Host,
1817
Inject,
1918
Input,
2019
OnChanges,
@@ -39,6 +38,7 @@ import {MatYearView} from './year-view';
3938
moduleId: module.id,
4039
selector: 'mat-calendar-header',
4140
templateUrl: 'calendar-header.html',
41+
exportAs: 'matCalendarHeader',
4242
encapsulation: ViewEncapsulation.None,
4343
changeDetection: ChangeDetectionStrategy.OnPush,
4444
})
@@ -47,7 +47,7 @@ export class MatCalendarHeader<D> implements OnDestroy {
4747
private _destroyed = new Subject<void>();
4848

4949
constructor(private _intl: MatDatepickerIntl,
50-
@Host() @Inject(forwardRef(() => MatCalendar)) public calendar: MatCalendar<D>,
50+
@Inject(forwardRef(() => MatCalendar)) public calendar: MatCalendar<D>,
5151
@Optional() private _dateAdapter: DateAdapter<D>,
5252
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
5353
changeDetectorRef: ChangeDetectorRef) {

src/lib/datepicker/datepicker.spec.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
createKeyboardEvent,
88
dispatchEvent,
99
} from '@angular/cdk/testing';
10-
import {Component, ViewChild} from '@angular/core';
10+
import {Component, FactoryProvider, Type, ValueProvider, ViewChild} from '@angular/core';
1111
import {ComponentFixture, fakeAsync, flush, inject, TestBed} from '@angular/core/testing';
1212
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
1313
import {
@@ -30,13 +30,17 @@ import {MatDatepickerToggle} from './datepicker-toggle';
3030
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
3131
import {Subject} from 'rxjs';
3232
import {Directionality} from '@angular/cdk/bidi';
33+
import {BrowserDynamicTestingModule} from '@angular/platform-browser-dynamic/testing';
3334

3435
describe('MatDatepicker', () => {
3536
const SUPPORTS_INTL = typeof Intl != 'undefined';
3637

3738
// Creates a test component fixture.
38-
function createComponent(component: any, imports: any[] = [], providers: any[] = []):
39-
ComponentFixture<any> {
39+
function createComponent(
40+
component: Type<any>,
41+
imports: Type<any>[] = [],
42+
providers: (FactoryProvider | ValueProvider)[] = [],
43+
entryComponents: Type<any>[] = []): ComponentFixture<any> {
4044

4145
TestBed.configureTestingModule({
4246
imports: [
@@ -49,7 +53,13 @@ describe('MatDatepicker', () => {
4953
...imports
5054
],
5155
providers,
52-
declarations: [component],
56+
declarations: [component, ...entryComponents],
57+
});
58+
59+
TestBed.overrideModule(BrowserDynamicTestingModule, {
60+
set: {
61+
entryComponents: [entryComponents]
62+
}
5363
}).compileComponents();
5464

5565
return TestBed.createComponent(component);
@@ -1489,6 +1499,35 @@ describe('MatDatepicker', () => {
14891499
}));
14901500

14911501
});
1502+
1503+
describe('datepicker with custom header', () => {
1504+
let fixture: ComponentFixture<DatepickerWithCustomHeader>;
1505+
let testComponent: DatepickerWithCustomHeader;
1506+
1507+
beforeEach(fakeAsync(() => {
1508+
fixture = createComponent(
1509+
DatepickerWithCustomHeader,
1510+
[MatNativeDateModule],
1511+
[],
1512+
[CustomHeaderForDatepicker]
1513+
);
1514+
fixture.detectChanges();
1515+
testComponent = fixture.componentInstance;
1516+
}));
1517+
1518+
it('should instantiate a datepicker with a custom header', fakeAsync(() => {
1519+
expect(testComponent).toBeTruthy();
1520+
}));
1521+
1522+
it('should find the standard header element', fakeAsync(() => {
1523+
testComponent.datepicker.open();
1524+
fixture.detectChanges();
1525+
flush();
1526+
fixture.detectChanges();
1527+
1528+
expect(document.querySelector('mat-calendar-header')).toBeTruthy();
1529+
}));
1530+
});
14921531
});
14931532

14941533

@@ -1736,3 +1775,22 @@ class DatepickerWithEvents {
17361775
class DatepickerOpeningOnFocus {
17371776
@ViewChild(MatDatepicker) datepicker: MatDatepicker<Date>;
17381777
}
1778+
1779+
1780+
@Component({
1781+
template: `
1782+
<input [matDatepicker]="ch">
1783+
<mat-datepicker #ch [calendarHeaderComponent]="CustomHeaderForDatepicker"></mat-datepicker>
1784+
`,
1785+
})
1786+
class DatepickerWithCustomHeader {
1787+
@ViewChild('ch') datepicker: MatDatepicker<Date>;
1788+
}
1789+
1790+
@Component({
1791+
template: `
1792+
<div>Custom element</div>
1793+
<mat-calendar-header></mat-calendar-header>
1794+
`,
1795+
})
1796+
class CustomHeaderForDatepicker {}

tools/package-tools/rollup-globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const rollupGlobals = {
3838
'@angular/platform-browser': 'ng.platformBrowser',
3939
'@angular/platform-server': 'ng.platformServer',
4040
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
41+
'@angular/platform-browser-dynamic/testing': 'ng.platformBrowserDynamic.testing',
4142
'@angular/platform-browser/animations': 'ng.platformBrowser.animations',
4243
'@angular/core/testing': 'ng.core.testing',
4344
'@angular/common/testing': 'ng.common.testing',

0 commit comments

Comments
 (0)