confirm.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import * as React from 'react';
  2. import {ModalRenderProps, openModal} from 'sentry/actionCreators/modal';
  3. import Button from 'sentry/components/button';
  4. import ButtonBar from 'sentry/components/buttonBar';
  5. import {t} from 'sentry/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. 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. * Callback when user confirms
  46. */
  47. onConfirm?: () => void;
  48. /**
  49. * Custom function to render the confirm button
  50. */
  51. renderConfirmButton?: (props: ConfirmButtonsRenderProps) => React.ReactNode;
  52. /**
  53. * Custom function to render the cancel button
  54. */
  55. renderCancelButton?: (props: ConfirmButtonsRenderProps) => React.ReactNode;
  56. /**
  57. * If true, will skip the confirmation modal and call `onConfirm` callback
  58. */
  59. bypass?: boolean;
  60. /**
  61. * Message to display to user when asking for confirmation
  62. */
  63. message?: React.ReactNode;
  64. /**
  65. * Used to render a message instead of using the static `message` prop.
  66. */
  67. renderMessage?: (renderProps: ConfirmMessageRenderProps) => React.ReactNode;
  68. /**
  69. * Callback function when user is in the confirming state called when the
  70. * confirm modal is opened
  71. */
  72. onConfirming?: () => void;
  73. /**
  74. * User cancels the modal
  75. */
  76. onCancel?: () => void;
  77. /**
  78. * Header of modal
  79. */
  80. header?: React.ReactNode;
  81. /**
  82. * Disables the confirm button.
  83. *
  84. * XXX: Once the modal has been opened mutating this property will _not_
  85. * propagate into the modal.
  86. *
  87. * If you need the confirm buttons disabled state to be reactively
  88. * controlled, consider using the renderMessage prop, which receives a
  89. * `disableConfirmButton` function that you may use to control the state of it.
  90. */
  91. disableConfirmButton?: boolean;
  92. /**
  93. * Button priority
  94. */
  95. priority?: React.ComponentProps<typeof Button>['priority'];
  96. /**
  97. * Text to show in the cancel button
  98. */
  99. cancelText?: React.ReactNode;
  100. /**
  101. * Text to show in the confirmation button
  102. */
  103. confirmText?: React.ReactNode;
  104. };
  105. type Props = 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 interstital 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 (!React.isValidElement(children)) {
  173. return null;
  174. }
  175. // TODO(ts): Understand why the return type of `cloneElement` is strange
  176. return React.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. * Is confirm button disabled
  196. */
  197. disableConfirmButton: boolean;
  198. /**
  199. * The callback registered from the rendered message to call
  200. */
  201. confirmCallback: null | (() => void);
  202. };
  203. class ConfirmModal extends React.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 (React.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. <React.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 onClick={this.handleClose}>{cancelText}</Button>
  275. )}
  276. {renderConfirmButton ? (
  277. renderConfirmButton({
  278. closeModal: this.props.closeModal,
  279. defaultOnClick: this.handleConfirm,
  280. })
  281. ) : (
  282. <Button
  283. data-test-id="confirm-button"
  284. disabled={this.state.disableConfirmButton}
  285. priority={priority}
  286. onClick={this.handleConfirm}
  287. autoFocus
  288. >
  289. {confirmText}
  290. </Button>
  291. )}
  292. </ButtonBar>
  293. </Footer>
  294. </React.Fragment>
  295. );
  296. }
  297. }
  298. export default Confirm;