|
| 1 | +import * as React from 'react'; |
| 2 | +import { StyleSheet } from 'react-native'; |
| 3 | +import { |
| 4 | + TabView, |
| 5 | + TabBar, |
| 6 | + SceneMap, |
| 7 | + NavigationState, |
| 8 | + SceneRendererProps, |
| 9 | +} from 'react-native-tab-view'; |
| 10 | +import Article from './Shared/Article'; |
| 11 | +import Albums from './Shared/Albums'; |
| 12 | +import Chat from './Shared/Chat'; |
| 13 | +import Contacts from './Shared/Contacts'; |
| 14 | + |
| 15 | +type State = NavigationState<{ |
| 16 | + key: string; |
| 17 | + title: string; |
| 18 | +}>; |
| 19 | + |
| 20 | +const animateOnIndexChange = (currentIndex: number, nextIndex: number) => { |
| 21 | + return Math.abs(currentIndex - nextIndex) === 1; |
| 22 | +}; |
| 23 | + |
| 24 | +const CustomAnimationExample = () => { |
| 25 | + const [index, onIndexChange] = React.useState(1); |
| 26 | + const [routes] = React.useState([ |
| 27 | + { key: 'article', title: 'Article' }, |
| 28 | + { key: 'contacts', title: 'Contacts' }, |
| 29 | + { key: 'albums', title: 'Albums' }, |
| 30 | + { key: 'chat', title: 'Chat' }, |
| 31 | + ]); |
| 32 | + |
| 33 | + const renderTabBar = ( |
| 34 | + props: SceneRendererProps & { navigationState: State } |
| 35 | + ) => ( |
| 36 | + <TabBar |
| 37 | + {...props} |
| 38 | + scrollEnabled |
| 39 | + indicatorStyle={styles.indicator} |
| 40 | + style={styles.tabbar} |
| 41 | + tabStyle={styles.tab} |
| 42 | + labelStyle={styles.label} |
| 43 | + /> |
| 44 | + ); |
| 45 | + |
| 46 | + const renderScene = SceneMap({ |
| 47 | + albums: Albums, |
| 48 | + contacts: Contacts, |
| 49 | + article: Article, |
| 50 | + chat: Chat, |
| 51 | + }); |
| 52 | + |
| 53 | + return ( |
| 54 | + <TabView |
| 55 | + lazy |
| 56 | + navigationState={{ |
| 57 | + index, |
| 58 | + routes, |
| 59 | + }} |
| 60 | + renderScene={renderScene} |
| 61 | + renderTabBar={renderTabBar} |
| 62 | + onIndexChange={onIndexChange} |
| 63 | + animateOnIndexChange={animateOnIndexChange} |
| 64 | + /> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +CustomAnimationExample.title = 'Custom Animation'; |
| 69 | +CustomAnimationExample.backgroundColor = '#3f51b5'; |
| 70 | +CustomAnimationExample.appbarElevation = 0; |
| 71 | + |
| 72 | +export default CustomAnimationExample; |
| 73 | + |
| 74 | +const styles = StyleSheet.create({ |
| 75 | + tabbar: { |
| 76 | + backgroundColor: '#3f51b5', |
| 77 | + }, |
| 78 | + tab: { |
| 79 | + width: 'auto', |
| 80 | + }, |
| 81 | + indicator: { |
| 82 | + backgroundColor: '#ffeb3b', |
| 83 | + }, |
| 84 | + label: { |
| 85 | + fontWeight: '400', |
| 86 | + }, |
| 87 | +}); |
0 commit comments