From c46c0ffaaa2f23c3c81eb0acb9dee40f7bc020f2 Mon Sep 17 00:00:00 2001 From: Jonathon Lee Date: Tue, 26 Apr 2016 11:38:47 +0100 Subject: [PATCH] fix(select): clicking on the input now causes the item list to appear (#154) * fix(README): Update docs with correct selector ng-select (#148) * fix(select): clicking on the input now causes the item list to appear * fix(select): tslint & merge errors --- components/select/off-click.ts | 23 +++++++++++++++ components/select/readme.md | 2 +- components/select/select.ts | 54 ++++++++++++---------------------- 3 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 components/select/off-click.ts diff --git a/components/select/off-click.ts b/components/select/off-click.ts new file mode 100644 index 00000000..6ccef08a --- /dev/null +++ b/components/select/off-click.ts @@ -0,0 +1,23 @@ +import {Directive, HostListener, Input, OnInit, OnDestroy} from 'angular2/core'; + +@Directive({ + selector: '[offClick]' +}) + +export class OffClickDirective implements OnInit, OnDestroy { + /* tslint:disable */ + @Input('offClick') public offClickHandler: any; + /* tslint:enable */ + @HostListener('click', ['$event']) public onClick($event: MouseEvent): void { + $event.stopPropagation(); + } + + public ngOnInit(): any { + setTimeout(() => {document.addEventListener('click', this.offClickHandler);}, 0); + } + + public ngOnDestroy(): any { + document.removeEventListener('click', this.offClickHandler); + } + +} diff --git a/components/select/readme.md b/components/select/readme.md index f445974f..fcf010b8 100644 --- a/components/select/readme.md +++ b/components/select/readme.md @@ -7,7 +7,7 @@ import {SELECT_DIRECTIVES} from 'ng2-select/ng2-select'; ```typescript // class Select @Component({ - selector: 'ng2-select', + selector: 'ng-select', properties: [ 'allowClear', 'placeholder', diff --git a/components/select/select.ts b/components/select/select.ts index 0109ab40..3c7fc2df 100644 --- a/components/select/select.ts +++ b/components/select/select.ts @@ -1,11 +1,12 @@ -import {Component, Input, Output, EventEmitter, ElementRef, OnInit, OnDestroy} from 'angular2/core'; +import {Component, Input, Output, EventEmitter, ElementRef, OnInit} from 'angular2/core'; import {SelectItem} from './select-item'; import {HighlightPipe, stripTags} from './select-pipes'; import {OptionsBehavior} from './select-interfaces'; import {escapeRegexp} from './common'; +import {OffClickDirective} from './off-click'; let optionsTemplate = ` - -