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' ;
22import { cssProperty } from '@nativescript-community/ui-material-core' ;
33
44/**
@@ -47,6 +47,32 @@ export enum TitleVisibility {
4747 never = 2
4848}
4949
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+
5076@CSSType ( 'BottomNavigationBar' )
5177export abstract class BottomNavigationBarBase extends View {
5278 static tabPressedEvent = 'tabPressed' ;
@@ -58,7 +84,7 @@ export abstract class BottomNavigationBarBase extends View {
5884 @cssProperty badgeColor : Color ;
5985 @cssProperty badgeTextColor : Color ;
6086
61- selectedTabIndex = 0 ;
87+ selectedTabIndex : number ;
6288 titleVisibility : TitleVisibility = TitleVisibility . always ;
6389 autoClearBadge : boolean ;
6490
@@ -98,15 +124,7 @@ export abstract class BottomNavigationBarBase extends View {
98124 }
99125
100126 _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 ) ;
110128 if ( this . autoClearBadge ) {
111129 this . removeBadge ( index ) ;
112130 }
@@ -138,8 +156,23 @@ export abstract class BottomNavigationBarBase extends View {
138156 this . _items [ index ] && this . _items [ index ] . removeBadge ( ) ;
139157 }
140158
159+ [ selectedTabIndexProperty . setNative ] ( value : number ) {
160+ this . selectTab ( value ) ;
161+ }
162+
141163 protected abstract selectTabNative ( index : number ) : void ;
142164 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+ }
143176}
144177
145178export const tabsProperty = new Property < BottomNavigationBarBase , BottomNavigationTabBase [ ] > ( {
@@ -195,6 +228,7 @@ export const badgeTextColorCssProperty = new CssProperty<Style, Color>({
195228 valueConverter : ( v ) => new Color ( v )
196229} ) ;
197230badgeTextColorCssProperty . register ( Style ) ;
231+ selectedTabIndexProperty . register ( BottomNavigationBarBase ) ;
198232
199233// Bottom Navigation Tab
200234
0 commit comments