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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // @flow
  2. import Button, { ButtonGroup } from '@atlaskit/button';
  3. import ModalDialog from '@atlaskit/modal-dialog';
  4. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  5. import _ from 'lodash';
  6. import React, { Component } from 'react';
  7. import { translate } from '../../i18n';
  8. import type { DialogProps } from '../constants';
  9. /**
  10. * The ID to be used for the cancel button if enabled.
  11. * @type {string}
  12. */
  13. const CANCEL_BUTTON_ID = 'modal-dialog-cancel-button';
  14. /**
  15. * The ID to be used for the ok button if enabled.
  16. * @type {string}
  17. */
  18. const OK_BUTTON_ID = 'modal-dialog-ok-button';
  19. /**
  20. * The type of the React {@code Component} props of {@link StatelessDialog}.
  21. *
  22. * @static
  23. */
  24. type Props = {
  25. ...DialogProps,
  26. /**
  27. * Disables dismissing the dialog when the blanket is clicked. Enabled
  28. * by default.
  29. */
  30. disableBlanketClickDismiss: boolean,
  31. /**
  32. * Whether the dialog is modal. This means clicking on the blanket will
  33. * leave the dialog open. No cancel button.
  34. */
  35. isModal: boolean,
  36. /**
  37. * Disables rendering of the submit button.
  38. */
  39. submitDisabled: boolean,
  40. /**
  41. * Width of the dialog, can be:
  42. * - 'small' (400px), 'medium' (600px), 'large' (800px),
  43. * 'x-large' (968px)
  44. * - integer value for pixel width
  45. * - string value for percentage
  46. */
  47. width: string
  48. };
  49. /**
  50. * Web dialog that uses atlaskit modal-dialog to display dialogs.
  51. */
  52. class StatelessDialog extends Component<Props> {
  53. _dialogElement: ?HTMLElement;
  54. /**
  55. * Initializes a new {@code StatelessDialog} instance.
  56. *
  57. * @param {Object} props - The read-only properties with which the new
  58. * instance is to be initialized.
  59. */
  60. constructor(props) {
  61. super(props);
  62. // Bind event handlers so they are only bound once for every instance.
  63. this._onCancel = this._onCancel.bind(this);
  64. this._onDialogDismissed = this._onDialogDismissed.bind(this);
  65. this._onKeyDown = this._onKeyDown.bind(this);
  66. this._onSubmit = this._onSubmit.bind(this);
  67. this._setDialogElement = this._setDialogElement.bind(this);
  68. }
  69. /**
  70. * React Component method that executes once component is mounted.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. this._updateButtonFocus();
  76. }
  77. /**
  78. * React Component method that executes once component is updated.
  79. *
  80. * @param {Object} prevProps - The previous properties, before the update.
  81. * @returns {void}
  82. */
  83. componentDidUpdate(prevProps) {
  84. // if there is an update in any of the buttons enable/disable props
  85. // update the focus if needed
  86. if (prevProps.okDisabled !== this.props.okDisabled
  87. || prevProps.cancelDisabled !== this.props.cancelDisabled
  88. || prevProps.submitDisabled !== this.props.submitDisabled) {
  89. this._updateButtonFocus();
  90. }
  91. }
  92. /**
  93. * Implements React's {@link Component#render()}.
  94. *
  95. * @inheritdoc
  96. * @returns {ReactElement}
  97. */
  98. render() {
  99. return (
  100. /**
  101. * Enabled light theme for dialogs until all in-dialog components
  102. * support dark theme.
  103. */
  104. <AtlasKitThemeProvider mode = 'light'>
  105. <div
  106. onKeyDown = { this._onKeyDown }
  107. ref = { this._setDialogElement }>
  108. <ModalDialog
  109. footer = { this._renderFooter() }
  110. header = { this._renderHeader() }
  111. isOpen = { true }
  112. onDialogDismissed = { this._onDialogDismissed }
  113. width = { this.props.width || 'medium' }>
  114. <div>
  115. <form
  116. className = 'modal-dialog-form'
  117. id = 'modal-dialog-form'
  118. onSubmit = { this._onSubmit }>
  119. { this.props.children }
  120. </form>
  121. </div>
  122. </ModalDialog>
  123. </div>
  124. </AtlasKitThemeProvider>
  125. );
  126. }
  127. _onCancel: () => void;
  128. /**
  129. * Dispatches action to hide the dialog.
  130. *
  131. * @returns {void}
  132. */
  133. _onCancel() {
  134. if (!this.props.isModal) {
  135. const { onCancel } = this.props;
  136. onCancel && onCancel();
  137. }
  138. }
  139. _onDialogDismissed: () => void;
  140. /**
  141. * Handles click on the blanket area.
  142. *
  143. * @returns {void}
  144. */
  145. _onDialogDismissed() {
  146. if (!this.props.disableBlanketClickDismiss) {
  147. this._onCancel();
  148. }
  149. }
  150. _onSubmit: (?string) => void;
  151. /**
  152. * Dispatches the action when submitting the dialog.
  153. *
  154. * @private
  155. * @param {string} value - The submitted value if any.
  156. * @returns {void}
  157. */
  158. _onSubmit(value) {
  159. const { onSubmit } = this.props;
  160. onSubmit && onSubmit(value);
  161. }
  162. /**
  163. * Renders Cancel button.
  164. *
  165. * @private
  166. * @returns {*} The Cancel button if enabled and dialog is not modal.
  167. */
  168. _renderCancelButton() {
  169. if (this.props.cancelDisabled || this.props.isModal) {
  170. return null;
  171. }
  172. const {
  173. t /* The following fixes a flow error: */ = _.identity
  174. } = this.props;
  175. return (
  176. <Button
  177. appearance = 'subtle'
  178. id = { CANCEL_BUTTON_ID }
  179. key = 'cancel'
  180. onClick = { this._onCancel }
  181. type = 'button'>
  182. { t(this.props.cancelTitleKey || 'dialog.Cancel') }
  183. </Button>
  184. );
  185. }
  186. /**
  187. * Renders component in dialog footer.
  188. *
  189. * @private
  190. * @returns {ReactElement}
  191. */
  192. _renderFooter() {
  193. // Filter out falsy (null) values because {@code ButtonGroup} will error
  194. // if passed in anything but buttons with valid type props.
  195. const buttons = [
  196. this._renderCancelButton(),
  197. this._renderOKButton()
  198. ].filter(Boolean);
  199. return (
  200. <footer className = 'modal-dialog-footer'>
  201. <ButtonGroup>
  202. { buttons }
  203. </ButtonGroup>
  204. </footer>
  205. );
  206. }
  207. /**
  208. * Renders component in dialog header.
  209. *
  210. * @private
  211. * @returns {ReactElement}
  212. */
  213. _renderHeader() {
  214. const {
  215. t /* The following fixes a flow error: */ = _.identity
  216. } = this.props;
  217. return (
  218. <header>
  219. <h3>
  220. { this.props.titleString || t(this.props.titleKey) }
  221. </h3>
  222. </header>
  223. );
  224. }
  225. /**
  226. * Renders OK button.
  227. *
  228. * @private
  229. * @returns {*} The OK button if enabled.
  230. */
  231. _renderOKButton() {
  232. if (this.props.submitDisabled) {
  233. return null;
  234. }
  235. const {
  236. t /* The following fixes a flow error: */ = _.identity
  237. } = this.props;
  238. return (
  239. <Button
  240. appearance = 'primary'
  241. form = 'modal-dialog-form'
  242. id = { OK_BUTTON_ID }
  243. isDisabled = { this.props.okDisabled }
  244. key = 'submit'
  245. onClick = { this._onSubmit }
  246. type = 'button'>
  247. { t(this.props.okTitleKey || 'dialog.Ok') }
  248. </Button>
  249. );
  250. }
  251. _setDialogElement: (?HTMLElement) => void;
  252. /**
  253. * Sets the instance variable for the div containing the component's dialog
  254. * element so it can be accessed directly.
  255. *
  256. * @param {HTMLElement} element - The DOM element for the component's
  257. * dialog.
  258. * @private
  259. * @returns {void}
  260. */
  261. _setDialogElement(element: ?HTMLElement) {
  262. this._dialogElement = element;
  263. }
  264. _onKeyDown: (Object) => void;
  265. /**
  266. * Handles 'Enter' key in the dialog to submit/hide dialog depending on
  267. * the available buttons and their disabled state.
  268. *
  269. * @param {Object} event - The key event.
  270. * @private
  271. * @returns {void}
  272. */
  273. _onKeyDown(event) {
  274. // If the event coming to the dialog has been subject to preventDefault
  275. // we don't handle it here.
  276. if (event.defaultPrevented) {
  277. return;
  278. }
  279. if (event.key === 'Enter') {
  280. event.preventDefault();
  281. event.stopPropagation();
  282. if (this.props.submitDisabled && !this.props.cancelDisabled) {
  283. this._onCancel();
  284. } else if (!this.props.okDisabled) {
  285. this._onSubmit();
  286. }
  287. }
  288. }
  289. /**
  290. * Updates focused button, if we have a reference to the dialog element.
  291. * Focus on available button if there is no focus already.
  292. *
  293. * @private
  294. * @returns {void}
  295. */
  296. _updateButtonFocus() {
  297. const dialogElement = this._dialogElement;
  298. if (dialogElement) {
  299. // if we have a focused element inside the dialog, skip changing
  300. // the focus
  301. if (dialogElement.contains(document.activeElement)) {
  302. return;
  303. }
  304. let buttonToFocus;
  305. if (this.props.submitDisabled) {
  306. buttonToFocus
  307. = dialogElement.querySelector(`[id=${CANCEL_BUTTON_ID}]`);
  308. } else if (!this.props.okDisabled) {
  309. buttonToFocus
  310. = dialogElement.querySelector(`[id=${OK_BUTTON_ID}]`);
  311. }
  312. if (buttonToFocus) {
  313. buttonToFocus.focus();
  314. }
  315. }
  316. }
  317. }
  318. export default translate(StatelessDialog);