-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass isGliding ref instead of current value and update types
BREAKING CHANGE: pass isGliding ref instead of isGliding.current Migration: use isGliding.current instead of isGliding inside your custom renderTabBar function Fixes #33
- Loading branch information
Showing
5 changed files
with
161 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import * as React from 'react'; | ||
import { StyleSheet, View, Text, Animated } from 'react-native'; | ||
import { | ||
CollapsibleTabView, | ||
RenderTabBarProps, | ||
useCollapsibleScene, | ||
} from 'react-native-collapsible-tab-view'; | ||
import { SceneMap, TabBarProps, TabBar } from 'react-native-tab-view'; | ||
import { ExampleComponentType } from './types'; | ||
|
||
type Route = { | ||
key: string; | ||
title: string; | ||
icon?: string; | ||
}; | ||
|
||
const SomeRoute: React.FC<{ routeKey: string; color: string }> = ({ | ||
routeKey, | ||
color, | ||
}) => { | ||
const scrollPropsAndRef = useCollapsibleScene(routeKey); | ||
|
||
return ( | ||
<Animated.ScrollView | ||
style={{ backgroundColor: color }} | ||
{...scrollPropsAndRef} | ||
> | ||
<View style={styles.content} /> | ||
</Animated.ScrollView> | ||
); | ||
}; | ||
|
||
const FirstScene = () => <SomeRoute routeKey="first" color="white" />; | ||
const SecondScene = () => <SomeRoute routeKey="second" color="black" />; | ||
|
||
const HEADER_HEIGHT = 250; | ||
|
||
const renderHeader = () => ( | ||
<View style={styles.header}> | ||
<Text style={styles.headerText}>COLLAPSIBLE</Text> | ||
</View> | ||
); | ||
|
||
const renderScene = SceneMap({ | ||
first: FirstScene, | ||
second: SecondScene, | ||
}); | ||
|
||
const renderTabBar = ( | ||
props: RenderTabBarProps<Route, TabBarProps<Route>> | ||
): React.ReactNode => ( | ||
<TabBar | ||
{...props} | ||
activeColor="pink" | ||
inactiveColor="red" | ||
onTabPress={(event) => { | ||
if (props?.isGliding?.current && props?.preventTabPressOnGliding) { | ||
event.preventDefault(); | ||
} | ||
props?.onTabPress && props.onTabPress(event); | ||
}} | ||
renderLabel={({ color, focused, route }) => ( | ||
<View style={styles.label}> | ||
<Text | ||
style={[ | ||
styles.title, | ||
{ color }, | ||
{ fontWeight: focused ? 'bold' : 'normal' }, | ||
]} | ||
> | ||
{route.icon ?? route.title} | ||
</Text> | ||
</View> | ||
)} | ||
/> | ||
); | ||
|
||
const CollapsibleTabViewExample: ExampleComponentType = () => { | ||
const [index, setIndex] = React.useState(0); | ||
const [routes] = React.useState<Route[]>([ | ||
{ key: 'first', title: 'First', icon: '👋' }, // or FontAwesome/badge | ||
{ key: 'second', title: 'Second', icon: '🤯' }, | ||
]); | ||
|
||
const handleIndexChange = (index: number) => { | ||
setIndex(index); | ||
}; | ||
|
||
return ( | ||
<CollapsibleTabView<Route, TabBarProps<Route>> | ||
navigationState={{ index, routes }} | ||
renderScene={renderScene} | ||
onIndexChange={handleIndexChange} | ||
renderHeader={renderHeader} // optional | ||
renderTabBar={renderTabBar} // optional | ||
headerHeight={HEADER_HEIGHT} // optional, will be computed. | ||
/> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
header: { | ||
height: HEADER_HEIGHT, | ||
backgroundColor: '#2196f3', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
elevation: 4, | ||
}, | ||
headerText: { | ||
color: 'white', | ||
fontSize: 24, | ||
}, | ||
content: { | ||
height: 1500, | ||
}, | ||
label: { | ||
alignContent: 'center', | ||
flex: 1, | ||
justifyContent: 'center', | ||
}, | ||
title: {}, | ||
}); | ||
|
||
CollapsibleTabViewExample.title = 'Custom Tab Bar demo'; | ||
CollapsibleTabViewExample.backgroundColor = '#2196f3'; | ||
CollapsibleTabViewExample.appbarElevation = 0; | ||
|
||
export default CollapsibleTabViewExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters