forked from ng-bootstrap/ng-bootstrap
/
tabset.ts
210 lines (184 loc) · 6.26 KB
/
tabset.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {
Component,
Input,
ContentChildren,
QueryList,
Directive,
TemplateRef,
AfterContentChecked,
Output,
EventEmitter
} from '@angular/core';
import {NgbTabsetConfig} from './tabset-config';
let nextId = 0;
/**
* A directive to wrap tab titles that need to contain HTML markup or other directives.
*
* Alternatively you could use the `NgbTab.title` input for string titles.
*/
@Directive({selector: 'ng-template[ngbTabTitle]'})
export class NgbTabTitle {
constructor(public templateRef: TemplateRef<any>) {}
}
/**
* A directive to wrap content to be displayed in a tab.
*/
@Directive({selector: 'ng-template[ngbTabContent]'})
export class NgbTabContent {
constructor(public templateRef: TemplateRef<any>) {}
}
/**
* A directive representing an individual tab.
*/
@Directive({selector: 'ngb-tab'})
export class NgbTab implements AfterContentChecked {
/**
* The tab identifier.
*
* Must be unique for the entire document for proper accessibility support.
*/
@Input() id = `ngb-tab-${nextId++}`;
/**
* The tab title.
*
* Use the [`NgbTabTitle`](#/components/tabset/api#NgbTabTitle) directive for non-string titles.
*/
@Input() title: string;
/**
* If `true`, the current tab is disabled and can't be toggled.
*/
@Input() disabled = false;
titleTpl: NgbTabTitle | null;
contentTpl: NgbTabContent | null;
@ContentChildren(NgbTabTitle, {descendants: false}) titleTpls: QueryList<NgbTabTitle>;
@ContentChildren(NgbTabContent, {descendants: false}) contentTpls: QueryList<NgbTabContent>;
ngAfterContentChecked() {
// We are using @ContentChildren instead of @ContentChild as in the Angular version being used
// only @ContentChildren allows us to specify the {descendants: false} option.
// Without {descendants: false} we are hitting bugs described in:
// https://github.com/ng-bootstrap/ng-bootstrap/issues/2240
this.titleTpl = this.titleTpls.first;
this.contentTpl = this.contentTpls.first;
}
}
/**
* The payload of the change event fired right before the tab change.
*/
export interface NgbTabChangeEvent {
/**
* The id of the currently active tab.
*/
activeId: string;
/**
* The id of the newly selected tab.
*/
nextId: string;
/**
* Calling this function will prevent tab switching.
*/
preventDefault: () => void;
}
/**
* A component that makes it easy to create tabbed interface.
*/
@Component({
selector: 'ngb-tabset',
exportAs: 'ngbTabset',
template: `
<ul [class]="'nav nav-' + type + (orientation == 'horizontal'? ' ' + justifyClass : ' flex-column')" role="tablist">
<li class="nav-item" *ngFor="let tab of tabs">
<a [id]="tab.id" class="nav-link" [class.active]="tab.id === activeId" [class.disabled]="tab.disabled"
href (click)="select(tab.id); $event.preventDefault()" role="tab" [attr.tabindex]="(tab.disabled ? '-1': undefined)"
[attr.aria-controls]="(!destroyOnHide || tab.id === activeId ? tab.id + '-panel' : null)"
[attr.aria-selected]="tab.id === activeId" [attr.aria-disabled]="tab.disabled">
{{tab.title}}<ng-template [ngTemplateOutlet]="tab.titleTpl?.templateRef"></ng-template>
</a>
</li>
</ul>
<div class="tab-content">
<ng-template ngFor let-tab [ngForOf]="tabs">
<div
class="tab-pane {{tab.id === activeId ? 'active' : null}}"
*ngIf="!destroyOnHide || tab.id === activeId"
role="tabpanel"
[attr.aria-labelledby]="tab.id" id="{{tab.id}}-panel">
<ng-template [ngTemplateOutlet]="tab.contentTpl?.templateRef"></ng-template>
</div>
</ng-template>
</div>
`
})
export class NgbTabset implements AfterContentChecked {
justifyClass: string;
@ContentChildren(NgbTab) tabs: QueryList<NgbTab>;
/**
* The identifier of the tab that should be opened **initially**.
*
* For subsequent tab switches use the `.select()` method and the `(tabChange)` event.
*/
@Input() activeId: string;
/**
* If `true`, non-visible tabs content will be removed from DOM. Otherwise it will just be hidden.
*/
@Input() destroyOnHide = true;
/**
* The horizontal alignment of the tabs with flexbox utilities.
*/
@Input()
set justify(className: 'start' | 'center' | 'end' | 'fill' | 'justified') {
if (className === 'fill' || className === 'justified') {
this.justifyClass = `nav-${className}`;
} else {
this.justifyClass = `justify-content-${className}`;
}
}
/**
* The orientation of the tabset.
*/
@Input() orientation: 'horizontal' | 'vertical';
/**
* Type of navigation to be used for tabs.
*
* Currently Bootstrap supports only `"tabs"` and `"pills"`.
*
* Since `3.0.0` can also be an arbitrary string (ex. for custom themes).
*/
@Input() type: 'tabs' | 'pills' | string;
/**
* A tab change event emitted right before the tab change happens.
*
* See [`NgbTabChangeEvent`](#/components/tabset/api#NgbTabChangeEvent) for payload details.
*/
@Output() tabChange = new EventEmitter<NgbTabChangeEvent>();
constructor(config: NgbTabsetConfig) {
this.type = config.type;
this.justify = config.justify;
this.orientation = config.orientation;
}
/**
* Selects the tab with the given id and shows its associated content panel.
*
* Any other tab that was previously selected becomes unselected and its associated pane is removed from DOM or
* hidden depending on the `destroyOnHide` value.
*/
select(tabId: string) {
let selectedTab = this._getTabById(tabId);
if (selectedTab && !selectedTab.disabled && this.activeId !== selectedTab.id) {
let defaultPrevented = false;
this.tabChange.emit(
{activeId: this.activeId, nextId: selectedTab.id, preventDefault: () => { defaultPrevented = true; }});
if (!defaultPrevented) {
this.activeId = selectedTab.id;
}
}
}
ngAfterContentChecked() {
// auto-correct activeId that might have been set incorrectly as input
let activeTab = this._getTabById(this.activeId);
this.activeId = activeTab ? activeTab.id : (this.tabs.length ? this.tabs.first.id : null);
}
private _getTabById(id: string): NgbTab {
let tabsWithId: NgbTab[] = this.tabs.filter(tab => tab.id === id);
return tabsWithId.length ? tabsWithId[0] : null;
}
}