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.1KB

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