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.

StatelessDialog.js 9.7KB

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