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.web.js 8.6KB

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