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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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._setDialogElement = this._setDialogElement.bind(this);
  89. this._Footer = this._createFooterConstructor(props);
  90. }
  91. /**
  92. * React Component method that executes before the component is updated.
  93. *
  94. * @inheritdoc
  95. * @param {Object} nextProps - The next properties, before the update.
  96. * @returns {void}
  97. */
  98. componentWillUpdate(nextProps) {
  99. // If button states have changed, update the Footer constructor function
  100. // so buttons of the proper state are rendered.
  101. if (nextProps.okDisabled !== this.props.okDisabled
  102. || nextProps.cancelDisabled !== this.props.cancelDisabled
  103. || nextProps.submitDisabled !== this.props.submitDisabled) {
  104. this._Footer = this._createFooterConstructor(nextProps);
  105. }
  106. }
  107. /**
  108. * Implements React's {@link Component#render()}.
  109. *
  110. * @inheritdoc
  111. * @returns {ReactElement}
  112. */
  113. render() {
  114. const {
  115. children,
  116. t /* The following fixes a flow error: */ = _.identity,
  117. titleString,
  118. titleKey,
  119. width
  120. } = this.props;
  121. return (
  122. <Modal
  123. autoFocus = { true }
  124. footer = { this._Footer }
  125. heading = { titleString || t(titleKey) }
  126. i18n = { this.props.i18n }
  127. onClose = { this._onDialogDismissed }
  128. onDialogDismissed = { this._onDialogDismissed }
  129. shouldCloseOnEscapePress = { true }
  130. width = { width || 'medium' }>
  131. {
  132. /**
  133. * Wrapping the contents of {@link Modal} with
  134. * {@link ContextProvider} is a workaround for the
  135. * i18n context becoming undefined as modal gets rendered
  136. * outside of the normal react app context.
  137. */
  138. }
  139. <ContextProvider i18n = { this.props.i18n }>
  140. <div
  141. onKeyDown = { this._onKeyDown }
  142. ref = { this._setDialogElement }>
  143. <form
  144. className = 'modal-dialog-form'
  145. id = 'modal-dialog-form'
  146. onSubmit = { this._onSubmit }>
  147. { children }
  148. </form>
  149. </div>
  150. </ContextProvider>
  151. </Modal>
  152. );
  153. }
  154. _onCancel: () => Function;
  155. /**
  156. * Returns a functional component to be used for the modal footer.
  157. *
  158. * @param {Object} options - The configuration for how the buttons in the
  159. * footer should display. Essentially {@code StatelessDialog} props should
  160. * be passed in.
  161. * @private
  162. * @returns {ReactElement}
  163. */
  164. _createFooterConstructor(options) {
  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(options),
  169. this._renderCancelButton(options)
  170. ].filter(Boolean);
  171. return function Footer(modalFooterProps) {
  172. return (
  173. <ModalFooter showKeyline = { modalFooterProps.showKeyline } >
  174. {
  175. /**
  176. * Atlaskit has this empty span (JustifySim) so...
  177. */
  178. }
  179. <span />
  180. <ButtonGroup>
  181. { buttons }
  182. </ButtonGroup>
  183. </ModalFooter>
  184. );
  185. };
  186. }
  187. _onCancel: () => void;
  188. /**
  189. * Dispatches action to hide the dialog.
  190. *
  191. * @returns {void}
  192. */
  193. _onCancel() {
  194. if (!this.props.isModal) {
  195. const { onCancel } = this.props;
  196. onCancel && onCancel();
  197. }
  198. }
  199. _onDialogDismissed: () => void;
  200. /**
  201. * Handles click on the blanket area.
  202. *
  203. * @returns {void}
  204. */
  205. _onDialogDismissed() {
  206. if (!this.props.disableBlanketClickDismiss) {
  207. this._onCancel();
  208. }
  209. }
  210. _onSubmit: (?string) => void;
  211. /**
  212. * Dispatches the action when submitting the dialog.
  213. *
  214. * @private
  215. * @param {string} value - The submitted value if any.
  216. * @returns {void}
  217. */
  218. _onSubmit(value) {
  219. const { onSubmit } = this.props;
  220. onSubmit && onSubmit(value);
  221. }
  222. /**
  223. * Renders Cancel button.
  224. *
  225. * @param {Object} options - The configuration for the Cancel button.
  226. * @param {boolean} options.cancelDisabled - True if the cancel button
  227. * should not be rendered.
  228. * @param {string} options.cancelTitleKey - The translation key to use as
  229. * text on the button.
  230. * @param {boolean} options.isModal - True if the cancel button should not
  231. * be rendered.
  232. * @private
  233. * @returns {ReactElement|null} The Cancel button if enabled and dialog is
  234. * not modal.
  235. */
  236. _renderCancelButton(options = {}) {
  237. if (options.cancelDisabled
  238. || options.isModal
  239. || options.hideCancelButton) {
  240. return null;
  241. }
  242. const {
  243. t /* The following fixes a flow error: */ = _.identity
  244. } = this.props;
  245. return (
  246. <Button
  247. appearance = 'subtle'
  248. id = { CANCEL_BUTTON_ID }
  249. key = 'cancel'
  250. onClick = { this._onCancel }
  251. type = 'button'>
  252. { t(options.cancelTitleKey || 'dialog.Cancel') }
  253. </Button>
  254. );
  255. }
  256. /**
  257. * Renders OK button.
  258. *
  259. * @param {Object} options - The configuration for the OK button.
  260. * @param {boolean} options.okDisabled - True if the button should display
  261. * as disabled and clicking should have no effect.
  262. * @param {string} options.okTitleKey - The translation key to use as text
  263. * on the button.
  264. * @param {boolean} options.submitDisabled - True if the button should not
  265. * be rendered.
  266. * @private
  267. * @returns {ReactElement|null} The OK button if enabled.
  268. */
  269. _renderOKButton(options = {}) {
  270. if (options.submitDisabled) {
  271. return null;
  272. }
  273. const {
  274. t /* The following fixes a flow error: */ = _.identity
  275. } = this.props;
  276. return (
  277. <Button
  278. appearance = 'primary'
  279. form = 'modal-dialog-form'
  280. id = { OK_BUTTON_ID }
  281. isDisabled = { options.okDisabled }
  282. key = 'submit'
  283. onClick = { this._onSubmit }
  284. type = 'button'>
  285. { t(options.okTitleKey || 'dialog.Ok') }
  286. </Button>
  287. );
  288. }
  289. _setDialogElement: (?HTMLElement) => void;
  290. /**
  291. * Sets the instance variable for the div containing the component's dialog
  292. * element so it can be accessed directly.
  293. *
  294. * @param {HTMLElement} element - The DOM element for the component's
  295. * dialog.
  296. * @private
  297. * @returns {void}
  298. */
  299. _setDialogElement(element: ?HTMLElement) {
  300. this._dialogElement = element;
  301. }
  302. _onKeyDown: (Object) => void;
  303. /**
  304. * Handles 'Enter' key in the dialog to submit/hide dialog depending on
  305. * the available buttons and their disabled state.
  306. *
  307. * @param {Object} event - The key event.
  308. * @private
  309. * @returns {void}
  310. */
  311. _onKeyDown(event) {
  312. // If the event coming to the dialog has been subject to preventDefault
  313. // we don't handle it here.
  314. if (event.defaultPrevented) {
  315. return;
  316. }
  317. if (event.key === 'Enter') {
  318. event.preventDefault();
  319. event.stopPropagation();
  320. if (this.props.submitDisabled && !this.props.cancelDisabled) {
  321. this._onCancel();
  322. } else if (!this.props.okDisabled) {
  323. this._onSubmit();
  324. }
  325. }
  326. }
  327. }
  328. export default translate(StatelessDialog);