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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 { dialogPropTypes } from '../constants';
  7. /**
  8. * Web dialog that uses atlaskit modal-dialog to display dialogs.
  9. */
  10. class StatelessDialog extends Component {
  11. /**
  12. * Web dialog component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. ...dialogPropTypes,
  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 Dialog 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. this._onCancel = this._onCancel.bind(this);
  54. this._onDialogDismissed = this._onDialogDismissed.bind(this);
  55. this._onSubmit = this._onSubmit.bind(this);
  56. }
  57. /**
  58. * Implements React's {@link Component#render()}.
  59. *
  60. * @inheritdoc
  61. * @returns {ReactElement}
  62. */
  63. render() {
  64. return (
  65. <ModalDialog
  66. footer = { this._renderFooter() }
  67. header = { this._renderHeader() }
  68. isOpen = { true }
  69. onDialogDismissed = { this._onDialogDismissed }
  70. width = { this.props.width || 'medium' }>
  71. <div>
  72. <form
  73. className = 'modal-dialog-form'
  74. id = 'modal-dialog-form'
  75. onSubmit = { this._onSubmit }>
  76. { this.props.children }
  77. </form>
  78. </div>
  79. </ModalDialog>);
  80. }
  81. /**
  82. * Handles click on the blanket area.
  83. *
  84. * @returns {void}
  85. */
  86. _onDialogDismissed() {
  87. if (!this.props.disableBlanketClickDismiss) {
  88. this._onCancel();
  89. }
  90. }
  91. /**
  92. * Render cancel button.
  93. *
  94. * @returns {*} The cancel button if enabled and dialog is not modal.
  95. * @private
  96. */
  97. _renderCancelButton() {
  98. if (this.props.cancelDisabled || this.props.isModal) {
  99. return null;
  100. }
  101. return (
  102. <AKButton
  103. appearance = 'subtle'
  104. id = 'modal-dialog-cancel-button'
  105. onClick = { this._onCancel }>
  106. { this.props.t(this.props.cancelTitleKey || 'dialog.Cancel') }
  107. </AKButton>
  108. );
  109. }
  110. /**
  111. * Render component in dialog footer.
  112. *
  113. * @returns {ReactElement}
  114. * @private
  115. */
  116. _renderFooter() {
  117. return (
  118. <footer className = 'modal-dialog-footer'>
  119. <AKButtonGroup>
  120. { this._renderCancelButton() }
  121. { this._renderOKButton() }
  122. </AKButtonGroup>
  123. </footer>
  124. );
  125. }
  126. /**
  127. * Render component in dialog header.
  128. *
  129. * @returns {ReactElement}
  130. * @private
  131. */
  132. _renderHeader() {
  133. const { t } = this.props;
  134. return (
  135. <header>
  136. <h2>
  137. { this.props.titleString || t(this.props.titleKey) }
  138. </h2>
  139. </header>
  140. );
  141. }
  142. /**
  143. * Render ok button.
  144. *
  145. * @returns {*} The ok button if enabled.
  146. * @private
  147. */
  148. _renderOKButton() {
  149. if (this.props.submitDisabled) {
  150. return null;
  151. }
  152. return (
  153. <AKButton
  154. appearance = 'primary'
  155. form = 'modal-dialog-form'
  156. id = 'modal-dialog-ok-button'
  157. isDisabled = { this.props.okDisabled }
  158. onClick = { this._onSubmit }>
  159. { this.props.t(this.props.okTitleKey || 'dialog.Ok') }
  160. </AKButton>
  161. );
  162. }
  163. /**
  164. * Dispatches action to hide the dialog.
  165. *
  166. * @returns {void}
  167. */
  168. _onCancel() {
  169. if (this.props.isModal) {
  170. return;
  171. }
  172. this.props.onCancel();
  173. }
  174. /**
  175. * Dispatches the action when submitting the dialog.
  176. *
  177. * @private
  178. * @param {string} value - The submitted value if any.
  179. * @returns {void}
  180. */
  181. _onSubmit(value) {
  182. this.props.onSubmit(value);
  183. }
  184. }
  185. export default translate(StatelessDialog);