-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.js
91 lines (74 loc) · 3 KB
/
index.js
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
import {createNavigatorFactory, useNavigationBuilder} from '@react-navigation/native';
import {StackView} from '@react-navigation/stack';
import PropTypes from 'prop-types';
import React, {useMemo, useRef} from 'react';
import useWindowDimensions from '@hooks/useWindowDimensions';
import NAVIGATORS from '@src/NAVIGATORS';
import CustomRouter from './CustomRouter';
const propTypes = {
/* Determines if the navigator should render the StackView (narrow) or ThreePaneView (wide) */
isSmallScreenWidth: PropTypes.bool.isRequired,
/* Children for the useNavigationBuilder hook */
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]).isRequired,
/* initialRouteName for this navigator */
initialRouteName: PropTypes.oneOf([PropTypes.string, PropTypes.undefined]),
/* Screen options defined for this navigator */
// eslint-disable-next-line react/forbid-prop-types
screenOptions: PropTypes.object,
};
const defaultProps = {
initialRouteName: undefined,
screenOptions: undefined,
};
function reduceReportRoutes(routes) {
const result = [];
let count = 0;
const reverseRoutes = [...routes].reverse();
reverseRoutes.forEach((route) => {
if (route.name === NAVIGATORS.CENTRAL_PANE_NAVIGATOR) {
// Remove all report routes except the last 3. This will improve performance.
if (count < 3) {
result.push(route);
count++;
}
} else {
result.push(route);
}
});
return result.reverse();
}
function ResponsiveStackNavigator(props) {
const {isSmallScreenWidth} = useWindowDimensions();
const isSmallScreenWidthRef = useRef(isSmallScreenWidth);
isSmallScreenWidthRef.current = isSmallScreenWidth;
const {navigation, state, descriptors, NavigationContent} = useNavigationBuilder(CustomRouter, {
children: props.children,
screenOptions: props.screenOptions,
initialRouteName: props.initialRouteName,
// Options for useNavigationBuilder won't update on prop change, so we need to pass a getter for the router to have the current state of isSmallScreenWidth.
getIsSmallScreenWidth: () => isSmallScreenWidthRef.current,
});
const stateToRender = useMemo(() => {
const result = reduceReportRoutes(state.routes);
return {
...state,
index: result.length - 1,
routes: [...result],
};
}, [state]);
return (
<NavigationContent>
<StackView
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
state={stateToRender}
descriptors={descriptors}
navigation={navigation}
/>
</NavigationContent>
);
}
ResponsiveStackNavigator.defaultProps = defaultProps;
ResponsiveStackNavigator.propTypes = propTypes;
ResponsiveStackNavigator.displayName = 'ResponsiveStackNavigator';
export default createNavigatorFactory(ResponsiveStackNavigator);