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

StatelessDialog.web.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import AKButton from '@atlaskit/button';
  2. import AKButtonGroup from '@atlaskit/button-group';
  3. import ModalDialog from '@atlaskit/modal-dialog';
  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. <div
  103. onKeyDown = { this._onKeyDown }
  104. ref = { this._setDialogElement }>
  105. <ModalDialog
  106. footer = { this._renderFooter() }
  107. header = { this._renderHeader() }
  108. isOpen = { true }
  109. onDialogDismissed = { this._onDialogDismissed }
  110. width = { this.props.width || 'medium' }>
  111. <div>
  112. <form
  113. className = 'modal-dialog-form'
  114. id = 'modal-dialog-form'
  115. onSubmit = { this._onSubmit }>
  116. { this.props.children }
  117. </form>
  118. </div>
  119. </ModalDialog>
  120. </div>
  121. );
  122. }
  123. /**
  124. * Dispatches action to hide the dialog.
  125. *
  126. * @returns {void}
  127. */
  128. _onCancel() {
  129. if (!this.props.isModal) {
  130. this.props.onCancel();
  131. }
  132. }
  133. /**
  134. * Handles click on the blanket area.
  135. *
  136. * @returns {void}
  137. */
  138. _onDialogDismissed() {
  139. if (!this.props.disableBlanketClickDismiss) {
  140. this._onCancel();
  141. }
  142. }
  143. /**
  144. * Dispatches the action when submitting the dialog.
  145. *
  146. * @private
  147. * @param {string} value - The submitted value if any.
  148. * @returns {void}
  149. */
  150. _onSubmit(value) {
  151. this.props.onSubmit(value);
  152. }
  153. /**
  154. * Renders Cancel button.
  155. *
  156. * @private
  157. * @returns {*} The Cancel button if enabled and dialog is not modal.
  158. */
  159. _renderCancelButton() {
  160. if (this.props.cancelDisabled || this.props.isModal) {
  161. return null;
  162. }
  163. return (
  164. <AKButton
  165. appearance = 'subtle'
  166. id = { CANCEL_BUTTON_ID }
  167. onClick = { this._onCancel }>
  168. { this.props.t(this.props.cancelTitleKey || 'dialog.Cancel') }
  169. </AKButton>
  170. );
  171. }
  172. /**
  173. * Renders component in dialog footer.
  174. *
  175. * @private
  176. * @returns {ReactElement}
  177. */
  178. _renderFooter() {
  179. return (
  180. <footer className = 'modal-dialog-footer'>
  181. <AKButtonGroup>
  182. { this._renderCancelButton() }
  183. { this._renderOKButton() }
  184. </AKButtonGroup>
  185. </footer>
  186. );
  187. }
  188. /**
  189. * Renders component in dialog header.
  190. *
  191. * @private
  192. * @returns {ReactElement}
  193. */
  194. _renderHeader() {
  195. const { t } = this.props;
  196. return (
  197. <header>
  198. <h3>
  199. { this.props.titleString || t(this.props.titleKey) }
  200. </h3>
  201. </header>
  202. );
  203. }
  204. /**
  205. * Renders OK button.
  206. *
  207. * @private
  208. * @returns {*} The OK button if enabled.
  209. */
  210. _renderOKButton() {
  211. if (this.props.submitDisabled) {
  212. return null;
  213. }
  214. return (
  215. <AKButton
  216. appearance = 'primary'
  217. form = 'modal-dialog-form'
  218. id = { OK_BUTTON_ID }
  219. isDisabled = { this.props.okDisabled }
  220. onClick = { this._onSubmit }>
  221. { this.props.t(this.props.okTitleKey || 'dialog.Ok') }
  222. </AKButton>
  223. );
  224. }
  225. /**
  226. * Sets the instance variable for the div containing the component's dialog
  227. * element so it can be accessed directly.
  228. *
  229. * @param {Object} element - The DOM element for the component's dialog.
  230. * @private
  231. * @returns {void}
  232. */
  233. _setDialogElement(element) {
  234. this._dialogElement = element;
  235. }
  236. /**
  237. * Handles 'Enter' key in the dialog to submit/hide dialog depending on
  238. * the available buttons and their disabled state.
  239. *
  240. * @param {Object} event - The key event.
  241. * @private
  242. * @returns {void}
  243. */
  244. _onKeyDown(event) {
  245. // If the event coming to the dialog has been subject to preventDefault
  246. // we don't handle it here.
  247. if (event.defaultPrevented) {
  248. return;
  249. }
  250. if (event.key === 'Enter') {
  251. event.preventDefault();
  252. event.stopPropagation();
  253. if (this.props.submitDisabled && !this.props.cancelDisabled) {
  254. this._onCancel();
  255. } else if (!this.props.okDisabled) {
  256. this._onSubmit();
  257. }
  258. }
  259. }
  260. /**
  261. * Updates focused button, if we have a reference to the dialog element.
  262. * Focus on available button if there is no focus already.
  263. *
  264. * @private
  265. * @returns {void}
  266. */
  267. _updateButtonFocus() {
  268. if (this._dialogElement) {
  269. // if we have a focused element inside the dialog, skip changing
  270. // the focus
  271. if (this._dialogElement.contains(document.activeElement)) {
  272. return;
  273. }
  274. let buttonToFocus;
  275. if (this.props.submitDisabled) {
  276. buttonToFocus = this._dialogElement
  277. .querySelector(`[id=${CANCEL_BUTTON_ID}]`);
  278. } else if (!this.props.okDisabled) {
  279. buttonToFocus = this._dialogElement
  280. .querySelector(`[id=${OK_BUTTON_ID}]`);
  281. }
  282. if (buttonToFocus) {
  283. buttonToFocus.focus();
  284. }
  285. }
  286. }
  287. }
  288. export default translate(StatelessDialog);