confirm.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import * as React from 'react';
  2. import {ModalRenderProps, openModal} from 'app/actionCreators/modal';
  3. import Button from 'app/components/button';
  4. import ButtonBar from 'app/components/buttonBar';
  5. import {t} from 'app/locale';
  6. export type ConfirmMessageRenderProps = {
  7. /**
  8. * Confirms the modal
  9. */
  10. confirm: () => void;
  11. /**
  12. * Closes the modal, if `bypass` is true, will call `onConfirm` callback
  13. */
  14. close: (e: React.MouseEvent) => void;
  15. /**
  16. * Set the disabled state of the confirm button
  17. */
  18. disableConfirmButton: (disable: boolean) => void;
  19. /**
  20. * When the modal is confirmed the function registered will be called.
  21. *
  22. * Useful if your rendered message contains some functionality that should be
  23. * triggered upon the modal being confirmed.
  24. *
  25. * This should be called in the components componentDidMount.
  26. */
  27. setConfirmCallback: (cb: () => void) => void;
  28. };
  29. type ChildrenRenderProps = {
  30. open: () => void;
  31. };
  32. type Props = {
  33. /**
  34. * Callback when user confirms
  35. */
  36. onConfirm?: () => void;
  37. /**
  38. * If true, will skip the confirmation modal and call `onConfirm` callback
  39. */
  40. bypass?: boolean;
  41. /**
  42. * Message to display to user when asking for confirmation
  43. */
  44. message?: React.ReactNode;
  45. /**
  46. * Used to render a message instead of using the static `message` prop.
  47. */
  48. renderMessage?: (renderProps: ConfirmMessageRenderProps) => React.ReactNode;
  49. /**
  50. * Render props to control rendering of the modal in its entirety
  51. */
  52. children?:
  53. | ((renderProps: ChildrenRenderProps) => React.ReactNode)
  54. | React.ReactElement<{disabled: boolean; onClick: (e: React.MouseEvent) => void}>;
  55. /**
  56. * Passed to `children` render function
  57. */
  58. disabled?: boolean;
  59. /**
  60. * Callback function when user is in the confirming state called when the
  61. * confirm modal is opened
  62. */
  63. onConfirming?: () => void;
  64. /**
  65. * User cancels the modal
  66. */
  67. onCancel?: () => void;
  68. /**
  69. * Header of modal
  70. */
  71. header?: React.ReactNode;
  72. /**
  73. * Stop event propagation when opening the confirm modal
  74. */
  75. stopPropagation?: boolean;
  76. /**
  77. * Disables the confirm button.
  78. *
  79. * XXX: Once the modal has been opened mutating this property will _not_
  80. * propagate into the modal.
  81. *
  82. * If you need the confirm buttons disabled state to be reactively
  83. * controlled, consider using the renderMessage prop, which receives a
  84. * `disableConfirmButton` function that you may use to control the state of it.
  85. */
  86. disableConfirmButton?: boolean;
  87. /**
  88. * Button priority
  89. */
  90. priority?: React.ComponentProps<typeof Button>['priority'];
  91. /**
  92. * Text to show in the cancel button
  93. */
  94. cancelText?: React.ReactNode;
  95. /**
  96. * Text to show in the confirmation button
  97. */
  98. confirmText?: React.ReactNode;
  99. };
  100. function Confirm({
  101. bypass,
  102. renderMessage,
  103. message,
  104. header,
  105. disabled,
  106. children,
  107. onConfirm,
  108. onConfirming,
  109. onCancel,
  110. priority = 'primary',
  111. cancelText = t('Cancel'),
  112. confirmText = t('Confirm'),
  113. stopPropagation = false,
  114. disableConfirmButton = false,
  115. }: Props) {
  116. const triggerModal = (e?: React.MouseEvent) => {
  117. if (stopPropagation) {
  118. e?.stopPropagation();
  119. }
  120. if (disabled) {
  121. return;
  122. }
  123. if (bypass) {
  124. onConfirm?.();
  125. return;
  126. }
  127. onConfirming?.();
  128. const modalProps = {
  129. priority,
  130. renderMessage,
  131. message,
  132. confirmText,
  133. cancelText,
  134. header,
  135. onConfirm,
  136. onCancel,
  137. disableConfirmButton,
  138. };
  139. openModal(renderProps => <ConfirmModal {...renderProps} {...modalProps} />);
  140. };
  141. if (typeof children === 'function') {
  142. return children({open: triggerModal});
  143. }
  144. if (!React.isValidElement(children)) {
  145. return null;
  146. }
  147. // TODO(ts): Understand why the return type of `cloneElement` is strange
  148. return React.cloneElement(children, {disabled, onClick: triggerModal}) as any;
  149. }
  150. type ModalProps = ModalRenderProps &
  151. Pick<
  152. Props,
  153. | 'priority'
  154. | 'renderMessage'
  155. | 'message'
  156. | 'confirmText'
  157. | 'cancelText'
  158. | 'header'
  159. | 'onConfirm'
  160. | 'onCancel'
  161. | 'disableConfirmButton'
  162. >;
  163. type ModalState = {
  164. /**
  165. * Is confirm button disabled
  166. */
  167. disableConfirmButton: boolean;
  168. /**
  169. * The callback registered from the rendered message to call
  170. */
  171. confirmCallback: null | (() => void);
  172. };
  173. class ConfirmModal extends React.Component<ModalProps, ModalState> {
  174. state: ModalState = {
  175. disableConfirmButton: !!this.props.disableConfirmButton,
  176. confirmCallback: null,
  177. };
  178. confirming: boolean = false;
  179. handleClose = () => {
  180. const {disableConfirmButton, onCancel, closeModal} = this.props;
  181. onCancel?.();
  182. this.setState({disableConfirmButton: disableConfirmButton ?? false});
  183. // always reset `confirming` when modal visibility changes
  184. this.confirming = false;
  185. closeModal();
  186. };
  187. handleConfirm = () => {
  188. const {onConfirm, closeModal} = this.props;
  189. // `confirming` is used to ensure `onConfirm` or the confirm callback is
  190. // only called once
  191. if (!this.confirming) {
  192. onConfirm?.();
  193. this.state.confirmCallback?.();
  194. }
  195. this.setState({disableConfirmButton: true});
  196. this.confirming = true;
  197. closeModal();
  198. };
  199. get confirmMessage() {
  200. const {message, renderMessage} = this.props;
  201. if (typeof renderMessage === 'function') {
  202. return renderMessage({
  203. confirm: this.handleConfirm,
  204. close: this.handleClose,
  205. disableConfirmButton: (state: boolean) =>
  206. this.setState({disableConfirmButton: state}),
  207. setConfirmCallback: (confirmCallback: () => void) =>
  208. this.setState({confirmCallback}),
  209. });
  210. }
  211. if (React.isValidElement(message)) {
  212. return message;
  213. }
  214. return (
  215. <p>
  216. <strong>{message}</strong>
  217. </p>
  218. );
  219. }
  220. render() {
  221. const {Header, Body, Footer, priority, confirmText, cancelText, header} = this.props;
  222. return (
  223. <React.Fragment>
  224. {header && <Header>{header}</Header>}
  225. <Body>{this.confirmMessage}</Body>
  226. <Footer>
  227. <ButtonBar gap={2}>
  228. <Button onClick={this.handleClose}>{cancelText}</Button>
  229. <Button
  230. data-test-id="confirm-button"
  231. disabled={this.state.disableConfirmButton}
  232. priority={priority}
  233. onClick={this.handleConfirm}
  234. autoFocus
  235. >
  236. {confirmText}
  237. </Button>
  238. </ButtonBar>
  239. </Footer>
  240. </React.Fragment>
  241. );
  242. }
  243. }
  244. export default Confirm;