Skip to content

Commit 3c5e671

Browse files
crisbetojosephperrott
authored andcommitted
fix(datepicker): toggle throwing an error if datepicker is not defined on init (#15256)
Fixes the `mat-datepicker-toggle` throwing an error if the `datepicker` undefined on init. We already have guards for this in all of the other cases, but it was missing in this one.
1 parent 78e7207 commit 3c5e671

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/lib/datepicker/datepicker-toggle.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export class MatDatepickerToggle<D> implements AfterContentInit, OnChanges, OnDe
6666
/** Whether the toggle button is disabled. */
6767
@Input()
6868
get disabled(): boolean {
69-
return this._disabled === undefined ? this.datepicker.disabled : !!this._disabled;
69+
if (this._disabled === undefined && this.datepicker) {
70+
return this.datepicker.disabled;
71+
}
72+
73+
return !!this._disabled;
7074
}
7175
set disabled(value: boolean) {
7276
this._disabled = coerceBooleanProperty(value);

src/lib/datepicker/datepicker.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,15 @@ describe('MatDatepicker', () => {
15381538
});
15391539
});
15401540

1541+
describe('datepicker toggle without a datepicker', () => {
1542+
it('should not throw on init if toggle does not have a datepicker', () => {
1543+
expect(() => {
1544+
const fixture = createComponent(DatepickerToggleWithNoDatepicker, [MatNativeDateModule]);
1545+
fixture.detectChanges();
1546+
}).not.toThrow();
1547+
});
1548+
});
1549+
15411550
describe('popup positioning', () => {
15421551
let fixture: ComponentFixture<StandardDatepicker>;
15431552
let testComponent: StandardDatepicker;
@@ -1963,7 +1972,6 @@ class DelayedDatepicker {
19631972
}
19641973

19651974

1966-
19671975
@Component({
19681976
template: `
19691977
<input [matDatepicker]="d">
@@ -1974,3 +1982,11 @@ class DelayedDatepicker {
19741982
`,
19751983
})
19761984
class DatepickerWithTabindexOnToggle {}
1985+
1986+
1987+
@Component({
1988+
template: `
1989+
<mat-datepicker-toggle></mat-datepicker-toggle>
1990+
`,
1991+
})
1992+
class DatepickerToggleWithNoDatepicker {}

0 commit comments

Comments
 (0)