2017-11-17 19:06:47 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-11-09 22:02:57 +00:00
|
|
|
import Button, { ButtonGroup } from '@atlaskit/button';
|
2018-03-13 04:45:00 +00:00
|
|
|
import Modal, { ModalFooter } from '@atlaskit/modal-dialog';
|
2017-11-17 19:06:47 +00:00
|
|
|
import _ from 'lodash';
|
2017-06-04 03:12:04 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
2019-09-13 13:03:40 +00:00
|
|
|
import { translate } from '../../../i18n/functions';
|
2018-10-18 08:28:08 +00:00
|
|
|
import type { DialogProps } from '../../constants';
|
2017-06-04 03:12:04 +00:00
|
|
|
|
2017-06-06 20:36:25 +00:00
|
|
|
/**
|
|
|
|
* The ID to be used for the cancel button if enabled.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const CANCEL_BUTTON_ID = 'modal-dialog-cancel-button';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID to be used for the ok button if enabled.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const OK_BUTTON_ID = 'modal-dialog-ok-button';
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
/**
|
2017-11-17 19:06:47 +00:00
|
|
|
* The type of the React {@code Component} props of {@link StatelessDialog}.
|
|
|
|
*
|
|
|
|
* @static
|
2017-06-04 03:12:04 +00:00
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
type Props = {
|
|
|
|
...DialogProps,
|
|
|
|
|
2020-04-01 07:47:51 +00:00
|
|
|
/**
|
|
|
|
* Custom dialog header that replaces the standard heading.
|
|
|
|
*/
|
|
|
|
customHeader?: React$Element<any> | Function,
|
2018-03-13 04:45:00 +00:00
|
|
|
|
2020-04-15 13:13:43 +00:00
|
|
|
/*
|
|
|
|
* True if listening for the Enter key should be disabled.
|
|
|
|
*/
|
|
|
|
disableEnter: boolean,
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
/**
|
2017-11-17 19:06:47 +00:00
|
|
|
* Disables dismissing the dialog when the blanket is clicked. Enabled
|
|
|
|
* by default.
|
|
|
|
*/
|
|
|
|
disableBlanketClickDismiss: boolean,
|
|
|
|
|
2018-03-07 00:28:19 +00:00
|
|
|
/**
|
|
|
|
* If true, the cancel button will not display but cancel actions, like
|
|
|
|
* clicking the blanket, will cancel.
|
|
|
|
*/
|
|
|
|
hideCancelButton: boolean,
|
|
|
|
|
2020-04-01 07:47:51 +00:00
|
|
|
i18n: Object,
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
/**
|
|
|
|
* Whether the dialog is modal. This means clicking on the blanket will
|
|
|
|
* leave the dialog open. No cancel button.
|
2017-06-04 03:12:04 +00:00
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
isModal: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables rendering of the submit button.
|
|
|
|
*/
|
|
|
|
submitDisabled: boolean,
|
|
|
|
|
2018-10-18 08:28:08 +00:00
|
|
|
/**
|
|
|
|
* Function to be used to retreive translated i18n labels.
|
|
|
|
*/
|
|
|
|
t: Function,
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
/**
|
|
|
|
* Width of the dialog, can be:
|
|
|
|
* - 'small' (400px), 'medium' (600px), 'large' (800px),
|
|
|
|
* 'x-large' (968px)
|
|
|
|
* - integer value for pixel width
|
|
|
|
* - string value for percentage
|
|
|
|
*/
|
|
|
|
width: string
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Web dialog that uses atlaskit modal-dialog to display dialogs.
|
|
|
|
*/
|
|
|
|
class StatelessDialog extends Component<Props> {
|
2018-03-13 04:45:00 +00:00
|
|
|
/**
|
|
|
|
* The functional component to be used for rendering the modal footer.
|
|
|
|
*/
|
|
|
|
_Footer: ?Function
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_dialogElement: ?HTMLElement;
|
2017-06-04 03:12:04 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-07 16:45:04 +00:00
|
|
|
* Initializes a new {@code StatelessDialog} instance.
|
2017-06-04 03:12:04 +00:00
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-06-07 16:45:04 +00:00
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
2017-06-04 03:12:04 +00:00
|
|
|
this._onCancel = this._onCancel.bind(this);
|
|
|
|
this._onDialogDismissed = this._onDialogDismissed.bind(this);
|
2017-06-06 20:36:25 +00:00
|
|
|
this._onKeyDown = this._onKeyDown.bind(this);
|
2017-06-04 03:12:04 +00:00
|
|
|
this._onSubmit = this._onSubmit.bind(this);
|
2018-10-29 16:30:31 +00:00
|
|
|
this._renderFooter = this._renderFooter.bind(this);
|
2017-06-06 20:36:25 +00:00
|
|
|
this._setDialogElement = this._setDialogElement.bind(this);
|
2017-06-04 03:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2018-03-13 04:45:00 +00:00
|
|
|
const {
|
2020-04-01 07:47:51 +00:00
|
|
|
customHeader,
|
2018-03-13 04:45:00 +00:00
|
|
|
children,
|
|
|
|
t /* The following fixes a flow error: */ = _.identity,
|
|
|
|
titleString,
|
|
|
|
titleKey,
|
|
|
|
width
|
|
|
|
} = this.props;
|
2017-10-06 16:14:45 +00:00
|
|
|
|
2018-03-13 04:45:00 +00:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
autoFocus = { true }
|
2020-04-01 07:47:51 +00:00
|
|
|
components = {{
|
|
|
|
Header: customHeader
|
|
|
|
}}
|
2018-10-29 16:30:31 +00:00
|
|
|
footer = { this._renderFooter }
|
2020-04-01 07:47:51 +00:00
|
|
|
heading = { customHeader ? undefined : titleString || t(titleKey) }
|
2018-03-13 04:45:00 +00:00
|
|
|
i18n = { this.props.i18n }
|
|
|
|
onClose = { this._onDialogDismissed }
|
|
|
|
onDialogDismissed = { this._onDialogDismissed }
|
|
|
|
shouldCloseOnEscapePress = { true }
|
|
|
|
width = { width || 'medium' }>
|
2019-01-04 09:49:04 +00:00
|
|
|
<div
|
|
|
|
onKeyDown = { this._onKeyDown }
|
|
|
|
ref = { this._setDialogElement }>
|
|
|
|
<form
|
|
|
|
className = 'modal-dialog-form'
|
|
|
|
id = 'modal-dialog-form'
|
|
|
|
onSubmit = { this._onSubmit }>
|
|
|
|
{ children }
|
|
|
|
</form>
|
|
|
|
</div>
|
2018-03-13 04:45:00 +00:00
|
|
|
</Modal>
|
2017-06-07 16:45:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:30:31 +00:00
|
|
|
_renderFooter: () => React$Node;
|
2018-03-13 04:45:00 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-29 16:30:31 +00:00
|
|
|
* Returns a ReactElement to display buttons for closing the modal.
|
2018-03-13 04:45:00 +00:00
|
|
|
*
|
2018-10-29 16:30:31 +00:00
|
|
|
* @param {Object} propsFromModalFooter - The props passed in from the
|
|
|
|
* {@link ModalFooter} component.
|
2018-03-13 04:45:00 +00:00
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2018-10-29 16:30:31 +00:00
|
|
|
_renderFooter(propsFromModalFooter) {
|
2018-03-13 04:45:00 +00:00
|
|
|
// Filter out falsy (null) values because {@code ButtonGroup} will error
|
|
|
|
// if passed in anything but buttons with valid type props.
|
|
|
|
const buttons = [
|
2018-10-29 16:30:31 +00:00
|
|
|
this._renderOKButton(),
|
|
|
|
this._renderCancelButton()
|
2018-03-13 04:45:00 +00:00
|
|
|
].filter(Boolean);
|
|
|
|
|
2018-10-29 16:30:31 +00:00
|
|
|
return (
|
|
|
|
<ModalFooter showKeyline = { propsFromModalFooter.showKeyline } >
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Atlaskit has this empty span (JustifySim) so...
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
<span />
|
|
|
|
<ButtonGroup>
|
|
|
|
{ buttons }
|
|
|
|
</ButtonGroup>
|
|
|
|
</ModalFooter>
|
|
|
|
);
|
2018-03-13 04:45:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_onCancel: () => void;
|
|
|
|
|
2017-06-07 16:45:04 +00:00
|
|
|
/**
|
|
|
|
* Dispatches action to hide the dialog.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onCancel() {
|
|
|
|
if (!this.props.isModal) {
|
2017-11-17 19:06:47 +00:00
|
|
|
const { onCancel } = this.props;
|
|
|
|
|
|
|
|
onCancel && onCancel();
|
2017-06-07 16:45:04 +00:00
|
|
|
}
|
2017-06-04 03:12:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_onDialogDismissed: () => void;
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
/**
|
|
|
|
* Handles click on the blanket area.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDialogDismissed() {
|
|
|
|
if (!this.props.disableBlanketClickDismiss) {
|
|
|
|
this._onCancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_onSubmit: (?string) => void;
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
/**
|
2017-06-07 16:45:04 +00:00
|
|
|
* Dispatches the action when submitting the dialog.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} value - The submitted value if any.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onSubmit(value) {
|
2017-11-17 19:06:47 +00:00
|
|
|
const { onSubmit } = this.props;
|
|
|
|
|
|
|
|
onSubmit && onSubmit(value);
|
2017-06-07 16:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders Cancel button.
|
2017-06-04 03:12:04 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-03-13 04:45:00 +00:00
|
|
|
* @returns {ReactElement|null} The Cancel button if enabled and dialog is
|
|
|
|
* not modal.
|
2017-06-04 03:12:04 +00:00
|
|
|
*/
|
2018-10-29 16:30:31 +00:00
|
|
|
_renderCancelButton() {
|
|
|
|
if (this.props.cancelDisabled
|
|
|
|
|| this.props.isModal
|
|
|
|
|| this.props.hideCancelButton) {
|
2017-06-04 03:12:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
const {
|
|
|
|
t /* The following fixes a flow error: */ = _.identity
|
|
|
|
} = this.props;
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
return (
|
2017-11-09 22:02:57 +00:00
|
|
|
<Button
|
2017-06-04 03:12:04 +00:00
|
|
|
appearance = 'subtle'
|
2017-06-06 20:36:25 +00:00
|
|
|
id = { CANCEL_BUTTON_ID }
|
2017-11-09 22:02:57 +00:00
|
|
|
key = 'cancel'
|
|
|
|
onClick = { this._onCancel }
|
|
|
|
type = 'button'>
|
2019-03-06 17:23:53 +00:00
|
|
|
{ t(this.props.cancelKey || 'dialog.Cancel') }
|
2017-11-09 22:02:57 +00:00
|
|
|
</Button>
|
2017-06-04 03:12:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-07 16:45:04 +00:00
|
|
|
* Renders OK button.
|
2017-06-04 03:12:04 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-03-13 04:45:00 +00:00
|
|
|
* @returns {ReactElement|null} The OK button if enabled.
|
2017-06-04 03:12:04 +00:00
|
|
|
*/
|
2018-10-29 16:30:31 +00:00
|
|
|
_renderOKButton() {
|
|
|
|
if (this.props.submitDisabled) {
|
2017-06-04 03:12:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
const {
|
|
|
|
t /* The following fixes a flow error: */ = _.identity
|
|
|
|
} = this.props;
|
|
|
|
|
2017-06-04 03:12:04 +00:00
|
|
|
return (
|
2017-11-09 22:02:57 +00:00
|
|
|
<Button
|
2017-06-04 03:12:04 +00:00
|
|
|
appearance = 'primary'
|
|
|
|
form = 'modal-dialog-form'
|
2017-06-06 20:36:25 +00:00
|
|
|
id = { OK_BUTTON_ID }
|
2018-10-29 16:30:31 +00:00
|
|
|
isDisabled = { this.props.okDisabled }
|
2017-11-09 22:02:57 +00:00
|
|
|
key = 'submit'
|
|
|
|
onClick = { this._onSubmit }
|
|
|
|
type = 'button'>
|
2019-03-06 17:23:53 +00:00
|
|
|
{ t(this.props.okKey || 'dialog.Ok') }
|
2017-11-09 22:02:57 +00:00
|
|
|
</Button>
|
2017-06-04 03:12:04 +00:00
|
|
|
);
|
|
|
|
}
|
2017-06-06 20:36:25 +00:00
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_setDialogElement: (?HTMLElement) => void;
|
|
|
|
|
2017-06-06 20:36:25 +00:00
|
|
|
/**
|
|
|
|
* Sets the instance variable for the div containing the component's dialog
|
|
|
|
* element so it can be accessed directly.
|
|
|
|
*
|
2017-11-17 19:06:47 +00:00
|
|
|
* @param {HTMLElement} element - The DOM element for the component's
|
|
|
|
* dialog.
|
2017-06-06 20:36:25 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-11-17 19:06:47 +00:00
|
|
|
_setDialogElement(element: ?HTMLElement) {
|
2017-06-06 20:36:25 +00:00
|
|
|
this._dialogElement = element;
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:06:47 +00:00
|
|
|
_onKeyDown: (Object) => void;
|
|
|
|
|
2017-06-06 20:36:25 +00:00
|
|
|
/**
|
|
|
|
* Handles 'Enter' key in the dialog to submit/hide dialog depending on
|
|
|
|
* the available buttons and their disabled state.
|
|
|
|
*
|
2017-06-09 04:57:43 +00:00
|
|
|
* @param {Object} event - The key event.
|
2017-06-06 20:36:25 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onKeyDown(event) {
|
2017-06-28 19:58:44 +00:00
|
|
|
// If the event coming to the dialog has been subject to preventDefault
|
|
|
|
// we don't handle it here.
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:13:43 +00:00
|
|
|
if (event.key === 'Enter' && !this.props.disableEnter) {
|
2017-06-14 18:41:22 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2017-06-06 20:36:25 +00:00
|
|
|
if (this.props.submitDisabled && !this.props.cancelDisabled) {
|
|
|
|
this._onCancel();
|
|
|
|
} else if (!this.props.okDisabled) {
|
|
|
|
this._onSubmit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-04 03:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(StatelessDialog);
|