This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 877
/
Copy pathsimple-select.component.ts
78 lines (66 loc) · 1.6 KB
/
simple-select.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// #docplaster
// #docregion
import {
Component,
ContentChild,
EventEmitter,
Input,
Output,
TemplateRef
} from '@angular/core';
interface ItemContext {
item: any;
index: number;
}
// #docregion component
// #docregion metadata
@Component({
selector: 'simple-select',
template:
`
<button (click)="toggleItems()" class="select-root">
<template
[ngTemplateOutlet]="itemTemplateRef"
[ngOutletContext]="{ item: value, index: -1 }">
</template>
</button>
<ul *ngIf="isShowingItems" class="select-items">
<li *ngFor="let item of items ; let index = index" (click)="selectItem(item)">
<template
[ngTemplateOutlet]="itemTemplateRef"
[ngOutletContext]="{ item: item, index: index }">
</template>
</li>
</ul>
`
})
// #enddocregion metadata
export class SimpleSelectComponent {
isShowingItems = false;
itemTemplateRef: TemplateRef<ItemContext>;
@Input() items: any[] = [];
@Input() value: any;
@Output() valueChange = new EventEmitter<any>();
// #docregion setters
@Input()
set template(newTemplate: TemplateRef<ItemContext>) {
if (newTemplate) {
this.itemTemplateRef = newTemplate;
}
}
@ContentChild(TemplateRef)
set contentChildTemplateRef(newTemplate: TemplateRef<ItemContext>) {
if (newTemplate) {
this.itemTemplateRef = newTemplate;
}
}
// #enddocregion setters
selectItem(item: any): void {
this.valueChange.emit(item);
this.toggleItems();
}
toggleItems(): void {
this.isShowingItems = ! this.isShowingItems;
}
}
// #enddocregion component