From 789f78c2ba86b230d3a937f43c38c902a50771fa Mon Sep 17 00:00:00 2001 From: crisbeto Date: Thu, 21 Dec 2017 20:00:35 +0100 Subject: [PATCH] fix(overlay): default options not being applied correctly * 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. --- src/cdk/overlay/overlay-config.ts | 4 +++- src/cdk/overlay/overlay.spec.ts | 10 ++++++++++ src/cdk/overlay/overlay.ts | 9 +++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/cdk/overlay/overlay-config.ts b/src/cdk/overlay/overlay-config.ts index ed12730a7fff..f9b8bc30f799 100644 --- a/src/cdk/overlay/overlay-config.ts +++ b/src/cdk/overlay/overlay-config.ts @@ -52,7 +52,9 @@ export class OverlayConfig { constructor(config?: OverlayConfig) { if (config) { - Object.keys(config).forEach(key => this[key] = config[key]); + Object.keys(config) + .filter(key => typeof config[key] !== 'undefined') + .forEach(key => this[key] = config[key]); } } } diff --git a/src/cdk/overlay/overlay.spec.ts b/src/cdk/overlay/overlay.spec.ts index 01fc5400ed3a..cba164d6550e 100644 --- a/src/cdk/overlay/overlay.spec.ts +++ b/src/cdk/overlay/overlay.spec.ts @@ -239,6 +239,16 @@ describe('Overlay', () => { expect(callbackOrder).toEqual(['attach', 'detach']); }); + it('should default to the ltr direction', () => { + const overlayRef = overlay.create({hasBackdrop: true}); + expect(overlayRef.getConfig().direction).toBe('ltr'); + }); + + it('should skip undefined values when applying the defaults', () => { + const overlayRef = overlay.create({direction: undefined}); + expect(overlayRef.getConfig().direction).toBe('ltr'); + }); + describe('positioning', () => { let config: OverlayConfig; diff --git a/src/cdk/overlay/overlay.ts b/src/cdk/overlay/overlay.ts index c6e75901f13e..20ef18f06974 100644 --- a/src/cdk/overlay/overlay.ts +++ b/src/cdk/overlay/overlay.ts @@ -27,10 +27,6 @@ import {DOCUMENT} from '@angular/common'; /** Next overlay unique ID. */ let nextUniqueId = 0; -/** The default config for newly created overlays. */ -let defaultConfig = new OverlayConfig(); - - /** * Service to create Overlays. Overlays are dynamically added pieces of floating UI, meant to be * used as a low-level building building block for other components. Dialogs, tooltips, menus, @@ -58,10 +54,11 @@ export class Overlay { * @param config Configuration applied to the overlay. * @returns Reference to the created overlay. */ - create(config: OverlayConfig = defaultConfig): OverlayRef { + create(config?: OverlayConfig): OverlayRef { const pane = this._createPaneElement(); const portalOutlet = this._createPortalOutlet(pane); - return new OverlayRef(portalOutlet, pane, config, this._ngZone, this._keyboardDispatcher); + return new OverlayRef(portalOutlet, pane, new OverlayConfig(config), this._ngZone, + this._keyboardDispatcher); } /**