Skip to content

Commit 509792a

Browse files
committed
added carousel
1 parent 9c54785 commit 509792a

File tree

5 files changed

+50
-49
lines changed

5 files changed

+50
-49
lines changed

babel.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ module.exports = {
22
presets: [
33
['module:react-native-builder-bob/babel-preset', { modules: 'commonjs' }],
44
],
5-
plugins: ['react-native-reanimated/plugin'],
65
};

declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'react-native-reanimated';

example/src/App.tsx

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,84 @@
1-
import { useState } from 'react';
2-
import { StyleSheet, View, Text, Dimensions, SafeAreaView } from 'react-native';
1+
import { useCallback, useMemo, useRef } from 'react';
2+
import { StyleSheet, View, Text, Dimensions, FlatList } from 'react-native';
33
import { TabBar } from 'react-native-bottom-tab-bar';
44

5-
let { width } = Dimensions.get('window');
5+
const { width: screenWidth } = Dimensions.get('window');
66
interface TabDataType {
77
name: string;
88
id: number;
9+
bgColor: string;
910
}
1011

1112
const tabData: Array<TabDataType> = [
1213
{
1314
name: 'Home',
1415
id: 1,
16+
bgColor: '#FFC0C7',
1517
},
1618
{
1719
name: 'Cart',
1820
id: 2,
21+
bgColor: '#FF7128',
1922
},
2023
{
2124
name: 'Search',
2225
id: 3,
26+
bgColor: '#0088cc',
2327
},
2428
{
2529
name: 'Profile',
2630
id: 4,
31+
bgColor: '#ff6666',
2732
},
2833
// {
2934
// name: 'Setting',
3035
// id: 5,
36+
// bgColor: '#66ff99'
3137
// },
3238
];
3339

3440
export default function App() {
35-
const [bgColor, setBgColor] = useState('#FFC0C7');
36-
const [tabs, setTabs] = useState<Array<TabDataType> | []>(tabData);
41+
const tabs = useMemo(() => tabData, []);
42+
const flatListRef = useRef<FlatList>(null);
3743

38-
const onTabChange = (item: TabDataType) => {
39-
let tempTabs = [...tabs];
40-
setTimeout(() => {
41-
tempTabs.map((val) => {
42-
if (item.name === 'Home' && val.name === 'Home') {
43-
// val.activeIcon = Object.assign({}, activeHome(true))
44-
setBgColor('#FFC0C7');
45-
} else if (item.name === 'Cart' && val.name === 'Cart') {
46-
// val.activeIcon = Object.assign({}, activeList(true))
47-
setBgColor('#FF7128');
48-
} else if (item.name === 'Search' && val.name === 'Search') {
49-
// val.activeIcon = Object.assign({}, activeCamera(true))
50-
setBgColor('#0088cc');
51-
} else if (item.name === 'Setting' && val.name === 'Setting') {
52-
// val.activeIcon = Object.assign({}, activeNotification(true))
53-
setBgColor('#ff6666');
54-
} else if (item.name === 'Profile' && val.name === 'Profile') {
55-
// val.activeIcon = Object.assign({}, activeUser(true))
56-
setBgColor('#66ff99');
57-
} else {
58-
// val.activeIcon = null
59-
}
60-
});
61-
62-
setTabs(tempTabs);
63-
}, 500);
64-
};
44+
const onTabChange = useCallback((_item: TabDataType, index: number) => {
45+
flatListRef.current?.scrollToIndex({
46+
animated: true,
47+
index: index,
48+
});
49+
}, []);
6550

6651
return (
6752
<View style={styles.container}>
68-
<View
69-
style={{
70-
backgroundColor: bgColor,
71-
flex: 1,
72-
justifyContent: 'center',
73-
alignItems: 'center',
53+
<FlatList
54+
ref={flatListRef}
55+
data={tabs}
56+
keyExtractor={(item) => item.id.toString()}
57+
renderItem={({ item }: { item: TabDataType }) => {
58+
return (
59+
<View
60+
style={[
61+
styles.slide,
62+
{
63+
backgroundColor: item.bgColor,
64+
},
65+
]}
66+
>
67+
<Text>{item.name}</Text>
68+
</View>
69+
);
7470
}}
75-
>
76-
<Text>App component</Text>
77-
</View>
71+
scrollEnabled={false}
72+
horizontal
73+
bounces={false}
74+
pagingEnabled
75+
showsHorizontalScrollIndicator={false}
76+
/>
7877
<TabBar
7978
tabs={tabs as Array<TabDataType>}
8079
onTabChange={onTabChange}
8180
// defaultActiveTabIndex={1}
82-
containerWidth={width}
81+
containerWidth={screenWidth}
8382
tabBarBackground={'#fff'}
8483
defaultActiveTabIndex={0}
8584
// transitionSpeed={100}
@@ -91,13 +90,15 @@ export default function App() {
9190
const styles = StyleSheet.create({
9291
container: {
9392
flex: 1,
94-
// backgroundColor: '#ccc',
95-
// alignItems: 'center',
96-
// justifyContent: 'center',
9793
},
9894
box: {
9995
width: 60,
10096
height: 60,
10197
marginVertical: 20,
10298
},
99+
slide: {
100+
width: screenWidth,
101+
justifyContent: 'center',
102+
alignItems: 'center',
103+
},
103104
});

src/TabBar/TabBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface TabBarProps<T> {
3333
// containerBottomRightRadius?: number;
3434
activeTabBackground?: string;
3535
// labelStyle?: TextStyle;
36-
onTabChange?: (tab: T | undefined) => void;
36+
onTabChange: (tab: T, index: number) => void;
3737
defaultActiveTabIndex?: number;
3838
// transitionSpeed?: number;
3939
}
@@ -162,7 +162,7 @@ export const TabBar = <T,>(props: TabBarProps<T>) => {
162162
onTabPress={() => {
163163
handleTabPress(index + 1);
164164
if (val !== undefined) {
165-
onTabChange(val);
165+
onTabChange(val, index);
166166
}
167167
}}
168168
containerWidth={containerWidth}

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12685,7 +12685,7 @@ __metadata:
1268512685
peerDependencies:
1268612686
react: "*"
1268712687
react-native: "*"
12688-
react-native-reanimated: ^3.15.4
12688+
react-native-reanimated: ^3.10.4
1268912689
languageName: unknown
1269012690
linkType: soft
1269112691

0 commit comments

Comments
 (0)