confirm.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import {cloneElement, Component, Fragment, isValidElement} from 'react';
  2. import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  3. import Button, {ButtonProps} from 'sentry/components/button';
  4. import ButtonBar from 'sentry/components/buttonBar';
  5. import {t} from 'sentry/locale';
  6. export type ConfirmMessageRenderProps = {
  7. /**
  8. * Closes the modal, if `bypass` is true, will call `onConfirm` callback
  9. */
  10. close: (e: React.MouseEvent) => void;
  11. /**
  12. * Confirms the modal
  13. */
  14. confirm: () => 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. export type ConfirmButtonsRenderProps = {
  30. /**
  31. * Applications can call this function to manually close the modal.
  32. */
  33. closeModal: () => void;
  34. /**
  35. * The default onClick behavior, including closing the modal and triggering the
  36. * onConfirm / onCancel callbacks.
  37. */
  38. defaultOnClick: () => void;
  39. };
  40. type ChildrenRenderProps = {
  41. open: () => void;
  42. };
  43. export type OpenConfirmOptions = {
  44. /**
  45. * If true, will skip the confirmation modal and call `onConfirm` callback
  46. */
  47. bypass?: boolean;
  48. /**
  49. * Text to show in the cancel button
  50. */
  51. cancelText?: React.ReactNode;
  52. /**
  53. * Text to show in the confirmation button
  54. */
  55. confirmText?: React.ReactNode;
  56. /**
  57. * Disables the confirm button.
  58. *
  59. * XXX: Once the modal has been opened mutating this property will _not_
  60. * propagate into the modal.
  61. *
  62. * If you need the confirm buttons disabled state to be reactively
  63. * controlled, consider using the renderMessage prop, which receives a
  64. * `disableConfirmButton` function that you may use to control the state of it.
  65. */
  66. disableConfirmButton?: boolean;
  67. /**
  68. * Header of modal
  69. */
  70. header?: React.ReactNode;
  71. /**
  72. * Message to display to user when asking for confirmation
  73. */
  74. message?: React.ReactNode;
  75. /**
  76. * User cancels the modal
  77. */
  78. onCancel?: () => void;
  79. /**
  80. * Callback when user confirms
  81. */
  82. onConfirm?: () => void;
  83. /**
  84. * Callback function when user is in the confirming state called when the
  85. * confirm modal is opened
  86. */
  87. onConfirming?: () => void;
  88. /**
  89. * Button priority
  90. */
  91. priority?: ButtonProps['priority'];
  92. /**
  93. * Custom function to render the cancel button
  94. */
  95. renderCancelButton?: (props: ConfirmButtonsRenderProps) => React.ReactNode;
  96. /**
  97. * Custom function to render the confirm button
  98. */
  99. renderConfirmButton?: (props: ConfirmButtonsRenderProps) => React.ReactNode;
  100. /**
  101. * Used to render a message instead of using the static `message` prop.
  102. */
  103. renderMessage?: (renderProps: ConfirmMessageRenderProps) => React.ReactNode;
  104. };
  105. interface Props extends OpenConfirmOptions {
  106. /**
  107. * Render props to control rendering of the modal in its entirety
  108. */
  109. children?:
  110. | ((renderProps: ChildrenRenderProps) => React.ReactNode)
  111. | React.ReactElement<{disabled: boolean; onClick: (e: React.MouseEvent) => void}>;
  112. /**
  113. * Passed to `children` render function
  114. */
  115. disabled?: boolean;
  116. /**
  117. * Stop event propagation when opening the confirm modal
  118. */
  119. stopPropagation?: boolean;
  120. }
  121. /**
  122. * Opens a confirmation modal when called. The procedural version of the
  123. * `Confirm` component
  124. */
  125. export const openConfirmModal = ({
  126. bypass,
  127. onConfirming,
  128. priority = 'primary',
  129. cancelText = t('Cancel'),
  130. confirmText = t('Confirm'),
  131. disableConfirmButton = false,
  132. ...rest
  133. }: OpenConfirmOptions) => {
  134. if (bypass) {
  135. rest.onConfirm?.();
  136. return;
  137. }
  138. const modalProps = {
  139. ...rest,
  140. priority,
  141. confirmText,
  142. cancelText,
  143. disableConfirmButton,
  144. };
  145. onConfirming?.();
  146. openModal(renderProps => <ConfirmModal {...renderProps} {...modalProps} />);
  147. };
  148. /**
  149. * The confirm component is somewhat special in that you can wrap any
  150. * onClick-able element with this to trigger a interstitial confirmation modal.
  151. *
  152. * This is the declarative alternative to using openConfirmModal
  153. */
  154. function Confirm({
  155. disabled,
  156. children,
  157. stopPropagation = false,
  158. ...openConfirmOptions
  159. }: Props) {
  160. const triggerModal = (e?: React.MouseEvent) => {
  161. if (stopPropagation) {
  162. e?.stopPropagation();
  163. }
  164. if (disabled) {
  165. return;
  166. }
  167. openConfirmModal(openConfirmOptions);
  168. };
  169. if (typeof children === 'function') {
  170. return children({open: triggerModal});
  171. }
  172. if (!isValidElement(children)) {
  173. return null;
  174. }
  175. // TODO(ts): Understand why the return type of `cloneElement` is strange
  176. return cloneElement(children, {disabled, onClick: triggerModal}) as any;
  177. }
  178. type ModalProps = ModalRenderProps &
  179. Pick<
  180. Props,
  181. | 'priority'
  182. | 'renderMessage'
  183. | 'renderConfirmButton'
  184. | 'renderCancelButton'
  185. | 'message'
  186. | 'confirmText'
  187. | 'cancelText'
  188. | 'header'
  189. | 'onConfirm'
  190. | 'onCancel'
  191. | 'disableConfirmButton'
  192. >;
  193. type ModalState = {
  194. /**
  195. * The callback registered from the rendered message to call
  196. */
  197. confirmCallback: null | (() => void);
  198. /**
  199. * Is confirm button disabled
  200. */
  201. disableConfirmButton: boolean;
  202. };
  203. class ConfirmModal extends Component<ModalProps, ModalState> {
  204. state: ModalState = {
  205. disableConfirmButton: !!this.props.disableConfirmButton,
  206. confirmCallback: null,
  207. };
  208. confirming: boolean = false;
  209. handleClose = () => {
  210. const {disableConfirmButton, onCancel, closeModal} = this.props;
  211. onCancel?.();
  212. this.setState({disableConfirmButton: disableConfirmButton ?? false});
  213. // always reset `confirming` when modal visibility changes
  214. this.confirming = false;
  215. closeModal();
  216. };
  217. handleConfirm = () => {
  218. const {onConfirm, closeModal} = this.props;
  219. // `confirming` is used to ensure `onConfirm` or the confirm callback is
  220. // only called once
  221. if (!this.confirming) {
  222. onConfirm?.();
  223. this.state.confirmCallback?.();
  224. }
  225. this.setState({disableConfirmButton: true});
  226. this.confirming = true;
  227. closeModal();
  228. };
  229. get confirmMessage() {
  230. const {message, renderMessage} = this.props;
  231. if (typeof renderMessage === 'function') {
  232. return renderMessage({
  233. confirm: this.handleConfirm,
  234. close: this.handleClose,
  235. disableConfirmButton: (state: boolean) =>
  236. this.setState({disableConfirmButton: state}),
  237. setConfirmCallback: (confirmCallback: () => void) =>
  238. this.setState({confirmCallback}),
  239. });
  240. }
  241. if (isValidElement(message)) {
  242. return message;
  243. }
  244. return (
  245. <p>
  246. <strong>{message}</strong>
  247. </p>
  248. );
  249. }
  250. render() {
  251. const {
  252. Header,
  253. Body,
  254. Footer,
  255. priority,
  256. confirmText,
  257. cancelText,
  258. header,
  259. renderConfirmButton,
  260. renderCancelButton,
  261. } = this.props;
  262. return (
  263. <Fragment>
  264. {header && <Header>{header}</Header>}
  265. <Body>{this.confirmMessage}</Body>
  266. <Footer>
  267. <ButtonBar gap={2}>
  268. {renderCancelButton ? (
  269. renderCancelButton({
  270. closeModal: this.props.closeModal,
  271. defaultOnClick: this.handleClose,
  272. })
  273. ) : (
  274. <Button
  275. onClick={this.handleClose}
  276. aria-label={typeof cancelText === 'string' ? cancelText : t('Cancel')}
  277. >
  278. {cancelText ?? t('Cancel')}
  279. </Button>
  280. )}
  281. {renderConfirmButton ? (
  282. renderConfirmButton({
  283. closeModal: this.props.closeModal,
  284. defaultOnClick: this.handleConfirm,
  285. })
  286. ) : (
  287. <Button
  288. data-test-id="confirm-button"
  289. disabled={this.state.disableConfirmButton}
  290. priority={priority}
  291. onClick={this.handleConfirm}
  292. autoFocus
  293. aria-label={typeof confirmText === 'string' ? confirmText : t('Confirm')}
  294. >
  295. {confirmText ?? t('Confirm')}
  296. </Button>
  297. )}
  298. </ButtonBar>
  299. </Footer>
  300. </Fragment>
  301. );
  302. }
  303. }
  304. export default Confirm;