Skip to content

Commit 436d430

Browse files
committed
feat(bottom-tabs,iOS): add support for badgeValue (#7)
## Description This PR adds support for setting the `badgeValue` of a tab bar item. ## Changes - **Remove redundant import** - **Add support for badgeValue** - **Update example** ## Checklist - [ ] Included code example that can be used to test this change - [ ] Ensured that CI passes
1 parent 1870f06 commit 436d430

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

apps/src/tests/TestBottomTabs.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { PropsWithChildren } from 'react';
1+
import React from 'react';
22
import { Button, Text, View, ViewProps } from 'react-native';
33

44
import { BottomTabs, BottomTabsScreen, enableFreeze } from 'react-native-screens';
@@ -32,21 +32,21 @@ function App() {
3232
return (
3333
<View style={{ flex: 1 }}>
3434
<BottomTabs tabBarBackgroundColor={'rgba(255, 255, 0, 0.5)'} tabBarBlurEffect={'dark'}>
35-
<BottomTabsScreen isFocused={focusedTab % 3 === 0}>
35+
<BottomTabsScreen isFocused={focusedTab % 3 === 0} badgeValue="Tab 1">
3636
<LayoutView style={{ backgroundColor: 'lightgreen' }} tabID={0}>
3737
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
3838
<Text>Hello world from native bottom tab</Text>
3939
<Button title="Next tab" onPress={selectNextTab} />
4040
</View>
4141
</LayoutView>
4242
</BottomTabsScreen>
43-
<BottomTabsScreen isFocused={focusedTab % 3 === 1}>
43+
<BottomTabsScreen isFocused={focusedTab % 3 === 1} badgeValue="Tab 2">
4444
<LayoutView style={{ backgroundColor: 'lightblue' }} tabID={1}>
4545
<Text>Tab2 world from native bottom tab</Text>
4646
<Button title="Next tab" onPress={selectNextTab} />
4747
</LayoutView>
4848
</BottomTabsScreen>
49-
<BottomTabsScreen isFocused={focusedTab % 3 === 2}>
49+
<BottomTabsScreen isFocused={focusedTab % 3 === 2} badgeValue="Tab 3">
5050
<LayoutView style={{ backgroundColor: 'yellow' }} tabID={2}>
5151
<Text>Tab3 world from native bottom tab</Text>
5252
<Button title="Next tab" onPress={selectNextTab} />

ios/bottom-tabs/RNSBottomTabsScreenComponentView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
3232
@interface RNSBottomTabsScreenComponentView ()
3333

3434
@property (nonatomic, readonly) BOOL isFocused;
35+
@property (nonatomic, readonly, nullable) NSString *badgeValue;
3536

3637
@end
3738

ios/bottom-tabs/RNSBottomTabsScreenComponentView.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ - (void)initState
3131
_controller = [RNSTabsScreenViewController new];
3232
_controller.view = self;
3333
_reactSuperview = nil;
34+
[self resetProps];
35+
}
36+
37+
- (void)resetProps
38+
{
3439
_isFocused = NO;
40+
_badgeValue = nil;
3541
}
3642

3743
- (nullable RNSBottomTabsHostComponentView *)reactSuperview
@@ -100,6 +106,11 @@ - (void)updateProps:(const facebook::react::Props::Shared &)props
100106
[_controller tabScreenFocusHasChanged];
101107
}
102108

109+
if (newComponentProps.badgeValue != oldComponentProps.badgeValue) {
110+
_badgeValue = RCTNSStringFromStringNilIfEmpty(newComponentProps.badgeValue);
111+
_controller.tabBarItem.badgeValue = _badgeValue;
112+
}
113+
103114
[super updateProps:props oldProps:oldProps];
104115
}
105116

src/components/BottomTabsScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { freezeEnabled } from '../core';
99
export interface BottomTabsScreenProps {
1010
children: ViewProps['children'];
1111
isFocused?: boolean;
12+
badgeValue?: string;
1213
}
1314

1415
// const LIFECYCLE_STATE_INITIAL = 0;

src/fabric/BottomTabScreenNativeComponent.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ export type LifecycleStateChangeEvent = Readonly<{
1616
}>;
1717

1818
export interface NativeProps extends ViewProps {
19-
isFocused?: boolean;
19+
// Events
2020
onLifecycleStateChange?: DirectEventHandler<LifecycleStateChangeEvent>;
2121
onWillAppear?: DirectEventHandler<GenericEmptyEvent>;
2222
onDidAppear?: DirectEventHandler<GenericEmptyEvent>;
2323
onWillDisappear?: DirectEventHandler<GenericEmptyEvent>;
2424
onDidDisappear?: DirectEventHandler<GenericEmptyEvent>;
25+
26+
// Control
27+
isFocused?: boolean;
28+
29+
// Appearance
30+
badgeValue?: string;
2531
}
2632

2733
export default codegenNativeComponent<NativeProps>('RNSBottomTabsScreen', {});

0 commit comments

Comments
 (0)