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

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