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.web.js 9.1KB

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