File tree 2 files changed +27
-11
lines changed
projects/coreui-angular/src/lib/tabs-2
2 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -7,9 +7,12 @@ import {
7
7
effect ,
8
8
ElementRef ,
9
9
inject ,
10
+ Injector ,
10
11
Input ,
11
12
input ,
12
13
InputSignal ,
14
+ OnInit ,
15
+ runInInjectionContext ,
13
16
signal ,
14
17
untracked
15
18
} from '@angular/core' ;
@@ -32,7 +35,8 @@ import { TabsService } from '../tabs.service';
32
35
'[tabindex]' : 'isActive() ? 0 : -1'
33
36
}
34
37
} )
35
- export class TabDirective implements FocusableOption {
38
+ export class TabDirective implements FocusableOption , OnInit {
39
+ readonly #injector = inject ( Injector ) ;
36
40
readonly #destroyRef = inject ( DestroyRef ) ;
37
41
readonly #elementRef = inject ( ElementRef ) ;
38
42
readonly #tabsService = inject ( TabsService ) ;
@@ -77,9 +81,7 @@ export class TabDirective implements FocusableOption {
77
81
alias : 'aria-controls'
78
82
} ) ;
79
83
80
- readonly isActive = computed < boolean > (
81
- ( ) => ! this . #disabled( ) && this . #tabsService. activeItemKey ( ) === this . itemKey ( )
82
- ) ;
84
+ readonly isActive = signal ( false ) ;
83
85
84
86
readonly hostClasses = computed ( ( ) => ( {
85
87
'nav-link' : true ,
@@ -114,4 +116,13 @@ export class TabDirective implements FocusableOption {
114
116
focus ( origin ?: FocusOrigin ) : void {
115
117
this . #elementRef. nativeElement . focus ( ) ;
116
118
}
119
+
120
+ ngOnInit ( ) : void {
121
+ runInInjectionContext ( this . #injector, ( ) => {
122
+ effect ( ( ) => {
123
+ const isActive = ! this . #disabled( ) && this . #tabsService. activeItemKey ( ) === this . itemKey ( ) ;
124
+ this . isActive . set ( isActive ) ;
125
+ } ) ;
126
+ } ) ;
127
+ }
117
128
}
Original file line number Diff line number Diff line change @@ -60,10 +60,11 @@ export class TabsListComponent {
60
60
#focusKeyManager! : FocusKeyManager < TabDirective > ;
61
61
62
62
readonly tabsEffect = effect ( ( ) => {
63
- if ( this . tabs ( ) . length === 0 ) {
63
+ const tabs = this . tabs ( ) ;
64
+ if ( tabs . length === 0 ) {
64
65
return ;
65
66
}
66
- this . #focusKeyManager = new FocusKeyManager ( this . tabs ( ) )
67
+ this . #focusKeyManager = new FocusKeyManager ( tabs )
67
68
. skipPredicate ( ( tab ) => tab . disabled === true )
68
69
. withHorizontalOrientation ( 'ltr' )
69
70
. withHomeAndEnd ( )
@@ -79,11 +80,15 @@ export class TabsListComponent {
79
80
)
80
81
. subscribe ( ) ;
81
82
82
- const activeItem = this . tabs ( ) . find ( ( tab ) => untracked ( tab . isActive ) ) ?? this . tabs ( ) . find ( ( tab ) => ! tab . disabled ) ;
83
- const activeItemIndex = this . tabs ( ) . findIndex ( ( tab ) => tab === activeItem ) ;
84
- this . #focusKeyManager?. updateActiveItem ( activeItemIndex < 0 ? 0 : activeItemIndex ) ;
85
- this . tabsService . activeItemKey . set ( this . #focusKeyManager. activeItem ?. itemKey ( ) ) ;
86
- this . tabsService . activeItem . set ( this . #focusKeyManager. activeItem ) ;
83
+ untracked ( ( ) => {
84
+ setTimeout ( ( ) => {
85
+ const activeItem = tabs . find ( ( tab ) => tab . isActive ( ) ) ?? tabs . find ( ( tab ) => ! tab . disabled ) ;
86
+ const activeItemIndex = tabs . findIndex ( ( tab ) => tab === activeItem ) ;
87
+ this . #focusKeyManager?. updateActiveItem ( activeItemIndex < 0 ? 0 : activeItemIndex ) ;
88
+ this . tabsService . activeItemKey . set ( this . #focusKeyManager. activeItem ?. itemKey ( ) ) ;
89
+ this . tabsService . activeItem . set ( this . #focusKeyManager. activeItem ) ;
90
+ } ) ;
91
+ } ) ;
87
92
} ) ;
88
93
89
94
tabsServiceEffect = effect ( ( ) => {
You can’t perform that action at this time.
0 commit comments