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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // @flow
  2. import { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { hideDialog } from '../actions';
  5. import type { DialogProps } from '../constants';
  6. /**
  7. * The type of the React {@code Component} props of {@link AbstractDialog}.
  8. */
  9. export type Props = DialogProps & {
  10. /**
  11. * Used to show/hide the dialog on cancel.
  12. */
  13. dispatch: Dispatch<any>
  14. };
  15. /**
  16. * The type of the React {@code Component} state of {@link AbstractDialog}.
  17. */
  18. export type State = {
  19. submitting: ?boolean
  20. };
  21. /**
  22. * An abstract implementation of a dialog on Web/React and mobile/react-native.
  23. */
  24. export default class AbstractDialog<P : Props, S : State>
  25. extends Component<P, S> {
  26. _mounted: boolean;
  27. /**
  28. * Initializes a new {@code AbstractDialog} instance.
  29. *
  30. * @param {Object} props - The read-only React {@code Component} props with
  31. * which the new instance is to be initialized.
  32. */
  33. constructor(props: P) {
  34. super(props);
  35. // Bind event handlers so they are only bound once per instance.
  36. this._onCancel = this._onCancel.bind(this);
  37. this._onSubmit = this._onSubmit.bind(this);
  38. this._onSubmitFulfilled = this._onSubmitFulfilled.bind(this);
  39. this._onSubmitRejected = this._onSubmitRejected.bind(this);
  40. }
  41. /**
  42. * Implements React's {@link Component#componentDidMount()}. Invoked
  43. * immediately before mounting occurs.
  44. *
  45. * @inheritdoc
  46. */
  47. componentDidMount() {
  48. this._mounted = true;
  49. }
  50. /**
  51. * Implements React's {@link Component#componentWillUnmount()}. Invoked
  52. * immediately before this component is unmounted and destroyed.
  53. *
  54. * @inheritdoc
  55. */
  56. componentWillUnmount() {
  57. this._mounted = false;
  58. }
  59. /**
  60. * Dispatches a redux action to hide this dialog.
  61. *
  62. * @returns {*} The return value of {@link hideDialog}.
  63. */
  64. _hide() {
  65. return this.props.dispatch(hideDialog());
  66. }
  67. _onCancel: () => void;
  68. /**
  69. * Dispatches a redux action to hide this dialog when it's canceled.
  70. *
  71. * @protected
  72. * @returns {void}
  73. */
  74. _onCancel() {
  75. const { cancelDisabled = false, onCancel } = this.props;
  76. if (!cancelDisabled && (!onCancel || onCancel())) {
  77. this._hide();
  78. }
  79. }
  80. _onSubmit: (?string) => void;
  81. /**
  82. * Submits this {@code Dialog}. If the React {@code Component} prop
  83. * {@code onSubmit} is defined, the function that is the value of the prop
  84. * is invoked. If the function returns a {@code thenable}, then the
  85. * resolution of the {@code thenable} is awaited. If the submission
  86. * completes successfully, a redux action will be dispatched to hide this
  87. * dialog.
  88. *
  89. * @protected
  90. * @param {string} [value] - The submitted value if any.
  91. * @returns {void}
  92. */
  93. _onSubmit(value: ?string) {
  94. const { okDisabled = false, onSubmit } = this.props;
  95. if (!okDisabled) {
  96. this.setState({ submitting: true });
  97. // Invoke the React Component prop onSubmit if any.
  98. const r = !onSubmit || onSubmit(value);
  99. // If the invocation returns a thenable, await its resolution;
  100. // otherwise, treat the return value as a boolean indicating whether
  101. // the submission has completed successfully.
  102. let then;
  103. if (r) {
  104. switch (typeof r) {
  105. case 'function':
  106. case 'object':
  107. then = r.then;
  108. break;
  109. }
  110. }
  111. if (typeof then === 'function' && then.length === 2) {
  112. then.call(r, this._onSubmitFulfilled, this._onSubmitRejected);
  113. } else if (r) {
  114. this._onSubmitFulfilled();
  115. } else {
  116. this._onSubmitRejected();
  117. }
  118. }
  119. }
  120. _onSubmitFulfilled: () => void;
  121. /**
  122. * Notifies this {@code AbstractDialog} that it has been submitted
  123. * successfully. Dispatches a redux action to hide this dialog after it has
  124. * been submitted.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _onSubmitFulfilled() {
  130. this._mounted && this.setState({ submitting: false });
  131. this._hide();
  132. }
  133. _onSubmitRejected: () => void;
  134. /**
  135. * Notifies this {@code AbstractDialog} that its submission has failed.
  136. *
  137. * @private
  138. * @returns {void}
  139. */
  140. _onSubmitRejected() {
  141. this._mounted && this.setState({ submitting: false });
  142. }
  143. }