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

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