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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * Web dialog that uses atlaskit modal-dialog to display dialogs.
  9. */
  10. class StatelessDialog extends Component {
  11. /**
  12. * {@code StatelessDialog} component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. ...DIALOG_PROP_TYPES,
  18. /**
  19. * This is the body of the dialog, the component children.
  20. */
  21. children: React.PropTypes.node,
  22. /**
  23. * Disables dismissing the dialog when the blanket is clicked. Enabled
  24. * by default.
  25. */
  26. disableBlanketClickDismiss: React.PropTypes.bool,
  27. /**
  28. * Whether the dialog is modal. This means clicking on the blanket will
  29. * leave the dialog open. No cancel button.
  30. */
  31. isModal: React.PropTypes.bool,
  32. /**
  33. * Disables rendering of the submit button.
  34. */
  35. submitDisabled: React.PropTypes.bool,
  36. /**
  37. * Width of the dialog, can be:
  38. * - 'small' (400px), 'medium' (600px), 'large' (800px),
  39. * 'x-large' (968px)
  40. * - integer value for pixel width
  41. * - string value for percentage
  42. */
  43. width: React.PropTypes.string
  44. };
  45. /**
  46. * Initializes a new {@code StatelessDialog} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props) {
  52. super(props);
  53. // Bind event handlers so they are only bound once for every instance.
  54. this._onCancel = this._onCancel.bind(this);
  55. this._onDialogDismissed = this._onDialogDismissed.bind(this);
  56. this._onSubmit = this._onSubmit.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. return (
  66. <ModalDialog
  67. footer = { this._renderFooter() }
  68. header = { this._renderHeader() }
  69. isOpen = { true }
  70. onDialogDismissed = { this._onDialogDismissed }
  71. width = { this.props.width || 'medium' }>
  72. <div>
  73. <form
  74. className = 'modal-dialog-form'
  75. id = 'modal-dialog-form'
  76. onSubmit = { this._onSubmit }>
  77. { this.props.children }
  78. </form>
  79. </div>
  80. </ModalDialog>
  81. );
  82. }
  83. /**
  84. * Dispatches action to hide the dialog.
  85. *
  86. * @returns {void}
  87. */
  88. _onCancel() {
  89. if (!this.props.isModal) {
  90. this.props.onCancel();
  91. }
  92. }
  93. /**
  94. * Handles click on the blanket area.
  95. *
  96. * @returns {void}
  97. */
  98. _onDialogDismissed() {
  99. if (!this.props.disableBlanketClickDismiss) {
  100. this._onCancel();
  101. }
  102. }
  103. /**
  104. * Dispatches the action when submitting the dialog.
  105. *
  106. * @private
  107. * @param {string} value - The submitted value if any.
  108. * @returns {void}
  109. */
  110. _onSubmit(value) {
  111. this.props.onSubmit(value);
  112. }
  113. /**
  114. * Renders Cancel button.
  115. *
  116. * @private
  117. * @returns {*} The Cancel button if enabled and dialog is not modal.
  118. */
  119. _renderCancelButton() {
  120. if (this.props.cancelDisabled || this.props.isModal) {
  121. return null;
  122. }
  123. return (
  124. <AKButton
  125. appearance = 'subtle'
  126. id = 'modal-dialog-cancel-button'
  127. onClick = { this._onCancel }>
  128. { this.props.t(this.props.cancelTitleKey || 'dialog.Cancel') }
  129. </AKButton>
  130. );
  131. }
  132. /**
  133. * Renders component in dialog footer.
  134. *
  135. * @private
  136. * @returns {ReactElement}
  137. */
  138. _renderFooter() {
  139. return (
  140. <footer className = 'modal-dialog-footer'>
  141. <AKButtonGroup>
  142. { this._renderCancelButton() }
  143. { this._renderOKButton() }
  144. </AKButtonGroup>
  145. </footer>
  146. );
  147. }
  148. /**
  149. * Renders component in dialog header.
  150. *
  151. * @private
  152. * @returns {ReactElement}
  153. */
  154. _renderHeader() {
  155. const { t } = this.props;
  156. return (
  157. <header>
  158. <h2>
  159. { this.props.titleString || t(this.props.titleKey) }
  160. </h2>
  161. </header>
  162. );
  163. }
  164. /**
  165. * Renders OK button.
  166. *
  167. * @private
  168. * @returns {*} The OK button if enabled.
  169. */
  170. _renderOKButton() {
  171. if (this.props.submitDisabled) {
  172. return null;
  173. }
  174. return (
  175. <AKButton
  176. appearance = 'primary'
  177. form = 'modal-dialog-form'
  178. id = 'modal-dialog-ok-button'
  179. isDisabled = { this.props.okDisabled }
  180. onClick = { this._onSubmit }>
  181. { this.props.t(this.props.okTitleKey || 'dialog.Ok') }
  182. </AKButton>
  183. );
  184. }
  185. }
  186. export default translate(StatelessDialog);