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

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