您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StatelessDialog.tsx 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* eslint-disable lines-around-comment */
  2. import Modal, { ModalFooter } from '@atlaskit/modal-dialog';
  3. import { Theme } from '@mui/material';
  4. import { withStyles } from '@mui/styles';
  5. import React, { Component, ReactElement } from 'react';
  6. import { WithTranslation } from 'react-i18next';
  7. import { translate } from '../../../i18n/functions';
  8. import Button from '../../../ui/components/web/Button';
  9. import { BUTTON_TYPES } from '../../../ui/constants';
  10. import type { DialogProps } from '../../constants';
  11. import ModalHeader from './ModalHeader';
  12. /**
  13. * The ID to be used for the cancel button if enabled.
  14. *
  15. * @type {string}
  16. */
  17. const CANCEL_BUTTON_ID = 'modal-dialog-cancel-button';
  18. /**
  19. * The ID to be used for the ok button if enabled.
  20. *
  21. * @type {string}
  22. */
  23. const OK_BUTTON_ID = 'modal-dialog-ok-button';
  24. /**
  25. * The type of the React {@code Component} props of {@link StatelessDialog}.
  26. *
  27. * @static
  28. */
  29. interface Props extends DialogProps, WithTranslation {
  30. /**
  31. * An object containing the CSS classes.
  32. */
  33. classes: any;
  34. /**
  35. * Custom dialog header that replaces the standard heading.
  36. */
  37. customHeader?: ReactElement<any> | Function;
  38. /**
  39. * Disables dismissing the dialog when the blanket is clicked. Enabled
  40. * by default.
  41. */
  42. disableBlanketClickDismiss: boolean;
  43. /*
  44. * True if listening for the Enter key should be disabled.
  45. */
  46. disableEnter: boolean;
  47. /**
  48. * If true, no footer will be displayed.
  49. */
  50. disableFooter?: boolean;
  51. /**
  52. * If true, the cancel button will not display but cancel actions, like
  53. * clicking the blanket, will cancel.
  54. */
  55. hideCancelButton: boolean;
  56. /**
  57. * If true, the close icon button will not be displayed.
  58. */
  59. hideCloseIconButton: boolean;
  60. /**
  61. * Whether the dialog is modal. This means clicking on the blanket will
  62. * leave the dialog open. No cancel button.
  63. */
  64. isModal: boolean;
  65. /**
  66. * The handler for the event when clicking the 'confirmNo' button.
  67. * Defaults to onCancel if absent.
  68. */
  69. onDecline?: () => void;
  70. /**
  71. * Callback invoked when setting the ref of the Dialog.
  72. */
  73. onDialogRef?: Function;
  74. /**
  75. * Disables rendering of the submit button.
  76. */
  77. submitDisabled: boolean;
  78. /**
  79. * Width of the dialog, can be:
  80. * - 'small' (400px), 'medium' (600px), 'large' (800px),
  81. * 'x-large' (968px)
  82. * - integer value for pixel width
  83. * - string value for percentage.
  84. */
  85. width: string;
  86. }
  87. /**
  88. * Creates the styles for the component.
  89. *
  90. * @param {Object} theme - The theme.
  91. * @returns {Object}
  92. */
  93. const styles = (theme: Theme) => {
  94. return {
  95. footer: {
  96. boxShadow: 'none'
  97. },
  98. buttonContainer: {
  99. display: 'flex',
  100. '& > button:first-child': {
  101. marginRight: theme.spacing(2)
  102. }
  103. }
  104. };
  105. };
  106. /**
  107. * Web dialog that uses atlaskit modal-dialog to display dialogs.
  108. */
  109. class StatelessDialog extends Component<Props> {
  110. static defaultProps = {
  111. hideCloseIconButton: false
  112. };
  113. /**
  114. * Initializes a new {@code StatelessDialog} instance.
  115. *
  116. * @param {Object} props - The read-only properties with which the new
  117. * instance is to be initialized.
  118. */
  119. constructor(props: Props) {
  120. super(props);
  121. // Bind event handlers so they are only bound once for every instance.
  122. this._onCancel = this._onCancel.bind(this);
  123. this._onDialogDismissed = this._onDialogDismissed.bind(this);
  124. this._onKeyPress = this._onKeyPress.bind(this);
  125. this._onSubmit = this._onSubmit.bind(this);
  126. this._renderFooter = this._renderFooter.bind(this);
  127. this._onDialogRef = this._onDialogRef.bind(this);
  128. }
  129. /**
  130. * Implements React's {@link Component#render()}.
  131. *
  132. * @inheritdoc
  133. * @returns {ReactElement}
  134. */
  135. render() {
  136. const {
  137. customHeader,
  138. children,
  139. hideCloseIconButton,
  140. t,
  141. titleString,
  142. titleKey,
  143. width
  144. } = this.props;
  145. return (
  146. <Modal
  147. autoFocus = { true }
  148. components = {{
  149. // @ts-ignore
  150. Header: customHeader ? customHeader : props => (
  151. // @ts-ignore
  152. <ModalHeader
  153. { ...props }
  154. heading = { titleString || t(titleKey ?? '') }
  155. hideCloseIconButton = { hideCloseIconButton } />
  156. )
  157. }}
  158. footer = { this._renderFooter }
  159. i18n = { this.props.i18n }
  160. onClose = { this._onDialogDismissed }
  161. onDialogDismissed = { this._onDialogDismissed }
  162. shouldCloseOnEscapePress = { true }
  163. width = { width || 'medium' }>
  164. <div
  165. onKeyPress = { this._onKeyPress }
  166. ref = { this._onDialogRef }>
  167. <form
  168. className = 'modal-dialog-form'
  169. id = 'modal-dialog-form'
  170. onSubmit = { this._onSubmit }>
  171. { children }
  172. </form>
  173. </div>
  174. </Modal>
  175. );
  176. }
  177. /**
  178. * Returns a ReactElement to display buttons for closing the modal.
  179. *
  180. * @param {Object} propsFromModalFooter - The props passed in from the
  181. * {@link ModalFooter} component.
  182. * @private
  183. * @returns {ReactElement}
  184. */
  185. _renderFooter(propsFromModalFooter: any) {
  186. // Filter out falsy (null) values because {@code ButtonGroup} will error
  187. // if passed in anything but buttons with valid type props.
  188. const buttons = [
  189. this._renderCancelButton(),
  190. this._renderOKButton()
  191. ].filter(Boolean);
  192. if (this.props.disableFooter) {
  193. return null;
  194. }
  195. return (
  196. <ModalFooter
  197. className = { this.props.classes.footer }
  198. showKeyline = { propsFromModalFooter.showKeyline } >
  199. {
  200. /**
  201. * Atlaskit has this empty span (JustifySim) so...
  202. */
  203. }
  204. <span />
  205. <div className = { this.props.classes.buttonContainer }>
  206. { buttons }
  207. </div>
  208. </ModalFooter>
  209. );
  210. }
  211. /**
  212. * Dispatches action to hide the dialog.
  213. *
  214. * @returns {void}
  215. */
  216. _onCancel() {
  217. if (!this.props.isModal) {
  218. const { onCancel } = this.props;
  219. onCancel?.();
  220. }
  221. }
  222. /**
  223. * Handles click on the blanket area.
  224. *
  225. * @returns {void}
  226. */
  227. _onDialogDismissed() {
  228. if (!this.props.disableBlanketClickDismiss) {
  229. this._onCancel();
  230. }
  231. }
  232. /**
  233. * Dispatches the action when submitting the dialog.
  234. *
  235. * @private
  236. * @param {string} value - The submitted value if any.
  237. * @returns {void}
  238. */
  239. _onSubmit(value?: any) {
  240. const { onSubmit } = this.props;
  241. onSubmit?.(value);
  242. }
  243. /**
  244. * Renders Cancel button.
  245. *
  246. * @private
  247. * @returns {ReactElement|null} The Cancel button if enabled and dialog is
  248. * not modal.
  249. */
  250. _renderCancelButton() {
  251. if (this.props.cancelDisabled
  252. || this.props.isModal
  253. || this.props.hideCancelButton) {
  254. return null;
  255. }
  256. const {
  257. t,
  258. onDecline
  259. } = this.props;
  260. return (
  261. <Button
  262. accessibilityLabel = { t(this.props.cancelKey || 'dialog.Cancel') }
  263. id = { CANCEL_BUTTON_ID }
  264. key = { CANCEL_BUTTON_ID }
  265. label = { t(this.props.cancelKey || 'dialog.Cancel') }
  266. onClick = { onDecline || this._onCancel }
  267. size = 'small'
  268. type = { BUTTON_TYPES.TERTIARY } />
  269. );
  270. }
  271. /**
  272. * Renders OK button.
  273. *
  274. * @private
  275. * @returns {ReactElement|null} The OK button if enabled.
  276. */
  277. _renderOKButton() {
  278. const {
  279. submitDisabled,
  280. t
  281. } = this.props;
  282. if (submitDisabled) {
  283. return null;
  284. }
  285. return (
  286. <Button
  287. accessibilityLabel = { t(this.props.okKey || 'dialog.Ok') }
  288. disabled = { this.props.okDisabled }
  289. id = { OK_BUTTON_ID }
  290. key = { OK_BUTTON_ID }
  291. label = { t(this.props.okKey || 'dialog.Ok') }
  292. onClick = { this._onSubmit }
  293. size = 'small' />
  294. );
  295. }
  296. /**
  297. * Callback invoked when setting the ref of the dialog's child passing the Modal ref.
  298. * It is done this way because we cannot directly access the ref of the Modal component.
  299. *
  300. * @param {HTMLElement} element - The DOM element for the dialog.
  301. * @private
  302. * @returns {void}
  303. */
  304. _onDialogRef(element?: any) {
  305. this.props.onDialogRef?.(element?.parentNode);
  306. }
  307. /**
  308. * Handles 'Enter' key in the dialog to submit/hide dialog depending on
  309. * the available buttons and their disabled state.
  310. *
  311. * @param {Object} event - The key event.
  312. * @private
  313. * @returns {void}
  314. */
  315. _onKeyPress(event: React.KeyboardEvent) {
  316. // If the event coming to the dialog has been subject to preventDefault
  317. // we don't handle it here.
  318. if (event.defaultPrevented) {
  319. return;
  320. }
  321. if (event.key === 'Enter' && !this.props.disableEnter) {
  322. event.preventDefault();
  323. event.stopPropagation();
  324. if (this.props.submitDisabled && !this.props.cancelDisabled) {
  325. this._onCancel();
  326. } else if (!this.props.okDisabled) {
  327. this._onSubmit();
  328. }
  329. }
  330. }
  331. }
  332. export default translate(withStyles(styles)(StatelessDialog));