Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

79 wiersze
2.6 KiB

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