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

StatelessDialog.web.js 9.3KB

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