forked from satya164/react-native-tab-view
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabView.tsx
More file actions
149 lines (140 loc) · 4.14 KB
/
TabView.tsx
File metadata and controls
149 lines (140 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as React from 'react';
import {
StyleSheet,
View,
StyleProp,
ViewStyle,
LayoutChangeEvent,
} from 'react-native';
import TabBar from './TabBar';
import SceneView from './SceneView';
import Pager from './Pager';
import type {
Layout,
NavigationState,
Route,
SceneRendererProps,
PagerProps,
} from './types';
export type Props<T extends Route> = PagerProps & {
onIndexChange: (index: number) => void;
navigationState: NavigationState<T>;
renderScene: (props: SceneRendererProps & { route: T }) => React.ReactNode;
renderLazyPlaceholder?: (props: { route: T }) => React.ReactNode;
renderTabBar?: (
props: SceneRendererProps & { navigationState: NavigationState<T> }
) => React.ReactNode;
tabBarPosition?: 'top' | 'bottom';
initialLayout?: Partial<Layout>;
lazy?: ((props: { route: T }) => boolean) | boolean;
lazyPreloadDistance?: number;
sceneContainerStyle?: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
};
export default function TabView<T extends Route>({
onIndexChange,
navigationState,
renderScene,
initialLayout,
keyboardDismissMode = 'auto',
lazy = false,
lazyPreloadDistance = 0,
onSwipeStart,
onSwipeEnd,
renderLazyPlaceholder = () => null,
renderTabBar = (props) => <TabBar {...props} />,
sceneContainerStyle,
style,
swipeEnabled = true,
tabBarPosition = 'top',
disableChangeTabAnimation,
}: Props<T>) {
const [layout, setLayout] = React.useState({
width: 0,
height: 0,
...initialLayout,
});
const jumpToIndex = (index: number) => {
if (index !== navigationState.index) {
onIndexChange(index);
}
};
const handleLayout = (e: LayoutChangeEvent) => {
const { height, width } = e.nativeEvent.layout;
setLayout((prevLayout) => {
if (prevLayout.width === width && prevLayout.height === height) {
return prevLayout;
}
return { height, width };
});
};
return (
<View onLayout={handleLayout} style={[styles.pager, style]}>
<Pager
layout={layout}
navigationState={navigationState}
keyboardDismissMode={keyboardDismissMode}
swipeEnabled={swipeEnabled}
onSwipeStart={onSwipeStart}
onSwipeEnd={onSwipeEnd}
onIndexChange={jumpToIndex}
disableChangeTabAnimation={disableChangeTabAnimation}
>
{({ position, render, addEnterListener, jumpTo }) => {
// All of the props here must not change between re-renders
// This is crucial to optimizing the routes with PureComponent
const sceneRendererProps = {
position,
layout,
jumpTo,
};
return (
<React.Fragment>
{tabBarPosition === 'top' &&
renderTabBar({
...sceneRendererProps,
navigationState,
})}
{render(
navigationState.routes.map((route, i) => {
return (
<SceneView
{...sceneRendererProps}
addEnterListener={addEnterListener}
key={route.key}
index={i}
lazy={typeof lazy === 'function' ? lazy({ route }) : lazy}
lazyPreloadDistance={lazyPreloadDistance}
navigationState={navigationState}
style={sceneContainerStyle}
>
{({ loading }) =>
loading
? renderLazyPlaceholder({ route })
: renderScene({
...sceneRendererProps,
route,
})
}
</SceneView>
);
})
)}
{tabBarPosition === 'bottom' &&
renderTabBar({
...sceneRendererProps,
navigationState,
})}
</React.Fragment>
);
}}
</Pager>
</View>
);
}
const styles = StyleSheet.create({
pager: {
flex: 1,
overflow: 'hidden',
},
});