Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. exports.__esModule = true;
  3. var _react = require("react");
  4. var _react2 = _interopRequireDefault(_react);
  5. var _propTypes = require("prop-types");
  6. var _propTypes2 = _interopRequireDefault(_propTypes);
  7. var _invariant = require("invariant");
  8. var _invariant2 = _interopRequireDefault(_invariant);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. 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; }
  12. 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; }
  13. /**
  14. * The public API for prompting the user before navigating away
  15. * from a screen with a component.
  16. */
  17. var Prompt = function (_React$Component) {
  18. _inherits(Prompt, _React$Component);
  19. function Prompt() {
  20. _classCallCheck(this, Prompt);
  21. return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  22. }
  23. Prompt.prototype.enable = function enable(message) {
  24. if (this.unblock) this.unblock();
  25. this.unblock = this.context.router.history.block(message);
  26. };
  27. Prompt.prototype.disable = function disable() {
  28. if (this.unblock) {
  29. this.unblock();
  30. this.unblock = null;
  31. }
  32. };
  33. Prompt.prototype.componentWillMount = function componentWillMount() {
  34. (0, _invariant2.default)(this.context.router, "You should not use <Prompt> outside a <Router>");
  35. if (this.props.when) this.enable(this.props.message);
  36. };
  37. Prompt.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  38. if (nextProps.when) {
  39. if (!this.props.when || this.props.message !== nextProps.message) this.enable(nextProps.message);
  40. } else {
  41. this.disable();
  42. }
  43. };
  44. Prompt.prototype.componentWillUnmount = function componentWillUnmount() {
  45. this.disable();
  46. };
  47. Prompt.prototype.render = function render() {
  48. return null;
  49. };
  50. return Prompt;
  51. }(_react2.default.Component);
  52. Prompt.propTypes = {
  53. when: _propTypes2.default.bool,
  54. message: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]).isRequired
  55. };
  56. Prompt.defaultProps = {
  57. when: true
  58. };
  59. Prompt.contextTypes = {
  60. router: _propTypes2.default.shape({
  61. history: _propTypes2.default.shape({
  62. block: _propTypes2.default.func.isRequired
  63. }).isRequired
  64. }).isRequired
  65. };
  66. exports.default = Prompt;