UNPKG

react-navigation-drawer

Version:

Drawer navigator component for React Navigation

122 lines (93 loc) 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNavigation = require("react-navigation"); var DrawerActions = _interopRequireWildcard(require("./DrawerActions")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _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; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _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; } function withDefaultValue(obj, key, defaultValue) { // @ts-ignore if (obj.hasOwnProperty(key) && typeof obj[key] !== 'undefined') { return obj; } // @ts-ignore obj[key] = defaultValue; return obj; } const getActiveRouteKey = route => { if (route.routes && typeof route.index === 'number' && route.routes[route.index]) { return getActiveRouteKey(route.routes[route.index]); } return route.key; }; var _default = (routeConfigs, config = {}) => { config = _objectSpread({}, config); config = withDefaultValue(config, 'resetOnBlur', config.unmountInactiveRoutes ? true : !!config.resetOnBlur); config = withDefaultValue(config, 'backBehavior', 'initialRoute'); const switchRouter = (0, _reactNavigation.SwitchRouter)(routeConfigs, config); return _objectSpread({}, switchRouter, { getActionCreators(route, navStateKey) { return _objectSpread({ openDrawer: () => DrawerActions.openDrawer({ key: navStateKey }), closeDrawer: () => DrawerActions.closeDrawer({ key: navStateKey }), toggleDrawer: () => DrawerActions.toggleDrawer({ key: navStateKey }) }, switchRouter.getActionCreators(route, navStateKey)); }, getStateForAction(action, state) { // Set up the initial state if needed if (!state) { return _objectSpread({}, switchRouter.getStateForAction(action, undefined), { isDrawerOpen: false }); } const isRouterTargeted = action.key == null || action.key === state.key; if (isRouterTargeted) { // Only handle actions that are meant for this drawer, as specified by action.key. if (action.type === DrawerActions.CLOSE_DRAWER || action.type === _reactNavigation.NavigationActions.BACK && state.isDrawerOpen) { return _objectSpread({}, state, { isDrawerOpen: false }); } if (action.type === DrawerActions.OPEN_DRAWER) { return _objectSpread({}, state, { isDrawerOpen: true }); } if (action.type === DrawerActions.TOGGLE_DRAWER) { return _objectSpread({}, state, { isDrawerOpen: !state.isDrawerOpen }); } } // Fall back on switch router for screen switching logic, and handling of child routers const switchedState = switchRouter.getStateForAction(action, state); if (switchedState === null) { // The switch router or a child router is attempting to swallow this action. We return null to allow this. return null; } // Has the switch router changed the state? if (switchedState !== state) { // If any navigation has happened, and the drawer is maybe open, make sure to close it if (getActiveRouteKey(switchedState) !== getActiveRouteKey(state) && state.isDrawerOpen) { return _objectSpread({}, switchedState, { isDrawerOpen: false }); } // At this point, return the state as defined by the switch router. // The active route key hasn't changed, so this most likely means that a child router has returned // a new state like a param change, but the same key is still active and the drawer will remain open return switchedState; } return state; } }); }; exports.default = _default; //# sourceMappingURL=DrawerRouter.js.map