1
- import { CSSType , Color , CssProperty , EventData , ImageSource , Property , Style , View , booleanConverter } from '@nativescript/core' ;
1
+ import { CSSType , CoercibleProperty , Color , CssProperty , EventData , ImageSource , Property , Style , View , booleanConverter } from '@nativescript/core' ;
2
2
import { cssProperty } from '@nativescript-community/ui-material-core' ;
3
3
4
4
/**
@@ -47,6 +47,32 @@ export enum TitleVisibility {
47
47
never = 2
48
48
}
49
49
50
+ export const selectedTabIndexProperty = new CoercibleProperty < BottomNavigationBarBase , number > ( {
51
+ name : 'selectedTabIndex' ,
52
+ defaultValue : - 1 ,
53
+ affectsLayout : global . isIOS ,
54
+ valueChanged : ( target , oldValue , newValue ) => {
55
+ target . onSelectedTabIndexChanged ( oldValue , newValue ) ;
56
+ } ,
57
+ coerceValue : ( target , value ) => {
58
+ const items = target . items ;
59
+ if ( items ) {
60
+ const max = items . length - 1 ;
61
+ if ( value < 0 ) {
62
+ value = 0 ;
63
+ }
64
+ if ( value > max ) {
65
+ value = max ;
66
+ }
67
+ } else {
68
+ value = - 1 ;
69
+ }
70
+
71
+ return value ;
72
+ } ,
73
+ valueConverter : ( v ) => parseInt ( v , 10 )
74
+ } ) ;
75
+
50
76
@CSSType ( 'BottomNavigationBar' )
51
77
export abstract class BottomNavigationBarBase extends View {
52
78
static tabPressedEvent = 'tabPressed' ;
@@ -58,7 +84,7 @@ export abstract class BottomNavigationBarBase extends View {
58
84
@cssProperty badgeColor : Color ;
59
85
@cssProperty badgeTextColor : Color ;
60
86
61
- selectedTabIndex = 0 ;
87
+ selectedTabIndex : number ;
62
88
titleVisibility : TitleVisibility = TitleVisibility . always ;
63
89
autoClearBadge : boolean ;
64
90
@@ -98,15 +124,7 @@ export abstract class BottomNavigationBarBase extends View {
98
124
}
99
125
100
126
_emitTabSelected ( index : number ) {
101
- const eventData : TabSelectedEventData = {
102
- eventName : BottomNavigationBarBase . tabSelectedEvent ,
103
- object : this ,
104
- oldIndex : this . selectedTabIndex ,
105
- newIndex : index
106
- } ;
107
- this . selectedTabIndex = index ;
108
- this . notify ( eventData ) ;
109
-
127
+ this . onSelectedTabIndexChanged ( this . selectedTabIndex , index ) ;
110
128
if ( this . autoClearBadge ) {
111
129
this . removeBadge ( index ) ;
112
130
}
@@ -138,8 +156,23 @@ export abstract class BottomNavigationBarBase extends View {
138
156
this . _items [ index ] && this . _items [ index ] . removeBadge ( ) ;
139
157
}
140
158
159
+ [ selectedTabIndexProperty . setNative ] ( value : number ) {
160
+ this . selectTab ( value ) ;
161
+ }
162
+
141
163
protected abstract selectTabNative ( index : number ) : void ;
142
164
protected abstract createTabs ( tabs : BottomNavigationTabBase [ ] | undefined ) : void ;
165
+
166
+ public onSelectedTabIndexChanged ( oldIndex : number , newIndex : number ) : void {
167
+ this . selectedTabIndex = newIndex ;
168
+ // to be overridden in platform specific files
169
+ this . notify ( {
170
+ eventName : BottomNavigationBarBase . tabSelectedEvent ,
171
+ object : this ,
172
+ oldIndex,
173
+ newIndex
174
+ } as TabSelectedEventData ) ;
175
+ }
143
176
}
144
177
145
178
export const tabsProperty = new Property < BottomNavigationBarBase , BottomNavigationTabBase [ ] > ( {
@@ -195,6 +228,7 @@ export const badgeTextColorCssProperty = new CssProperty<Style, Color>({
195
228
valueConverter : ( v ) => new Color ( v )
196
229
} ) ;
197
230
badgeTextColorCssProperty . register ( Style ) ;
231
+ selectedTabIndexProperty . register ( BottomNavigationBarBase ) ;
198
232
199
233
// Bottom Navigation Tab
200
234
0 commit comments