Skip to content

Commit b153947

Browse files
crisbetojelbourn
authored andcommitted
fix(overlay): default options not being applied correctly (#9088)
* Fixes the overlay defaults not being applied correctly when the passed in object isn't an instance of the `OverlayConfig`. This caused some issues like the `dir` being set to `"undefined"` if the consumer didn't specify it. * Tweaks the logic that applies the overlay config defaults to skip keys with `undefined` values.
1 parent 3d18006 commit b153947

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/cdk/overlay/overlay-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export class OverlayConfig {
5252

5353
constructor(config?: OverlayConfig) {
5454
if (config) {
55-
Object.keys(config).forEach(key => this[key] = config[key]);
55+
Object.keys(config)
56+
.filter(key => typeof config[key] !== 'undefined')
57+
.forEach(key => this[key] = config[key]);
5658
}
5759
}
5860
}

src/cdk/overlay/overlay.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ describe('Overlay', () => {
239239
expect(callbackOrder).toEqual(['attach', 'detach']);
240240
});
241241

242+
it('should default to the ltr direction', () => {
243+
const overlayRef = overlay.create({hasBackdrop: true});
244+
expect(overlayRef.getConfig().direction).toBe('ltr');
245+
});
246+
247+
it('should skip undefined values when applying the defaults', () => {
248+
const overlayRef = overlay.create({direction: undefined});
249+
expect(overlayRef.getConfig().direction).toBe('ltr');
250+
});
251+
242252
describe('positioning', () => {
243253
let config: OverlayConfig;
244254

src/cdk/overlay/overlay.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import {DOCUMENT} from '@angular/common';
2727
/** Next overlay unique ID. */
2828
let nextUniqueId = 0;
2929

30-
/** The default config for newly created overlays. */
31-
let defaultConfig = new OverlayConfig();
32-
33-
3430
/**
3531
* Service to create Overlays. Overlays are dynamically added pieces of floating UI, meant to be
3632
* used as a low-level building building block for other components. Dialogs, tooltips, menus,
@@ -58,10 +54,11 @@ export class Overlay {
5854
* @param config Configuration applied to the overlay.
5955
* @returns Reference to the created overlay.
6056
*/
61-
create(config: OverlayConfig = defaultConfig): OverlayRef {
57+
create(config?: OverlayConfig): OverlayRef {
6258
const pane = this._createPaneElement();
6359
const portalOutlet = this._createPortalOutlet(pane);
64-
return new OverlayRef(portalOutlet, pane, config, this._ngZone, this._keyboardDispatcher);
60+
return new OverlayRef(portalOutlet, pane, new OverlayConfig(config), this._ngZone,
61+
this._keyboardDispatcher);
6562
}
6663

6764
/**

0 commit comments

Comments
 (0)