Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StatelessDialog.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 { withStyles } from '@material-ui/core/styles';
  6. import _ from 'lodash';
  7. import React, { Component } from 'react';
  8. import { translate } from '../../../i18n/functions';
  9. import type { DialogProps } from '../../constants';
  10. import ModalHeader from './ModalHeader';
  11. /**
  12. * The ID to be used for the cancel button if enabled.
  13. *
  14. * @type {string}
  15. */
  16. const CANCEL_BUTTON_ID = 'modal-dialog-cancel-button';
  17. /**
  18. * The ID to be used for the ok button if enabled.
  19. *
  20. * @type {string}
  21. */
  22. const OK_BUTTON_ID = 'modal-dialog-ok-button';
  23. /**
  24. * The type of the React {@code Component} props of {@link StatelessDialog}.
  25. *
  26. * @static
  27. */
  28. type Props = DialogProps & {
  29. /**
  30. * An object containing the CSS classes.
  31. */
  32. classes: Object,
  33. /**
  34. * Custom dialog header that replaces the standard heading.
  35. */
  36. customHeader?: React$Element<any> | Function,
  37. /*
  38. * True if listening for the Enter key should be disabled.
  39. */
  40. disableEnter: boolean,
  41. /**
  42. * Disables dismissing the dialog when the blanket is clicked. Enabled
  43. * by default.
  44. */
  45. disableBlanketClickDismiss: boolean,
  46. /**
  47. * If true, the cancel button will not display but cancel actions, like
  48. * clicking the blanket, will cancel.
  49. */
  50. hideCancelButton: boolean,
  51. /**
  52. * If true, the close icon button will not be displayed.
  53. */
  54. hideCloseIconButton: boolean,
  55. /**
  56. * If true, no footer will be displayed.
  57. */
  58. disableFooter?: boolean,
  59. i18n: Object,
  60. /**
  61. * Whether the dialog is modal. This means clicking on the blanket will
  62. * leave the dialog open. No cancel button.
  63. */
  64. isModal: boolean,
  65. /**
  66. * The handler for the event when clicking the 'confirmNo' button.
  67. * Defaults to onCancel if absent.
  68. */
  69. onDecline?: Function,
  70. /**
  71. * Callback invoked when setting the ref of the Dialog.
  72. */
  73. onDialogRef?: Function,
  74. /**
  75. * Disables rendering of the submit button.
  76. */
  77. submitDisabled: boolean,
  78. /**
  79. * Function to be used to retrieve translated i18n labels.
  80. */
  81. t: Function,
  82. /**
  83. * Width of the dialog, can be:
  84. * - 'small' (400px), 'medium' (600px), 'large' (800px),
  85. * 'x-large' (968px)
  86. * - integer value for pixel width
  87. * - string value for percentage.
  88. */
  89. width: string
  90. };
  91. /**
  92. * Creates the styles for the component.
  93. *
  94. * @returns {Object}
  95. */
  96. const styles = () => {
  97. return {
  98. footer: {
  99. boxShadow: 'none'
  100. }
  101. };
  102. };
  103. /**
  104. * Web dialog that uses atlaskit modal-dialog to display dialogs.
  105. */
  106. class StatelessDialog extends Component<Props> {
  107. static defaultProps = {
  108. hideCloseIconButton: false
  109. };
  110. /**
  111. * The functional component to be used for rendering the modal footer.
  112. */
  113. _Footer: ?Function;
  114. _dialogElement: ?HTMLElement;
  115. /**
  116. * Initializes a new {@code StatelessDialog} instance.
  117. *
  118. * @param {Object} props - The read-only properties with which the new
  119. * instance is to be initialized.
  120. */
  121. constructor(props) {
  122. super(props);
  123. // Bind event handlers so they are only bound once for every instance.
  124. this._onCancel = this._onCancel.bind(this);
  125. this._onDialogDismissed = this._onDialogDismissed.bind(this);
  126. this._onKeyPress = this._onKeyPress.bind(this);
  127. this._onSubmit = this._onSubmit.bind(this);
  128. this._renderFooter = this._renderFooter.bind(this);
  129. this._onDialogRef = this._onDialogRef.bind(this);
  130. }
  131. /**
  132. * Implements React's {@link Component#render()}.
  133. *
  134. * @inheritdoc
  135. * @returns {ReactElement}
  136. */
  137. render() {
  138. const {
  139. customHeader,
  140. children,
  141. hideCloseIconButton,
  142. t /* The following fixes a flow error: */ = _.identity,
  143. titleString,
  144. titleKey,
  145. width
  146. } = this.props;
  147. return (
  148. <Modal
  149. autoFocus = { true }
  150. components = {{
  151. Header: customHeader ? customHeader : props => (
  152. <ModalHeader
  153. { ...props }
  154. heading = { titleString || t(titleKey) }
  155. hideCloseIconButton = { hideCloseIconButton } />
  156. )
  157. }}
  158. footer = { this._renderFooter }
  159. i18n = { this.props.i18n }
  160. onClose = { this._onDialogDismissed }
  161. onDialogDismissed = { this._onDialogDismissed }
  162. shouldCloseOnEscapePress = { true }
  163. width = { width || 'medium' }>
  164. <div
  165. onKeyPress = { this._onKeyPress }
  166. ref = { this._onDialogRef }>
  167. <form
  168. className = 'modal-dialog-form'
  169. id = 'modal-dialog-form'
  170. onSubmit = { this._onSubmit }>
  171. { children }
  172. </form>
  173. </div>
  174. </Modal>
  175. );
  176. }
  177. _renderFooter: () => React$Node;
  178. /**
  179. * Returns a ReactElement to display buttons for closing the modal.
  180. *
  181. * @param {Object} propsFromModalFooter - The props passed in from the
  182. * {@link ModalFooter} component.
  183. * @private
  184. * @returns {ReactElement}
  185. */
  186. _renderFooter(propsFromModalFooter) {
  187. // Filter out falsy (null) values because {@code ButtonGroup} will error
  188. // if passed in anything but buttons with valid type props.
  189. const buttons = [
  190. this._renderOKButton(),
  191. this._renderCancelButton()
  192. ].filter(Boolean);
  193. if (this.props.disableFooter) {
  194. return null;
  195. }
  196. return (
  197. <ModalFooter
  198. className = { this.props.classes.footer }
  199. showKeyline = { propsFromModalFooter.showKeyline } >
  200. {
  201. /**
  202. * Atlaskit has this empty span (JustifySim) so...
  203. */
  204. }
  205. <span />
  206. <ButtonGroup>
  207. { buttons }
  208. </ButtonGroup>
  209. </ModalFooter>
  210. );
  211. }
  212. _onCancel: () => void;
  213. /**
  214. * Dispatches action to hide the dialog.
  215. *
  216. * @returns {void}
  217. */
  218. _onCancel() {
  219. if (!this.props.isModal) {
  220. const { onCancel } = this.props;
  221. onCancel && onCancel();
  222. }
  223. }
  224. _onDialogDismissed: () => void;
  225. /**
  226. * Handles click on the blanket area.
  227. *
  228. * @returns {void}
  229. */
  230. _onDialogDismissed() {
  231. if (!this.props.disableBlanketClickDismiss) {
  232. this._onCancel();
  233. }
  234. }
  235. _onSubmit: (?string) => void;
  236. /**
  237. * Dispatches the action when submitting the dialog.
  238. *
  239. * @private
  240. * @param {string} value - The submitted value if any.
  241. * @returns {void}
  242. */
  243. _onSubmit(value) {
  244. const { onSubmit } = this.props;
  245. onSubmit && onSubmit(value);
  246. }
  247. /**
  248. * Renders Cancel button.
  249. *
  250. * @private
  251. * @returns {ReactElement|null} The Cancel button if enabled and dialog is
  252. * not modal.
  253. */
  254. _renderCancelButton() {
  255. if (this.props.cancelDisabled
  256. || this.props.isModal
  257. || this.props.hideCancelButton) {
  258. return null;
  259. }
  260. const {
  261. t /* The following fixes a flow error: */ = _.identity,
  262. onDecline
  263. } = this.props;
  264. return (
  265. <Button
  266. appearance = 'subtle'
  267. id = { CANCEL_BUTTON_ID }
  268. key = 'cancel'
  269. onClick = { onDecline || this._onCancel }
  270. type = 'button'>
  271. { t(this.props.cancelKey || 'dialog.Cancel') }
  272. </Button>
  273. );
  274. }
  275. /**
  276. * Renders OK button.
  277. *
  278. * @private
  279. * @returns {ReactElement|null} The OK button if enabled.
  280. */
  281. _renderOKButton() {
  282. if (this.props.submitDisabled) {
  283. return null;
  284. }
  285. const {
  286. t /* The following fixes a flow error: */ = _.identity
  287. } = this.props;
  288. return (
  289. <Button
  290. appearance = 'primary'
  291. form = 'modal-dialog-form'
  292. id = { OK_BUTTON_ID }
  293. isDisabled = { this.props.okDisabled }
  294. key = 'submit'
  295. onClick = { this._onSubmit }
  296. type = 'button'>
  297. { t(this.props.okKey || 'dialog.Ok') }
  298. </Button>
  299. );
  300. }
  301. _onDialogRef: (?Element) => void;
  302. /**
  303. * Callback invoked when setting the ref of the dialog's child passing the Modal ref.
  304. * It is done this way because we cannot directly access the ref of the Modal component.
  305. *
  306. * @param {HTMLElement} element - The DOM element for the dialog.
  307. * @private
  308. * @returns {void}
  309. */
  310. _onDialogRef(element: ?Element) {
  311. this.props.onDialogRef && this.props.onDialogRef(element && element.parentNode);
  312. }
  313. _onKeyPress: (Object) => void;
  314. /**
  315. * Handles 'Enter' key in the dialog to submit/hide dialog depending on
  316. * the available buttons and their disabled state.
  317. *
  318. * @param {Object} event - The key event.
  319. * @private
  320. * @returns {void}
  321. */
  322. _onKeyPress(event) {
  323. // If the event coming to the dialog has been subject to preventDefault
  324. // we don't handle it here.
  325. if (event.defaultPrevented) {
  326. return;
  327. }
  328. if (event.key === 'Enter' && !this.props.disableEnter) {
  329. event.preventDefault();
  330. event.stopPropagation();
  331. if (this.props.submitDisabled && !this.props.cancelDisabled) {
  332. this._onCancel();
  333. } else if (!this.props.okDisabled) {
  334. this._onSubmit();
  335. }
  336. }
  337. }
  338. }
  339. export default translate(withStyles(styles)(StatelessDialog));