@@ -17,19 +17,14 @@ import {
1717 ViewChild ,
1818 ViewChildren
1919} from '@angular/core' ;
20-
2120import type { TemplateRef , QueryList } from '@angular/core' ;
2221
23- import { TabToggleDirective } from './tabs.directive ' ;
22+ import { Subscription } from 'rxjs ' ;
2423
24+ import { TabToggleDirective } from './tabs.directive' ;
2525import { removeListeners , removeSubscriptions } from '../helpers' ;
26- import { Subscription } from 'rxjs' ;
2726
28- // @TODO Vertical tabs
2927
30- /*
31- *
32- */
3328@Component ( {
3429 selector : 'mk-tab-header' ,
3530 template : '<ng-template #templateRef><ng-content></ng-content></ng-template>' ,
@@ -40,9 +35,6 @@ export class TabHeaderComponent {
4035}
4136
4237
43- /*
44- *
45- */
4638@Component ( {
4739 selector : 'mk-tab-content' ,
4840 template : '<ng-template #templateRef><ng-content></ng-content></ng-template>' ,
@@ -52,44 +44,27 @@ export class TabContentComponent {
5244 @ViewChild ( 'templateRef' , { static : true } ) public templateRef ! : TemplateRef < ElementRef > ;
5345}
5446
55- /*
56- *
57- */
47+
5848@Component ( {
5949 selector : 'mk-tab' ,
6050 template : '<ng-template #templateRef><ng-content></ng-content></ng-template>' ,
6151 changeDetection : ChangeDetectionStrategy . OnPush
6252} )
63- export class TabComponent implements AfterContentInit {
64- public index ! : number ;
65- public isActive = false ;
66-
67- public contentTemplateRef ! : TemplateRef < ElementRef > ;
68-
53+ export class TabComponent {
6954 @Input ( ) public header ?: string ;
7055 @Input ( ) public isDisabled = false ;
7156 @Input ( ) public tabColor ?: string ;
7257
7358 @ViewChild ( 'templateRef' , { static : true } ) public templateRef ! : TemplateRef < ElementRef > ;
7459
75- @ContentChild ( TabHeaderComponent ) public tabHeaderComponent ! : TabHeaderComponent ;
60+ @ContentChild ( TabHeaderComponent ) public tabHeaderComponent ? : TabHeaderComponent ;
7661 @ContentChild ( TabContentComponent ) public tabContentComponent ?: TabContentComponent ;
7762
78- /**
79- * @method ngAfterContentInit
80- */
81- ngAfterContentInit ( ) : void {
82- if ( this . tabContentComponent ) {
83- this . contentTemplateRef = this . tabContentComponent . templateRef ;
84- } else {
85- this . contentTemplateRef = this . templateRef ;
86- }
87- }
63+ public index ! : number ;
64+ public isActive = false ;
8865}
8966
90- /*
91- *
92- */
67+
9368@Component ( {
9469 selector : 'mk-tabs-header' ,
9570 template : '<ng-template #templateRef><ng-content></ng-content></ng-template>' ,
@@ -99,20 +74,14 @@ export class TabsHeaderComponent {
9974 @ViewChild ( 'templateRef' , { static : true } ) public templateRef ! : TemplateRef < ElementRef > ;
10075}
10176
102- /*
103- *
104- */
77+
10578@Component ( {
10679 selector : 'mk-tabs' ,
10780 templateUrl : './tabs.component.html' ,
10881 styleUrls : [ './tabs.component.css' ] ,
10982 changeDetection : ChangeDetectionStrategy . OnPush
11083} )
11184export class TabsComponent implements AfterContentInit , AfterViewInit , OnChanges , OnDestroy {
112- private activatedTabIndex ?: number ;
113- private listeners : ( ( ) => void ) [ ] = [ ] ;
114- private subscriptions : Subscription [ ] = [ ] ;
115-
11685 @Input ( ) public set activeTabIndex ( index : number ) {
11786 this . activatedTabIndex = index ;
11887 this . changeDetectorRef . detectChanges ( ) ;
@@ -127,43 +96,37 @@ export class TabsComponent implements AfterContentInit, AfterViewInit, OnChanges
12796 @Output ( ) public closeTab = new EventEmitter ( ) ;
12897 @Output ( ) public openTab = new EventEmitter ( ) ;
12998
130- @ContentChild ( TabsHeaderComponent , { static : true } ) public tabsHeaderComponent ! : TabsHeaderComponent ;
99+ @ContentChild ( TabsHeaderComponent ) public tabsHeaderComponent ? : TabsHeaderComponent ;
131100
132- @ContentChildren ( TabComponent ) public tabs ! : QueryList < TabComponent > ;
101+ @ContentChildren ( TabComponent ) public tabs ? : QueryList < TabComponent > ;
133102
134103 @ViewChildren ( TabToggleDirective ) public tabToggleDirectives ?: QueryList < TabToggleDirective > ;
135104
136- /**
137- * @method constructor
138- * @param changeDetectorRef [description]
139- * @param ngZone [description]
140- * @param renderer2 [description]
141- */
105+ private activatedTabIndex ?: number ;
106+ private listeners : ( ( ) => void ) [ ] = [ ] ;
107+ private subscriptions : Subscription [ ] = [ ] ;
108+
142109 constructor (
143110 private changeDetectorRef : ChangeDetectorRef ,
144111 private ngZone : NgZone ,
145112 private renderer2 : Renderer2
146113 ) { }
147114
148- /**
149- * @method ngAfterViewInit
150- */
151115 ngAfterContentInit ( ) : void {
152116 // Set tab index on load.
153117 this . setTabIndex ( ) ;
154118
155119 // Update tab index if tabs is updated.
156- this . subscriptions . push ( this . tabs . changes . subscribe ( ( ) => {
157- this . setTabIndex ( ) ;
158- } ) ) ;
120+ if ( this . tabs ) {
121+ this . subscriptions . push ( this . tabs . changes . subscribe ( ( ) => {
122+ this . setTabIndex ( ) ;
123+ } ) ) ;
124+ }
159125
160126 // Open tab on load.
161127 this . openTabIndex ( ) ;
162128 }
163129
164- /**
165- * @method ngAfterViewInit
166- */
167130 ngAfterViewInit ( ) : void {
168131 // Set tab toggles on load.
169132 this . setTabsToggle ( ) ;
@@ -176,28 +139,17 @@ export class TabsComponent implements AfterContentInit, AfterViewInit, OnChanges
176139 }
177140 }
178141
179- /**
180- * @method ngOnChanges
181- * @param changes [description]
182- */
183142 ngOnChanges ( changes : { [ propKey : string ] : SimpleChange } ) : void {
184143 if ( changes . activeTabIndex ) {
185144 this . openTabIndex ( ) ;
186145 }
187146 }
188147
189- /**
190- * @method ngOnDestroy
191- */
192148 ngOnDestroy ( ) : void {
193149 removeListeners ( this . listeners ) ;
194150 removeSubscriptions ( this . subscriptions ) ;
195151 }
196152
197- /**
198- * [toggleTab description]
199- * @method toggleTab
200- */
201153 public openTabIndex ( ) : void {
202154 if ( this . tabs ) {
203155 this . tabs . forEach ( ( tab : TabComponent ) => {
@@ -214,39 +166,31 @@ export class TabsComponent implements AfterContentInit, AfterViewInit, OnChanges
214166 }
215167 }
216168
217- /**
218- * [openTab description]
219- * @method openTab
220- * @param event [description]
221- * @param tabToOpen [description]
222- */
223169 public onOpenTab ( event : Event , tabToOpen : TabComponent ) : void {
224170 event . preventDefault ( ) ;
225171 tabToOpen . isActive = true ;
226172 this . openTab . emit ( { event, index : tabToOpen . index } ) ;
227- this . tabs . forEach ( ( tab : TabComponent ) => {
228- if ( tab . isActive && tabToOpen !== tab ) {
229- tab . isActive = false ;
230- this . closeTab . emit ( { event, index : tab . index } ) ;
231- }
232- } ) ;
173+
174+ if ( this . tabs ) {
175+ this . tabs . forEach ( ( tab : TabComponent ) => {
176+ if ( tab . isActive && tabToOpen !== tab ) {
177+ tab . isActive = false ;
178+ this . closeTab . emit ( { event, index : tab . index } ) ;
179+ }
180+ } ) ;
181+ }
233182 }
234183
235- /**
236- * [setTabIndex description]
237- * @method setTabIndex
238- */
239184 private setTabIndex ( ) : void {
240- this . tabs . forEach ( ( tab : TabComponent , index : number ) => {
241- tab . index = index ;
242- } ) ;
185+ if ( this . tabs ) {
186+ this . tabs . forEach ( ( tab : TabComponent , index : number ) => {
187+ tab . index = index ;
188+ } ) ;
189+ }
190+
243191 this . changeDetectorRef . detectChanges ( ) ;
244192 }
245193
246- /**
247- * [setTabsToggle description]
248- * @method setTabsToggle
249- */
250194 private setTabsToggle ( ) : void {
251195 this . listeners = removeListeners ( this . listeners ) ;
252196 this . ngZone . runOutsideAngular ( ( ) => {
0 commit comments