DrawerView.js
8.6 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _reactNativeDrawerLayoutPolyfill = require('react-native-drawer-layout-polyfill');
var _reactNativeDrawerLayoutPolyfill2 = _interopRequireDefault(_reactNativeDrawerLayoutPolyfill);
var _addNavigationHelpers = require('../../addNavigationHelpers');
var _addNavigationHelpers2 = _interopRequireDefault(_addNavigationHelpers);
var _DrawerSidebar = require('./DrawerSidebar');
var _DrawerSidebar2 = _interopRequireDefault(_DrawerSidebar);
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 drawer.
*/
var DrawerView = function (_React$PureComponent) {
_inherits(DrawerView, _React$PureComponent);
function DrawerView() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, DrawerView);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DrawerView.__proto__ || Object.getPrototypeOf(DrawerView)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
drawerWidth: typeof _this.props.drawerWidth === 'function' ? _this.props.drawerWidth() : _this.props.drawerWidth
}, _this._handleDrawerOpen = function () {
var _this$props = _this.props,
navigation = _this$props.navigation,
drawerOpenRoute = _this$props.drawerOpenRoute;
var _navigation$state = navigation.state,
routes = _navigation$state.routes,
index = _navigation$state.index;
if (routes[index].routeName !== drawerOpenRoute) {
_this.props.navigation.navigate(drawerOpenRoute);
}
}, _this._handleDrawerClose = function () {
var _this$props2 = _this.props,
navigation = _this$props2.navigation,
drawerCloseRoute = _this$props2.drawerCloseRoute;
var _navigation$state2 = navigation.state,
routes = _navigation$state2.routes,
index = _navigation$state2.index;
if (routes[index].routeName !== drawerCloseRoute) {
_this.props.navigation.navigate(drawerCloseRoute);
}
}, _this._updateScreenNavigation = function (navigation) {
var drawerCloseRoute = _this.props.drawerCloseRoute;
// $FlowFixMe there's no way type the specific shape of the nav state
var navigationState = navigation.state.routes.find(function (route) {
return route.routeName === drawerCloseRoute;
});
if (_this._screenNavigationProp && _this._screenNavigationProp.state === navigationState) {
return;
}
_this._screenNavigationProp = (0, _addNavigationHelpers2.default)({
dispatch: navigation.dispatch,
state: navigationState
});
}, _this._updateWidth = function () {
var drawerWidth = typeof _this.props.drawerWidth === 'function' ? _this.props.drawerWidth() : _this.props.drawerWidth;
if (_this.state.drawerWidth !== drawerWidth) {
_this.setState({ drawerWidth: drawerWidth });
}
}, _this._getNavigationState = function (navigation) {
var drawerCloseRoute = _this.props.drawerCloseRoute;
var navigationState = navigation.state.routes.find(function (route) {
return route.routeName === drawerCloseRoute;
});
return navigationState;
}, _this._renderNavigationView = function () {
return React.createElement(_DrawerSidebar2.default, {
screenProps: _this.props.screenProps,
navigation: _this._screenNavigationProp,
router: _this.props.router,
contentComponent: _this.props.contentComponent,
contentOptions: _this.props.contentOptions,
drawerPosition: _this.props.drawerPosition,
style: _this.props.style
});
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(DrawerView, [{
key: 'componentWillMount',
value: function componentWillMount() {
this._updateScreenNavigation(this.props.navigation);
_reactNative.Dimensions.addEventListener('change', this._updateWidth);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
_reactNative.Dimensions.removeEventListener('change', this._updateWidth);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.navigation.state.index !== nextProps.navigation.state.index) {
var _props = this.props,
_drawerOpenRoute = _props.drawerOpenRoute,
_drawerCloseRoute = _props.drawerCloseRoute,
_drawerToggleRoute = _props.drawerToggleRoute;
var _nextProps$navigation = nextProps.navigation.state,
routes = _nextProps$navigation.routes,
_index = _nextProps$navigation.index;
if (routes[_index].routeName === _drawerOpenRoute) {
this._drawer.openDrawer();
} else if (routes[_index].routeName === _drawerToggleRoute) {
if (this._drawer.state.drawerShown) {
this.props.navigation.navigate(_drawerCloseRoute);
} else {
this.props.navigation.navigate(_drawerOpenRoute);
}
} else {
this._drawer.closeDrawer();
}
}
this._updateScreenNavigation(nextProps.navigation);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var DrawerScreen = this.props.router.getComponentForRouteName(this.props.drawerCloseRoute);
var screenNavigation = (0, _addNavigationHelpers2.default)({
state: this._screenNavigationProp.state,
dispatch: this._screenNavigationProp.dispatch
});
var config = this.props.router.getScreenOptions(screenNavigation, this.props.screenProps);
return React.createElement(
_reactNativeDrawerLayoutPolyfill2.default,
{
ref: function ref(c) {
_this2._drawer = c;
},
drawerLockMode: this.props.screenProps && this.props.screenProps.drawerLockMode || config && config.drawerLockMode,
drawerBackgroundColor: this.props.drawerBackgroundColor,
drawerWidth: this.state.drawerWidth,
onDrawerOpen: this._handleDrawerOpen,
onDrawerClose: this._handleDrawerClose,
useNativeAnimations: this.props.useNativeAnimations,
renderNavigationView: this._renderNavigationView,
drawerPosition: this.props.drawerPosition === 'right' ? _reactNativeDrawerLayoutPolyfill2.default.positions.Right : _reactNativeDrawerLayoutPolyfill2.default.positions.Left
},
React.createElement(DrawerScreen, {
screenProps: this.props.screenProps,
navigation: this._screenNavigationProp
})
);
}
}]);
return DrawerView;
}(React.PureComponent);
exports.default = DrawerView;