Skip to content

Commit 11197d3

Browse files
Enhance SearchBar and SegmentedTopBar components with conditional rendering and count display
- Updated SearchBar to show the cancel button only when there is text input. - Modified SegmentedTopBar to accept and display a count for each tab. - Adjusted Tab component to render the count alongside the label, improving user feedback. - Increased default height of SegmentedTopBar for better UI consistency.
1 parent 8126eb6 commit 11197d3

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

Diff for: src/components/SearchBar.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export const SearchBar = ({
137137
}}
138138
{...searchbarProps}
139139
/>
140-
{showCancelButton && (
140+
{showCancelButton && searchText.length > 0 && (
141141
<Container>
142142
<Touchable
143143
flexGrow={1}

Diff for: src/components/SegmentedTopBar/Tab.jsx

+41-19
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,51 @@ import React, { forwardRef, useCallback } from "react";
22
import { View } from "react-native";
33

44
import PropTypes from "prop-types";
5+
import { moderateScale } from "react-native-size-matters";
56

67
import { Typography, Touchable } from "@components";
78

8-
export const Tab = forwardRef(({ label, value, navigation, flex }, ref) => {
9-
const handleOnPress = useCallback(
10-
() => navigation.navigate(value),
11-
[navigation, value]
12-
);
9+
export const Tab = forwardRef(
10+
({ label, value, navigation, flex, count }, ref) => {
11+
const handleOnPress = useCallback(
12+
() => navigation.navigate(value),
13+
[navigation, value]
14+
);
1315

14-
return (
15-
<View ref={ref} style={{ flex }}>
16-
<Touchable flex={1} justifyContent="center" onPress={handleOnPress}>
17-
<Typography
18-
fontFamily="sf500"
19-
fontSize="xs"
20-
numberOfLines={1}
21-
textAlign="center"
16+
return (
17+
<View
18+
ref={ref}
19+
style={{ flex, minWidth: count !== undefined && moderateScale(56) }}
20+
>
21+
<Touchable
22+
alignItems="center"
23+
flex={1}
24+
justifyContent="center"
25+
onPress={handleOnPress}
2226
>
23-
{label}
24-
</Typography>
25-
</Touchable>
26-
</View>
27-
);
28-
});
27+
<Typography
28+
fontFamily="sf600"
29+
fontSize="xs"
30+
numberOfLines={1}
31+
textAlign="center"
32+
>
33+
{label}
34+
</Typography>
35+
{count !== undefined && (
36+
<Typography
37+
fontFamily="sf500"
38+
fontSize="xs"
39+
numberOfLines={1}
40+
textAlign="center"
41+
>
42+
({count})
43+
</Typography>
44+
)}
45+
</Touchable>
46+
</View>
47+
);
48+
}
49+
);
2950

3051
Tab.displayName = "Tab";
3152

@@ -34,4 +55,5 @@ Tab.propTypes = {
3455
label: PropTypes.string.isRequired,
3556
value: PropTypes.string.isRequired,
3657
navigation: PropTypes.object,
58+
count: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
3759
};

Diff for: src/components/SegmentedTopBar/index.jsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ import { Tab } from "./Tab";
4242
* <Tab.Screen
4343
* name="Home"
4444
* component={HomeScreen}
45+
* options={{ count: 8 }}
4546
* />
4647
*
4748
* <Tab.Screen
4849
* name="Settings"
4950
* component={SettingsScreen}
51+
* options={{ count: 10 }}
5052
* />
5153
* </Tab.Navigator>
5254
* </NavigationContainer>
@@ -70,10 +72,11 @@ export const SegmentedTopBar = ({
7072
routes.map(({ key, name }) => {
7173
const { options } = descriptors[key];
7274
const label = options.tabBarLabel ?? options.title ?? name;
75+
const count = options.count;
7376

74-
return { label, value: name, ref: createRef() };
77+
return { label, value: name, ref: createRef(), count };
7578
}),
76-
[]
79+
[descriptors, routes]
7780
);
7881

7982
useLayoutEffect(() => {
@@ -93,8 +96,9 @@ export const SegmentedTopBar = ({
9396
return (
9497
<View height={height} ref={containerRef} style={styles.container}>
9598
{measures.length > 0 && <Indicator measure={measures[index]} />}
96-
{tabsData.map(({ label, value, ref }) => (
99+
{tabsData.map(({ label, value, ref, count }) => (
97100
<Tab
101+
count={count}
98102
flex={tabsData.length > 3 ? label.length : 1}
99103
key={value}
100104
label={label}
@@ -124,5 +128,5 @@ SegmentedTopBar.propTypes = {
124128

125129
SegmentedTopBar.defaultProps = {
126130
tabs: [],
127-
height: moderateScale(36),
131+
height: moderateScale(48),
128132
};

0 commit comments

Comments
 (0)