Skip to content

Commit 8b45cd4

Browse files
committed
fix(bottomnavigationbar): initial selectedTabIndex fix
1 parent 4edcf4d commit 8b45cd4

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

demo-vue/app/examples/BottomNavigationBar.vue

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
row="1"
1919
activeColor="blue"
2020
inactiveColor="green"
21+
selectedTabIndex="2"
2122
class="mdi"
2223
@loaded="onbottomNavigationBarLoaded"
2324
@tabPressed="onBottomNavigationTabPressed"

src/bottomnavigationbar/bottomnavigationbar-common.ts

+45-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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';
22
import { 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')
5177
export 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

145178
export const tabsProperty = new Property<BottomNavigationBarBase, BottomNavigationTabBase[]>({
@@ -195,6 +228,7 @@ export const badgeTextColorCssProperty = new CssProperty<Style, Color>({
195228
valueConverter: (v) => new Color(v)
196229
});
197230
badgeTextColorCssProperty.register(Style);
231+
selectedTabIndexProperty.register(BottomNavigationBarBase);
198232

199233
// Bottom Navigation Tab
200234

src/bottomnavigationbar/bottomnavigationbar.android.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { getFullColorStateList, state, stateSets } from '@nativescript-community/ui-material-core/android/utils';
2-
import { Color, fontInternalProperty, ImageSource, Utils } from '@nativescript/core';
1+
import { state, stateSets } from '@nativescript-community/ui-material-core/android/utils';
2+
import { Color, ImageSource, Utils } from '@nativescript/core';
33
import {
44
BottomNavigationBarBase,
55
BottomNavigationTabBase,
66
TitleVisibility,
77
activeColorCssProperty,
8-
badgeColorCssProperty,
9-
badgeTextColorCssProperty,
8+
iconProperty,
109
inactiveColorCssProperty,
1110
tabsProperty,
12-
titleVisibilityProperty,
13-
iconProperty
11+
titleVisibilityProperty
1412
} from './bottomnavigationbar-common';
1513

1614
// Types shortcuts
@@ -122,6 +120,7 @@ export class BottomNavigationBar extends BottomNavigationBarBase {
122120
// Create the tabs before setting the default values for each tab
123121
// We call this method here to create the tabs defined in the xml
124122
this.createTabs(this._items);
123+
this.selectTabNative(this.selectedTabIndex);
125124
}
126125
disposeNativeView(): void {
127126
this.nativeViewProtected.setOnNavigationItemReselectedListener(null);
@@ -205,8 +204,8 @@ export class BottomNavigationBar extends BottomNavigationBarBase {
205204
if (bottomNavigationTabs.size() === 0) {
206205
return;
207206
}
208-
209-
this.nativeView.setSelectedItemId(index);
207+
this.nativeViewProtected.setSelectedItemId(index);
208+
this.selectedTabIndex = index;
210209
}
211210
}
212211

src/bottomnavigationbar/bottomnavigationbar.ios.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
activeColorCssProperty,
88
badgeColorCssProperty,
99
badgeTextColorCssProperty,
10+
iconProperty,
1011
inactiveColorCssProperty,
1112
tabsProperty,
12-
titleVisibilityProperty,
13-
iconProperty
13+
titleVisibilityProperty
1414
} from './bottomnavigationbar-common';
1515

1616
@NativeClass
@@ -154,6 +154,7 @@ export class BottomNavigationBar extends BottomNavigationBarBase {
154154
this.nativeViewProtected.items = new NSArray({ array: bottomNavigationTabs });
155155

156156
// TODO: this is for he v8 runtime. Should not have to need this setTimeout(), find better way.
157+
this.selectTabNative(this.selectedTabIndex);
157158
setTimeout(() => {
158159
this.nativeViewProtected.selectedItem = this.nativeViewProtected.items[this.selectedTabIndex];
159160
}, 0);

0 commit comments

Comments
 (0)