UNPKG

react-junctions

Version:

Junction-based routing for React.

116 lines (112 loc) 4.34 kB
"use strict"; /** * This component is based on the `<Prompt>` component in react-router v4: * https://github.com/ReactTraining/react-router/blob/bb969201817b4ce1667d3933a69497777e1cad15/packages/react-router/modules/Prompt.js */ var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /* MIT License Copyright (c) 2016-present React Training Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var React = require("react"); var PropTypes = require("prop-types"); /** * The public API for prompting the user before navigating away * from a screen with a component. */ var ExitPrompt = /** @class */ (function (_super) { __extends(ExitPrompt, _super); function ExitPrompt(props, context) { var _this = _super.call(this, props, context) || this; _this.navigation = _this.props.env ? _this.props.env.navigation : context.navigation; if (process.env.NODE_ENV !== 'production') { if (!_this.navigation) { console.warn("An <ExitPrompt> was created without access to a \"navigation\" object. " + "You can provide a Navigation object through an \"env\" prop, or via " + "React Context."); } } return _this; } ExitPrompt.prototype.enable = function (message) { if (this.unblock) { this.unblock(); } this.unblock = this.navigation.block(message); }; ExitPrompt.prototype.disable = function () { if (this.unblock) { this.unblock(); delete this.unblock; } }; ExitPrompt.prototype.componentDidMount = function () { if (this.props.when) { this.enable(this.props.message); } }; ExitPrompt.prototype.componentDidUpdate = function (nextProps) { if (nextProps.when) { if (!this.props.when || this.props.message !== nextProps.message) { this.enable(nextProps.message); } } else { this.disable(); } }; ExitPrompt.prototype.componentWillUnmount = function () { this.disable(); }; ExitPrompt.prototype.render = function () { return null; }; ExitPrompt.propTypes = { env: PropTypes.shape({ navigation: PropTypes.shape({ block: PropTypes.func.isRequired, }) }), when: PropTypes.bool, message: PropTypes.oneOfType([ PropTypes.func, PropTypes.string ]).isRequired }; ExitPrompt.defaultProps = { when: true, }; ExitPrompt.contextTypes = { navigation: PropTypes.shape({ block: PropTypes.func.isRequired }).isRequired }; return ExitPrompt; }(React.Component)); exports.ExitPrompt = ExitPrompt; //# sourceMappingURL=ExitPrompt.js.map