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 10KB

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