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

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