Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output emiters on BdDropdown #1951

Merged
merged 2 commits into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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