-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.jsx
More file actions
132 lines (117 loc) · 3.11 KB
/
index.jsx
File metadata and controls
132 lines (117 loc) · 3.11 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
import React, {
createRef,
useState,
useMemo,
useRef,
useLayoutEffect,
} from "react";
import { StyleSheet, View } from "react-native";
import PropTypes from "prop-types";
import { moderateScale } from "react-native-size-matters";
import { theme } from "@theme";
import { Indicator } from "./Indicator";
import { Tab } from "./Tab";
/**
*
* A Segment Picker component that provides toggling between two or more views.
*
* <div class="screenshots">
* <img src="screenshots/toptabbar/toptabbar-1.png" />
* <img src="screenshots/toptabbar/toptabbar-2.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { createMaterialTopTabNavigator } from "@react-navigation/material-top-tabs";
* import { NavigationContainer } from "@react-navigation/native";
*
* import { SegmentedTopBar } from '@bigbinary/neetoui-rn';
*
* const Tab = createMaterialTopTabNavigator();
*
* export default function Main(){
*
* return (
* <NavigationContainer>
* <Tab.Navigator tabBar={props => <SegmentedTopBar {...props} />} >
* <Tab.Screen
* name="Home"
* component={HomeScreen}
* options={{ count: 8 }}
* />
*
* <Tab.Screen
* name="Settings"
* component={SettingsScreen}
* options={{ count: 10 }}
* />
* </Tab.Navigator>
* </NavigationContainer>
* );
* }
* ```
*/
export const SegmentedTopBar = ({
state: { routes, index },
navigation,
height,
descriptors,
}) => {
const containerRef = useRef();
const [measures, setMeasures] = useState([]);
const tabsData = useMemo(
() =>
routes.map(({ key, name }) => {
const { options } = descriptors[key];
const label = options.tabBarLabel ?? options.title ?? name;
const count = options.count;
return { label, value: name, ref: createRef(), count };
}),
[descriptors, routes]
);
useLayoutEffect(() => {
const _measures = [];
tabsData.forEach(({ ref }) => {
ref.current.measureLayout(containerRef.current, (x, y, width, height) => {
_measures.push({ x, y, width, height });
if (_measures.length === tabsData.length) {
setMeasures(_measures);
}
});
});
}, [measures.length]);
return (
<View height={height} ref={containerRef} style={styles.container}>
{measures.length > 0 && <Indicator measure={measures[index]} />}
{tabsData.map(({ label, value, ref, count }) => (
<Tab
count={count}
flex={tabsData.length > 3 ? label.length : 1}
key={value}
label={label}
navigation={navigation}
ref={ref}
value={value}
/>
))}
</View>
);
};
const styles = StyleSheet.create({
container: {
flexDirection: "row",
borderRadius: moderateScale(6),
backgroundColor: theme.colors.background.secondary,
},
});
SegmentedTopBar.propTypes = {
state: PropTypes.object,
height: PropTypes.number,
navigation: PropTypes.object,
descriptors: PropTypes.object,
};
SegmentedTopBar.defaultProps = {
tabs: [],
height: moderateScale(48),
};