Skip to content

Commit aca2533

Browse files
authored
fix(datepicker): prevent default dialog options from affecting… (#18657)
When the datepicker is opened in touch mode, we use a `MatDialog` which means that the `MAT_DIALOG_DEFAULT_OPTIONS` injection has an effect on them. We don't want the default to affect the datepicker's dialog, because they can make it look and behave inconsistently. These changes set values to all of the options which will override the defaults. Fixes #18648.
1 parent c2c7f44 commit aca2533

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/material/datepicker/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ ng_test_library(
8585
"//src/cdk/scrolling",
8686
"//src/cdk/testing/private",
8787
"//src/material/core",
88+
"//src/material/dialog",
8889
"//src/material/form-field",
8990
"//src/material/input",
9091
"//src/material/testing",

src/material/datepicker/datepicker.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
dispatchKeyboardEvent,
1010
dispatchMouseEvent,
1111
} from '@angular/cdk/testing/private';
12-
import {Component, FactoryProvider, Type, ValueProvider, ViewChild} from '@angular/core';
12+
import {Component, Type, ViewChild, Provider} from '@angular/core';
1313
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
1414
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
1515
import {MAT_DATE_LOCALE, MatNativeDateModule, NativeDateModule} from '@angular/material/core';
@@ -18,6 +18,7 @@ import {DEC, JAN, JUL, JUN, SEP} from '@angular/material/testing';
1818
import {By} from '@angular/platform-browser';
1919
import {BrowserDynamicTestingModule} from '@angular/platform-browser-dynamic/testing';
2020
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
21+
import {MAT_DIALOG_DEFAULT_OPTIONS, MatDialogConfig} from '@angular/material/dialog';
2122
import {Subject} from 'rxjs';
2223
import {MatInputModule} from '../input/index';
2324
import {MatDatepicker} from './datepicker';
@@ -32,7 +33,7 @@ describe('MatDatepicker', () => {
3233
function createComponent(
3334
component: Type<any>,
3435
imports: Type<any>[] = [],
35-
providers: (FactoryProvider | ValueProvider)[] = [],
36+
providers: Provider[] = [],
3637
entryComponents: Type<any>[] = []): ComponentFixture<any> {
3738

3839
TestBed.configureTestingModule({
@@ -1761,6 +1762,24 @@ describe('MatDatepicker', () => {
17611762
}));
17621763
});
17631764

1765+
it('should not pick up values from the global dialog config', () => {
1766+
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
1767+
provide: MAT_DIALOG_DEFAULT_OPTIONS,
1768+
useValue: {
1769+
minWidth: '1337px',
1770+
hasBackdrop: false
1771+
} as MatDialogConfig
1772+
}]);
1773+
fixture.componentInstance.touch = true;
1774+
fixture.detectChanges();
1775+
fixture.componentInstance.datepicker.open();
1776+
fixture.detectChanges();
1777+
1778+
const overlay = document.querySelector('.cdk-overlay-pane') as HTMLElement;
1779+
expect(document.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
1780+
expect(overlay.style.minWidth).toBeFalsy();
1781+
});
1782+
17641783
});
17651784

17661785

src/material/datepicker/datepicker.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,20 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
424424
direction: this._dir ? this._dir.value : 'ltr',
425425
viewContainerRef: this._viewContainerRef,
426426
panelClass: 'mat-datepicker-dialog',
427+
428+
// These values are all the same as the defaults, but we set them explicitly so that the
429+
// datepicker dialog behaves consistently even if the user changed the defaults.
430+
hasBackdrop: true,
431+
disableClose: false,
432+
width: '',
433+
height: '',
434+
minWidth: '',
435+
minHeight: '',
436+
maxWidth: '80vw',
437+
maxHeight: '',
438+
position: {},
439+
autoFocus: true,
440+
restoreFocus: true
427441
});
428442

429443
this._dialogRef.afterClosed().subscribe(() => this.close());

0 commit comments

Comments
 (0)