forked from react-navigation/react-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageWithSidebar.js
More file actions
153 lines (146 loc) · 5.51 KB
/
Copy pathPageWithSidebar.js
File metadata and controls
153 lines (146 loc) · 5.51 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
150
151
152
153
import React, { Component } from 'react';
import Link from './Link';
import { NavigationActions, addNavigationHelpers } from 'react-navigation';
const LinkableLi = Link.Linkable(props => <li {...props} />);
function getConfig(router, state, action, configName) {
if (action) {
state = router.getStateForAction(action, state);
}
const Component = router.getComponentForState(state);
return Component.navigationOptions[configName];
}
class PageWithSidebar extends Component {
render() {
const { router, navigation } = this.props;
const { dispatch, state } = navigation;
const ActiveScreen = router.getComponentForState(state);
let prevAction = null;
if (state.routes[state.index].index > 0) {
const prev = state.routes[state.index].index - 1;
prevAction = NavigationActions.navigate({
routeName: state.routes[state.index].routes[prev].routeName,
});
}
if (!prevAction && state.index > 0) {
const prev = state.index - 1;
prevAction = NavigationActions.navigate({
routeName: state.routes[prev].routeName,
action: NavigationActions.navigate({
routeName:
state.routes[prev].routes[state.routes[prev].routes.length - 1]
.routeName,
}),
});
}
let nextAction = null;
if (
state.routes[state.index].index <
state.routes[state.index].routes.length - 1
) {
const next = state.routes[state.index].index + 1;
nextAction = NavigationActions.navigate({
routeName: state.routes[state.index].routes[next].routeName,
});
}
if (!nextAction && state.index < state.routes.length - 1) {
const next = state.index + 1;
nextAction = NavigationActions.navigate({
routeName: state.routes[next].routeName,
});
}
let prevName =
prevAction && getConfig(router, state, prevAction, 'linkName');
let nextName =
nextAction && getConfig(router, state, nextAction, 'linkName');
const docPath = getConfig(router, state, null, 'doc') + '.md';
return (
<div className="page-body">
<div className="inner-page-body">
<div className="left-sidebar">
<ul className="pt-menu pt-elevation-1 navmenu">
{state.routes &&
state.routes.map((route, i) => {
const DocComponent = router.getComponentForRouteName(
route.routeName
);
const childNavigation = addNavigationHelpers({
state: route,
dispatch,
});
const options = router.getScreenOptions(childNavigation, {});
const isActive = state.index === i;
return (
<span key={i}>
<LinkableLi
to={route.routeName}
className={
'pt-menu-header ' +
options.icon +
' ' +
(isActive ? 'active' : '')
}
>
<h6>{options.linkName}</h6>
</LinkableLi>
<div>
{route.routes &&
route.routes.map((childRoute, childI) => {
const isChildActive =
isActive && route.index === childI;
const secondChildNavigation = addNavigationHelpers({
state: childRoute,
dispatch,
});
const secondOptions =
DocComponent.router &&
DocComponent.router.getScreenOptions(
secondChildNavigation,
{}
);
const routerLinkName =
secondOptions && secondOptions.linkName;
const linkName =
routerLinkName || childRoute.routeName;
return (
<Link
to={childRoute.routeName}
className={`pt-menu-item page ${isChildActive
? 'active'
: ''}`}
key={childI}
>
{linkName}
</Link>
);
})}
</div>
</span>
);
})}
</ul>
</div>
<div className="main-section">
<ActiveScreen navigation={this.props.navigation} />
<hr />
{state.routeName === 'Docs' && (
<Link
href={`https://github.com/react-community/react-navigation/tree/master/docs/${docPath}`}
className="editLink"
>
{' '}
Edit on GitHub
</Link>
)}
{prevAction && <Link to={prevAction}>Previous: {prevName}</Link>}
{nextAction && (
<Link to={nextAction} className="nextLink">
Next: {nextName}
</Link>
)}
</div>
</div>
</div>
);
}
}
export default PageWithSidebar;