You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractDialog.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import PropTypes from 'prop-types';
  2. import { Component } from 'react';
  3. import { hideDialog } from '../actions';
  4. import { DIALOG_PROP_TYPES } from '../constants';
  5. /**
  6. * An abstract implementation of a dialog on Web/React and mobile/react-native.
  7. */
  8. export default class AbstractDialog extends Component {
  9. /**
  10. * <tt>AbstractDialog</tt> React <tt>Component</tt>'s prop types.
  11. *
  12. * @static
  13. */
  14. static propTypes = {
  15. ...DIALOG_PROP_TYPES,
  16. /**
  17. * The React <tt>Component</tt> children of <tt>AbstractDialog</tt>
  18. * which represents the dialog's body.
  19. */
  20. children: PropTypes.node,
  21. /**
  22. * Used to show/hide the dialog on cancel.
  23. */
  24. dispatch: PropTypes.func
  25. };
  26. /**
  27. * Initializes a new <tt>AbstractDialog</tt> instance.
  28. *
  29. * @param {Object} props - The read-only React <tt>Component</tt> props with
  30. * which the new instance is to be initialized.
  31. */
  32. constructor(props) {
  33. super(props);
  34. this._onCancel = this._onCancel.bind(this);
  35. this._onSubmit = this._onSubmit.bind(this);
  36. }
  37. /**
  38. * Dispatches a redux action to hide this dialog when it's canceled.
  39. *
  40. * @protected
  41. * @returns {void}
  42. */
  43. _onCancel() {
  44. const { onCancel } = this.props;
  45. if (!onCancel || onCancel()) {
  46. this.props.dispatch(hideDialog());
  47. }
  48. }
  49. /**
  50. * Dispatches a redux action to hide this dialog when it's submitted.
  51. *
  52. * @private
  53. * @param {string} value - The submitted value if any.
  54. * @returns {void}
  55. */
  56. _onSubmit(value) {
  57. const { onSubmit } = this.props;
  58. if (!onSubmit || onSubmit(value)) {
  59. this.props.dispatch(hideDialog());
  60. }
  61. }
  62. }