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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // @flow
  2. import { Component } from 'react';
  3. import { hideDialog } from '../actions';
  4. import type { DialogProps } from '../constants';
  5. /**
  6. * The type of the React {@code Component} props of {@link AbstractDialog}.
  7. */
  8. export type Props = {
  9. ...DialogProps,
  10. /**
  11. * Used to show/hide the dialog on cancel.
  12. */
  13. dispatch: Dispatch<*>
  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#componentWillMount()}. Invoked
  43. * immediately before mounting occurs.
  44. *
  45. * @inheritdoc
  46. */
  47. componentWillMount() {
  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, onCancel } = this.props;
  76. if ((typeof cancelDisabled === 'undefined' || !cancelDisabled)
  77. && (!onCancel || onCancel())) {
  78. this._hide();
  79. }
  80. }
  81. _onSubmit: (?string) => void;
  82. /**
  83. * Submits this {@code Dialog}. If the React {@code Component} prop
  84. * {@code onSubmit} is defined, the function that is the value of the prop
  85. * is invoked. If the function returns a {@code thenable}, then the
  86. * resolution of the {@code thenable} is awaited. If the submission
  87. * completes successfully, a redux action will be dispatched to hide this
  88. * dialog.
  89. *
  90. * @protected
  91. * @param {string} [value] - The submitted value if any.
  92. * @returns {void}
  93. */
  94. _onSubmit(value: ?string) {
  95. const { okDisabled, onSubmit } = this.props;
  96. if (typeof okDisabled === 'undefined' || !okDisabled) {
  97. this.setState({ submitting: true });
  98. // Invoke the React Compnent prop onSubmit if any.
  99. const r = !onSubmit || onSubmit(value);
  100. // If the invocation returns a thenable, await its resolution;
  101. // otherwise, treat the return value as a boolean indicating whether
  102. // the submission has completed successfully.
  103. let then;
  104. if (r) {
  105. switch (typeof r) {
  106. case 'function':
  107. case 'object':
  108. then = r.then;
  109. break;
  110. }
  111. }
  112. if (typeof then === 'function' && then.length === 2) {
  113. then.call(r, this._onSubmitFulfilled, this._onSubmitRejected);
  114. } else if (r) {
  115. this._onSubmitFulfilled();
  116. } else {
  117. this._onSubmitRejected();
  118. }
  119. }
  120. }
  121. _onSubmitFulfilled: () => void;
  122. /**
  123. * Notifies this {@code AbstractDialog} that it has been submitted
  124. * successfully. Dispatches a redux action to hide this dialog after it has
  125. * been submitted.
  126. *
  127. * @private
  128. * @returns {void}
  129. */
  130. _onSubmitFulfilled() {
  131. this._mounted && this.setState({ submitting: false });
  132. this._hide();
  133. }
  134. _onSubmitRejected: () => void;
  135. /**
  136. * Notifies this {@code AbstractDialog} that its submission has failed.
  137. *
  138. * @private
  139. * @returns {void}
  140. */
  141. _onSubmitRejected() {
  142. this._mounted && this.setState({ submitting: false });
  143. }
  144. }