DrawerSidebar.js
6.87 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
154
155
156
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _reactNative = require('react-native');
var _withCachedChildNavigation = require('../../withCachedChildNavigation');
var _withCachedChildNavigation2 = _interopRequireDefault(_withCachedChildNavigation);
var _NavigationActions = require('../../NavigationActions');
var _NavigationActions2 = _interopRequireDefault(_NavigationActions);
var _invariant = require('../../utils/invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _SafeAreaView = require('../SafeAreaView');
var _SafeAreaView2 = _interopRequireDefault(_SafeAreaView);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Component that renders the sidebar screen of the drawer.
*/
var DrawerSidebar = function (_React$PureComponent) {
_inherits(DrawerSidebar, _React$PureComponent);
function DrawerSidebar() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, DrawerSidebar);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DrawerSidebar.__proto__ || Object.getPrototypeOf(DrawerSidebar)).call.apply(_ref, [this].concat(args))), _this), _this._getScreenOptions = function (routeKey) {
var DrawerScreen = _this.props.router.getComponentForRouteName('DrawerClose');
(0, _invariant2.default)(DrawerScreen.router, 'NavigationComponent with routeName DrawerClose should be a Navigator');
var childNavigation = _this.props.childNavigationProps[routeKey];
return DrawerScreen.router.getScreenOptions(childNavigation.state.index !== undefined // if the child screen is a StackRouter then always show the screen options of its first screen (see #1914)
? _extends({}, childNavigation, {
state: _extends({}, childNavigation.state, { index: 0 })
}) : childNavigation, _this.props.screenProps);
}, _this._getLabel = function (_ref2) {
var focused = _ref2.focused,
tintColor = _ref2.tintColor,
route = _ref2.route;
var _this$_getScreenOptio = _this._getScreenOptions(route.key),
drawerLabel = _this$_getScreenOptio.drawerLabel,
title = _this$_getScreenOptio.title;
if (drawerLabel) {
return typeof drawerLabel === 'function' ? drawerLabel({ tintColor: tintColor, focused: focused }) : drawerLabel;
}
if (typeof title === 'string') {
return title;
}
return route.routeName;
}, _this._renderIcon = function (_ref3) {
var focused = _ref3.focused,
tintColor = _ref3.tintColor,
route = _ref3.route;
var _this$_getScreenOptio2 = _this._getScreenOptions(route.key),
drawerIcon = _this$_getScreenOptio2.drawerIcon;
if (drawerIcon) {
return typeof drawerIcon === 'function' ? drawerIcon({ tintColor: tintColor, focused: focused }) : drawerIcon;
}
return null;
}, _this._onItemPress = function (_ref4) {
var route = _ref4.route,
focused = _ref4.focused;
_this.props.navigation.navigate('DrawerClose');
if (!focused) {
var subAction = void 0;
// if the child screen is a StackRouter then always navigate to its first screen (see #1914)
if (route.index !== undefined && route.index !== 0) {
route = route;
subAction = _NavigationActions2.default.navigate({
routeName: route.routes[0].routeName
});
}
_this.props.navigation.navigate(route.routeName, undefined, subAction);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(DrawerSidebar, [{
key: 'render',
value: function render() {
var ContentComponent = this.props.contentComponent;
if (!ContentComponent) {
return null;
}
var state = this.props.navigation.state;
(0, _invariant2.default)(typeof state.index === 'number', 'should be set');
return React.createElement(
_reactNative.View,
{ style: [styles.container, this.props.style] },
React.createElement(ContentComponent, _extends({}, this.props.contentOptions, {
navigation: this.props.navigation,
items: state.routes,
activeItemKey: state.routes[state.index] ? state.routes[state.index].key : null,
screenProps: this.props.screenProps,
getLabel: this._getLabel,
renderIcon: this._renderIcon,
onItemPress: this._onItemPress,
router: this.props.router,
drawerPosition: this.props.drawerPosition
}))
);
}
}]);
return DrawerSidebar;
}(React.PureComponent);
exports.default = (0, _withCachedChildNavigation2.default)(DrawerSidebar);
var styles = _reactNative.StyleSheet.create({
container: {
flex: 1
}
});