Skip to content

Commit

Permalink
fix: fix crash when change tab bar label during runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
owinter86 committed Jan 21, 2025
1 parent c19fdef commit c2439ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions apps/example/src/Examples/NativeBottomTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { Contacts } from '../Screens/Contacts';
import { Chat } from '../Screens/Chat';
import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation';
import { Platform } from 'react-native';
import { useState } from 'react';

const Tab = createNativeBottomTabNavigator();

function NativeBottomTabs() {
const [label, setLabel] = useState('Article');
return (
<Tab.Navigator
initialRouteName="Chat"
Expand Down Expand Up @@ -40,11 +42,13 @@ function NativeBottomTabs() {
console.log(
`${Platform.OS}: Long press detected on tab with key ${data.target} at the screen level.`
);
setLabel("CHANGED")
},
}}
options={{
tabBarButtonTestID: 'articleTestID',
tabBarBadge: '10',
tabBarLabel: label,
tabBarIcon: ({ focused }) =>
focused
? require('../../assets/icons/person_dark.png')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
LayoutParams.WRAP_CONTENT
))


post {
addOnLayoutChangeListener { _, left, top, right, bottom,
_, _, _, _ ->
Expand Down Expand Up @@ -197,16 +198,19 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
}

private fun onTabSelected(item: MenuItem) {
val selectedItem = items.first { it.title == item.title }
val selectedItem = items[item.itemId]
println(selectedItem.title)
println(item.title)
selectedItem.let {
onTabSelectedListener?.invoke(selectedItem.key)
emitHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
}

}

private fun onTabLongPressed(item: MenuItem) {
val longPressedItem = items.firstOrNull { it.title == item.title }
longPressedItem?.let {
val longPressedItem = items[item.itemId]
longPressedItem.let {
onTabLongPressedListener?.invoke(longPressedItem.key)
emitHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
}
Expand All @@ -220,6 +224,10 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
this.items = items
items.forEachIndexed { index, item ->
val menuItem = getOrCreateItem(index, item.title)
if (item.title !== menuItem.title) {
menuItem.title = item.title
}

menuItem.isVisible = !item.hidden
if (iconSources.containsKey(index)) {
getDrawable(iconSources[index]!!) {
Expand Down Expand Up @@ -370,7 +378,7 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {

private fun updateTextAppearance() {
if (fontSize != null || fontFamily != null || fontWeight != null) {
val menuView = getChildAt(0) as? ViewGroup ?: return
val menuView = bottomNavigation.getChildAt(0) as? ViewGroup ?: return
val size = fontSize?.toFloat()?.takeIf { it > 0 } ?: 12f
val typeface = ReactFontManager.getInstance().getTypeface(
fontFamily ?: "",
Expand Down Expand Up @@ -403,7 +411,9 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {

private fun updateTintColors(item: MenuItem? = null) {
// First let's check current item color.
val currentItemTintColor = items.find { it.title == item?.title }?.activeTintColor
val currentItemTintColor = item?.itemId?.let { itemId ->
items[itemId].activeTintColor
}

// getDefaultColor will always return a valid color but to satisfy the compiler we need to check for null
val colorPrimary = currentItemTintColor ?: activeTintColor ?: Utils.getDefaultColorFor(
Expand Down

0 comments on commit c2439ee

Please sign in to comment.