UNPKG

8.06 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = createTabNavigator;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _reactNavigation = require("react-navigation");
11
12function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
13
14function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
16function _extends() { _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; }; return _extends.apply(this, arguments); }
17
18function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
20function createTabNavigator(TabView) {
21 class NavigationView extends React.Component {
22 constructor(...args) {
23 super(...args);
24
25 _defineProperty(this, "_renderScene", ({
26 route
27 }) => {
28 const {
29 screenProps,
30 descriptors
31 } = this.props;
32 const descriptor = descriptors[route.key];
33 const TabComponent = descriptor.getComponent();
34 return /*#__PURE__*/React.createElement(_reactNavigation.SceneView, {
35 screenProps: screenProps,
36 navigation: descriptor.navigation,
37 component: TabComponent
38 });
39 });
40
41 _defineProperty(this, "_renderIcon", ({
42 route,
43 focused,
44 tintColor,
45 horizontal = false
46 }) => {
47 const {
48 descriptors
49 } = this.props;
50 const descriptor = descriptors[route.key];
51 const options = descriptor.options;
52
53 if (options.tabBarIcon) {
54 return typeof options.tabBarIcon === 'function' ? options.tabBarIcon({
55 focused,
56 tintColor,
57 horizontal
58 }) : options.tabBarIcon;
59 }
60
61 return null;
62 });
63
64 _defineProperty(this, "_getLabelText", ({
65 route
66 }) => {
67 const {
68 descriptors
69 } = this.props;
70 const descriptor = descriptors[route.key];
71 const options = descriptor.options;
72
73 if (options.tabBarLabel) {
74 return options.tabBarLabel;
75 }
76
77 if (typeof options.title === 'string') {
78 return options.title;
79 }
80
81 return route.routeName;
82 });
83
84 _defineProperty(this, "_getAccessibilityLabel", ({
85 route
86 }) => {
87 const {
88 descriptors
89 } = this.props;
90 const descriptor = descriptors[route.key];
91 const options = descriptor.options;
92
93 if (typeof options.tabBarAccessibilityLabel !== 'undefined') {
94 return options.tabBarAccessibilityLabel;
95 }
96
97 const label = this._getLabelText({
98 route
99 });
100
101 if (typeof label === 'string') {
102 const {
103 routes
104 } = this.props.navigation.state;
105 return "".concat(label, ", tab, ").concat(routes.indexOf(route) + 1, " of ").concat(routes.length);
106 }
107
108 return undefined;
109 });
110
111 _defineProperty(this, "_getTestID", ({
112 route
113 }) => {
114 const {
115 descriptors
116 } = this.props;
117 const descriptor = descriptors[route.key];
118 const options = descriptor.options;
119 return options.tabBarTestID;
120 });
121
122 _defineProperty(this, "_makeDefaultHandler", ({
123 route,
124 navigation
125 }) => () => {
126 if (navigation.isFocused()) {
127 if (route.hasOwnProperty('index') && route.index > 0) {
128 // If current tab has a nested navigator, pop to top
129 navigation.dispatch(_reactNavigation.StackActions.popToTop({
130 key: route.key
131 }));
132 } else {
133 navigation.emit('refocus');
134 }
135 } else {
136 this._jumpTo(route.routeName);
137 }
138 });
139
140 _defineProperty(this, "_handleTabPress", ({
141 route
142 }) => {
143 this._isTabPress = true; // After tab press, handleIndexChange will be called synchronously
144 // So we reset it in promise callback
145
146 Promise.resolve().then(() => this._isTabPress = false);
147 const {
148 descriptors
149 } = this.props;
150 const descriptor = descriptors[route.key];
151 const {
152 navigation,
153 options
154 } = descriptor;
155
156 const defaultHandler = this._makeDefaultHandler({
157 route,
158 navigation
159 });
160
161 if (options.tabBarOnPress) {
162 options.tabBarOnPress({
163 navigation,
164 defaultHandler
165 });
166 } else {
167 defaultHandler();
168 }
169 });
170
171 _defineProperty(this, "_handleTabLongPress", ({
172 route
173 }) => {
174 const {
175 descriptors
176 } = this.props;
177 const descriptor = descriptors[route.key];
178 const {
179 navigation,
180 options
181 } = descriptor;
182
183 const defaultHandler = this._makeDefaultHandler({
184 route,
185 navigation
186 });
187
188 if (options.tabBarOnLongPress) {
189 options.tabBarOnLongPress({
190 navigation,
191 defaultHandler
192 });
193 } else {
194 defaultHandler();
195 }
196 });
197
198 _defineProperty(this, "_handleIndexChange", index => {
199 if (this._isTabPress) {
200 this._isTabPress = false;
201 return;
202 }
203
204 this._jumpTo(this.props.navigation.state.routes[index].routeName);
205 });
206
207 _defineProperty(this, "_jumpTo", routeName => {
208 const {
209 navigation
210 } = this.props;
211 navigation.dispatch(_reactNavigation.SwitchActions.jumpTo({
212 routeName,
213 key: navigation.state.key
214 }));
215 });
216
217 _defineProperty(this, "_isTabPress", false);
218 }
219
220 render() {
221 const {
222 descriptors,
223 navigation,
224 screenProps,
225 navigationConfig
226 } = this.props;
227 const {
228 state
229 } = navigation;
230 const route = state.routes[state.index];
231 const descriptor = descriptors[route.key];
232 return (
233 /*#__PURE__*/
234 // TODO: don't have time to fix it right now
235 // @ts-ignore
236 React.createElement(TabView, _extends({}, navigationConfig, descriptor.options, {
237 getLabelText: this._getLabelText,
238 getAccessibilityLabel: this._getAccessibilityLabel,
239 getTestID: this._getTestID,
240 renderIcon: this._renderIcon,
241 renderScene: this._renderScene,
242 onIndexChange: this._handleIndexChange,
243 onTabPress: this._handleTabPress,
244 onTabLongPress: this._handleTabLongPress,
245 navigation: navigation,
246 descriptors: descriptors,
247 screenProps: screenProps
248 }))
249 );
250 }
251
252 }
253
254 return (routes, config = {}) => {
255 const router = (0, _reactNavigation.TabRouter)(routes, config);
256 return (0, _reactNavigation.createNavigator)(NavigationView, router, config);
257 };
258}
259//# sourceMappingURL=createTabNavigator.js.map
\No newline at end of file