Skip to content

Commit c46c0ff

Browse files
jhuntoovalorkin
authored andcommitted
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
1 parent 376c4b4 commit c46c0ff

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

components/select/off-click.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Directive, HostListener, Input, OnInit, OnDestroy} from 'angular2/core';
2+
3+
@Directive({
4+
selector: '[offClick]'
5+
})
6+
7+
export class OffClickDirective implements OnInit, OnDestroy {
8+
/* tslint:disable */
9+
@Input('offClick') public offClickHandler: any;
10+
/* tslint:enable */
11+
@HostListener('click', ['$event']) public onClick($event: MouseEvent): void {
12+
$event.stopPropagation();
13+
}
14+
15+
public ngOnInit(): any {
16+
setTimeout(() => {document.addEventListener('click', this.offClickHandler);}, 0);
17+
}
18+
19+
public ngOnDestroy(): any {
20+
document.removeEventListener('click', this.offClickHandler);
21+
}
22+
23+
}

components/select/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {SELECT_DIRECTIVES} from 'ng2-select/ng2-select';
77
```typescript
88
// class Select
99
@Component({
10-
selector: 'ng2-select',
10+
selector: 'ng-select',
1111
properties: [
1212
'allowClear',
1313
'placeholder',

components/select/select.ts

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {Component, Input, Output, EventEmitter, ElementRef, OnInit, OnDestroy} from 'angular2/core';
1+
import {Component, Input, Output, EventEmitter, ElementRef, OnInit} from 'angular2/core';
22
import {SelectItem} from './select-item';
33
import {HighlightPipe, stripTags} from './select-pipes';
44
import {OptionsBehavior} from './select-interfaces';
55
import {escapeRegexp} from './common';
6+
import {OffClickDirective} from './off-click';
67

78
let optionsTemplate = `
8-
<ul *ngIf="optionsOpened && options && options.length > 0 && !itemObjects[0].hasChildren()"
9+
<ul *ngIf="optionsOpened && options && options.length > 0 && !firstItemHasChildren"
910
class="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu">
1011
<li class="ui-select-choices-group">
1112
<div *ngFor="#o of options"
@@ -20,7 +21,7 @@ let optionsTemplate = `
2021
</li>
2122
</ul>
2223
23-
<ul *ngIf="optionsOpened && options && options.length > 0 && itemObjects[0].hasChildren()"
24+
<ul *ngIf="optionsOpened && options && options.length > 0 && firstItemHasChildren"
2425
class="ui-select-choices ui-select-choices-content ui-select-dropdown dropdown-menu">
2526
<li *ngFor="#c of options; #index=index" class="ui-select-choices-group">
2627
<div class="divider" *ngIf="index > 0"></div>
@@ -41,18 +42,20 @@ let optionsTemplate = `
4142
`;
4243
@Component({
4344
selector: 'ng-select',
45+
directives: [OffClickDirective],
4446
pipes: [HighlightPipe],
4547
template: `
4648
<div tabindex="0"
4749
*ngIf="multiple === false"
4850
(keyup)="mainClick($event)"
51+
[offClick]="clickedOutside"
4952
class="ui-select-container ui-select-bootstrap dropdown open">
5053
<div [ngClass]="{'ui-disabled': disabled}"></div>
5154
<div class="ui-select-match"
5255
*ngIf="!inputMode">
5356
<span tabindex="-1"
5457
class="btn btn-default btn-secondary form-control ui-select-toggle"
55-
(^click)="matchClick()"
58+
(click)="matchClick($event)"
5659
style="outline: 0;">
5760
<span *ngIf="active.length <= 0" class="ui-select-placeholder text-muted">{{placeholder}}</span>
5861
<span *ngIf="active.length > 0" class="ui-select-match-text pull-left"
@@ -110,7 +113,7 @@ let optionsTemplate = `
110113
</div>
111114
`
112115
})
113-
export class Select implements OnInit, OnDestroy {
116+
export class Select implements OnInit {
114117
@Input() public allowClear:boolean = false;
115118
@Input() public placeholder:string = '';
116119
@Input() public initData:Array<any> = [];
@@ -141,7 +144,6 @@ export class Select implements OnInit, OnDestroy {
141144
public activeOption:SelectItem;
142145
public element:ElementRef;
143146

144-
private offSideClickHandler:any;
145147
private inputMode:boolean = false;
146148
private optionsOpened:boolean = false;
147149
private behavior:OptionsBehavior;
@@ -151,6 +153,7 @@ export class Select implements OnInit, OnDestroy {
151153

152154
public constructor(element:ElementRef) {
153155
this.element = element;
156+
this.clickedOutside = this.clickedOutside.bind(this);
154157
}
155158

156159
public inputEvent(e:any, isUpMode:boolean = false):void {
@@ -229,21 +232,14 @@ export class Select implements OnInit, OnDestroy {
229232
}
230233

231234
public ngOnInit():any {
232-
this.behavior = this.itemObjects[0].hasChildren() ?
235+
this.behavior = (this.firstItemHasChildren) ?
233236
new ChildrenBehavior(this) : new GenericBehavior(this);
234-
this.offSideClickHandler = this.getOffSideClickHandler(this);
235-
document.addEventListener('click', this.offSideClickHandler);
236237
if (this.initData) {
237238
this.active = this.initData.map((data:any) => new SelectItem(data));
238239
this.data.emit(this.active);
239240
}
240241
}
241242

242-
public ngOnDestroy():any {
243-
document.removeEventListener('click', this.offSideClickHandler);
244-
this.offSideClickHandler = void 0;
245-
}
246-
247243
public remove(item:SelectItem):void {
248244
if (this._disabled === true) {
249245
return;
@@ -267,6 +263,15 @@ export class Select implements OnInit, OnDestroy {
267263
}
268264
}
269265

266+
public clickedOutside():void {
267+
this.inputMode = false;
268+
this.optionsOpened = false;
269+
}
270+
271+
public get firstItemHasChildren():boolean {
272+
return this.itemObjects[0] && this.itemObjects[0].hasChildren();
273+
}
274+
270275
protected matchClick(e:any):void {
271276
if (this._disabled === true) {
272277
return;
@@ -337,27 +342,6 @@ export class Select implements OnInit, OnDestroy {
337342
this.optionsOpened = true;
338343
}
339344

340-
private getOffSideClickHandler(context:any):Function {
341-
return function (e:any):void {
342-
if (e.target && e.target.nodeName === 'INPUT'
343-
&& e.target.className && e.target.className.indexOf('ui-select') >= 0) {
344-
return;
345-
}
346-
347-
if (e.srcElement.contains(context.element.nativeElement)
348-
&& e.srcElement && e.srcElement.className &&
349-
e.srcElement.className.indexOf('ui-select') >= 0) {
350-
if (e.target.nodeName !== 'INPUT') {
351-
context.matchClick(void 0);
352-
}
353-
return;
354-
}
355-
356-
context.inputMode = false;
357-
context.optionsOpened = false;
358-
};
359-
}
360-
361345
private hideOptions():void {
362346
this.inputMode = false;
363347
this.optionsOpened = false;

0 commit comments

Comments
 (0)