Skip to content

Commit

Permalink
fix(overlay): default options not being applied correctly
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
crisbeto committed Dec 21, 2017
1 parent d773102 commit 79f869f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/cdk/overlay/overlay-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
10 changes: 10 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 3 additions & 6 deletions src/cdk/overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 79f869f

Please sign in to comment.