您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StatelessDialog.js 8.9KB

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