Skip to content

Commit

Permalink
fix(dropdown): fixed onShow and onHidden events for inline dropdown m…
Browse files Browse the repository at this point in the history
…odule (#1951)

* Add output emiters

* fix(dropdown): remove unnecessary event binding and method
  • Loading branch information
cesar-garay authored and valorkin committed May 12, 2017
1 parent d069757 commit ead8d52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<span dropdown (isOpenChange)="toggled($event)">
<span dropdown (onShown)="onShown()" (onHidden)="onHidden()">
<a href dropdownToggle (click)="false">Click me for a dropdown, yo!</a>
<ul *dropdownMenu class="dropdown-menu">
<li *ngFor="let choice of items">
Expand Down
9 changes: 6 additions & 3 deletions demo/src/app/components/+dropdown/demos/basic/basic-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { Component } from '@angular/core';
templateUrl: './basic-link.html'
})
export class DemoDropdownBasicLinkComponent {
public items:string[] = ['The first choice!',
public items: string[] = ['The first choice!',
'And another choice for you.', 'but wait! A third!'];

public toggled(open:boolean):void {
console.log('Dropdown is now: ', open);
public onHidden(): void {
console.log('Dropdown is hidden');
}
public onShown(): void {
console.log('Dropdown is shown');
}
}
22 changes: 13 additions & 9 deletions src/dropdown/bs-dropdown.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
* Emits an event when the popover is shown
*/
@Output() onShown: EventEmitter<any>;

/**
* Emits an event when the popover is hidden
*/
Expand All @@ -112,15 +113,15 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
private _isInited = false;

constructor(private _elementRef: ElementRef,
private _renderer: Renderer,
private _viewContainerRef: ViewContainerRef,
private _cis: ComponentLoaderFactory,
private _config: BsDropdownConfig,
private _state: BsDropdownState) {
private _renderer: Renderer,
private _viewContainerRef: ViewContainerRef,
private _cis: ComponentLoaderFactory,
private _config: BsDropdownConfig,
private _state: BsDropdownState) {
// create dropdown component loader
this._dropdown = this._cis
.createLoader<BsDropdownContainerComponent>(this._elementRef, this._viewContainerRef, this._renderer)
.provide({provide: BsDropdownState, useValue: this._state});
.provide({ provide: BsDropdownState, useValue: this._state });

this.onShown = this._dropdown.onShown;
this.onHidden = this._dropdown.onHidden;
Expand All @@ -133,7 +134,7 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
// fix: seems there are an issue with `routerLinkActive`
// which result in duplicated call ngOnInit without call to ngOnDestroy
// read more: https://github.com/valor-software/ngx-bootstrap/issues/1885
if (this._isInited) {return;}
if (this._isInited) { return; }
this._isInited = true;

this._showInline = !this.container;
Expand All @@ -157,7 +158,7 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
// attach dropdown menu inside of dropdown
if (this._showInline) {
this._state.dropdownMenu
.then((dropdownMenu:BsComponentRef<BsDropdownMenuDirective>) => {
.then((dropdownMenu: BsComponentRef<BsDropdownMenuDirective>) => {
this._inlinedMenu = dropdownMenu.viewContainer.createEmbeddedView(dropdownMenu.templateRef);
});
}
Expand All @@ -175,6 +176,7 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
if (this._showInline) {
this._isInlineOpen = true;
this._state.isOpenChange.emit(true);
this.onShown.emit(true);
return;
}

Expand All @@ -191,13 +193,14 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
this._dropdown
.attach(BsDropdownContainerComponent)
.to(this.container)
.position({attachment: _placement})
.position({ attachment: _placement })
.show({
content: dropdownMenu.templateRef,
placement: _placement
});

this._state.isOpenChange.emit(true);
this.onShown.emit(true);
});
}

Expand All @@ -217,6 +220,7 @@ export class BsDropdownDirective implements OnInit, OnDestroy {
}

this._state.isOpenChange.emit(false);
this.onHidden.emit(true);
}

/**
Expand Down

0 comments on commit ead8d52

Please sign in to comment.