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

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