1 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
2 |
|
3 | 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; }
|
4 |
|
5 | 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; }
|
6 |
|
7 | import React from 'react';
|
8 | import PropTypes from 'prop-types';
|
9 | import invariant from 'invariant';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var Prompt = function (_React$Component) {
|
17 | _inherits(Prompt, _React$Component);
|
18 |
|
19 | function Prompt() {
|
20 | _classCallCheck(this, Prompt);
|
21 |
|
22 | return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
|
23 | }
|
24 |
|
25 | Prompt.prototype.enable = function enable(message) {
|
26 | if (this.unblock) this.unblock();
|
27 |
|
28 | this.unblock = this.context.router.history.block(message);
|
29 | };
|
30 |
|
31 | Prompt.prototype.disable = function disable() {
|
32 | if (this.unblock) {
|
33 | this.unblock();
|
34 | this.unblock = null;
|
35 | }
|
36 | };
|
37 |
|
38 | Prompt.prototype.componentWillMount = function componentWillMount() {
|
39 | invariant(this.context.router, 'You should not use <Prompt> outside a <Router>');
|
40 |
|
41 | if (this.props.when) this.enable(this.props.message);
|
42 | };
|
43 |
|
44 | Prompt.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
45 | if (nextProps.when) {
|
46 | if (!this.props.when || this.props.message !== nextProps.message) this.enable(nextProps.message);
|
47 | } else {
|
48 | this.disable();
|
49 | }
|
50 | };
|
51 |
|
52 | Prompt.prototype.componentWillUnmount = function componentWillUnmount() {
|
53 | this.disable();
|
54 | };
|
55 |
|
56 | Prompt.prototype.render = function render() {
|
57 | return null;
|
58 | };
|
59 |
|
60 | return Prompt;
|
61 | }(React.Component);
|
62 |
|
63 | Prompt.propTypes = {
|
64 | when: PropTypes.bool,
|
65 | message: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired
|
66 | };
|
67 | Prompt.defaultProps = {
|
68 | when: true
|
69 | };
|
70 | Prompt.contextTypes = {
|
71 | router: PropTypes.shape({
|
72 | history: PropTypes.shape({
|
73 | block: PropTypes.func.isRequired
|
74 | }).isRequired
|
75 | }).isRequired
|
76 | };
|
77 |
|
78 |
|
79 | export default Prompt; |
\ | No newline at end of file |