This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
generated from satya164/typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 41
/
MaterialTopTabView.tsx
81 lines (71 loc) · 2.13 KB
/
MaterialTopTabView.tsx
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
import * as React from 'react';
import { TabView, SceneRendererProps } from 'react-native-tab-view';
import { Route, useTheme } from '@react-navigation/native';
import { TabNavigationState, TabActions } from '@react-navigation/routers';
import MaterialTopTabBar from './MaterialTopTabBar';
import {
MaterialTopTabDescriptorMap,
MaterialTopTabNavigationConfig,
MaterialTopTabNavigationHelpers,
MaterialTopTabBarProps,
} from '../types';
type Props = MaterialTopTabNavigationConfig & {
state: TabNavigationState;
navigation: MaterialTopTabNavigationHelpers;
descriptors: MaterialTopTabDescriptorMap;
tabBarPosition?: 'top' | 'bottom';
};
export default function MaterialTopTabView({
lazyPlaceholder,
tabBar = (props: MaterialTopTabBarProps) => <MaterialTopTabBar {...props} />,
tabBarOptions,
state,
navigation,
descriptors,
sceneContainerStyle,
...rest
}: Props) {
const { colors } = useTheme();
const renderLazyPlaceholder = (props: { route: Route<string> }) => {
if (lazyPlaceholder != null) {
return lazyPlaceholder(props);
}
return null;
};
const renderTabBar = (props: SceneRendererProps) => {
const route = state.routes[state.index];
const options = descriptors[route.key].options;
const tabBarVisible = options.tabBarVisible !== false;
if (tabBarVisible === false) {
return null;
}
return tabBar({
...tabBarOptions,
...props,
state: state,
navigation: navigation,
descriptors: descriptors,
});
};
return (
<TabView
{...rest}
onIndexChange={index =>
navigation.dispatch({
...TabActions.jumpTo(state.routes[index].name),
target: state.key,
})
}
renderScene={({ route }) => descriptors[route.key].render()}
navigationState={state}
renderTabBar={renderTabBar}
renderLazyPlaceholder={renderLazyPlaceholder}
onSwipeStart={() => navigation.emit({ type: 'swipeStart' })}
onSwipeEnd={() => navigation.emit({ type: 'swipeEnd' })}
sceneContainerStyle={[
{ backgroundColor: colors.background },
sceneContainerStyle,
]}
/>
);
}