| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 | // @flow
import Button, { ButtonGroup } from '@atlaskit/button';
import ModalDialog from '@atlaskit/modal-dialog';
import { AtlasKitThemeProvider } from '@atlaskit/theme';
import _ from 'lodash';
import React, { Component } from 'react';
import { translate } from '../../i18n';
import type { DialogProps } from '../constants';
/**
 * 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';
/**
 * The type of the React {@code Component} props of {@link StatelessDialog}.
 *
 * @static
 */
type Props = {
    ...DialogProps,
    /**
     * Disables dismissing the dialog when the blanket is clicked. Enabled
     * by default.
     */
    disableBlanketClickDismiss: boolean,
    /**
     * Whether the dialog is modal. This means clicking on the blanket will
     * leave the dialog open. No cancel button.
     */
    isModal: boolean,
    /**
     * Disables rendering of the submit button.
     */
    submitDisabled: boolean,
    /**
     * 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> {
    _dialogElement: ?HTMLElement;
    /**
     * Initializes a new {@code StatelessDialog} instance.
     *
     * @param {Object} props - The read-only properties with which the new
     * instance is to be initialized.
     */
    constructor(props) {
        super(props);
        // Bind event handlers so they are only bound once for every instance.
        this._onCancel = this._onCancel.bind(this);
        this._onDialogDismissed = this._onDialogDismissed.bind(this);
        this._onKeyDown = this._onKeyDown.bind(this);
        this._onSubmit = this._onSubmit.bind(this);
        this._setDialogElement = this._setDialogElement.bind(this);
    }
    /**
     * React Component method that executes once component is mounted.
     *
     * @inheritdoc
     */
    componentDidMount() {
        this._updateButtonFocus();
    }
    /**
     * React Component method that executes once component is updated.
     *
     * @param {Object} prevProps - The previous properties, before the update.
     * @returns {void}
     */
    componentDidUpdate(prevProps) {
        // if there is an update in any of the buttons enable/disable props
        // update the focus if needed
        if (prevProps.okDisabled !== this.props.okDisabled
                || prevProps.cancelDisabled !== this.props.cancelDisabled
                || prevProps.submitDisabled !== this.props.submitDisabled) {
            this._updateButtonFocus();
        }
    }
    /**
     * Implements React's {@link Component#render()}.
     *
     * @inheritdoc
     * @returns {ReactElement}
     */
    render() {
        return (
            /**
             * Enabled light theme for dialogs until all in-dialog components
             * support dark theme.
             */
            <AtlasKitThemeProvider mode = 'light'>
                <div
                    onKeyDown = { this._onKeyDown }
                    ref = { this._setDialogElement }>
                    <ModalDialog
                        footer = { this._renderFooter() }
                        header = { this._renderHeader() }
                        isOpen = { true }
                        onDialogDismissed = { this._onDialogDismissed }
                        width = { this.props.width || 'medium' }>
                        <div>
                            <form
                                className = 'modal-dialog-form'
                                id = 'modal-dialog-form'
                                onSubmit = { this._onSubmit }>
                                { this.props.children }
                            </form>
                        </div>
                    </ModalDialog>
                </div>
            </AtlasKitThemeProvider>
        );
    }
    _onCancel: () => void;
    /**
     * Dispatches action to hide the dialog.
     *
     * @returns {void}
     */
    _onCancel() {
        if (!this.props.isModal) {
            const { onCancel } = this.props;
            onCancel && onCancel();
        }
    }
    _onDialogDismissed: () => void;
    /**
     * Handles click on the blanket area.
     *
     * @returns {void}
     */
    _onDialogDismissed() {
        if (!this.props.disableBlanketClickDismiss) {
            this._onCancel();
        }
    }
    _onSubmit: (?string) => void;
    /**
     * Dispatches the action when submitting the dialog.
     *
     * @private
     * @param {string} value - The submitted value if any.
     * @returns {void}
     */
    _onSubmit(value) {
        const { onSubmit } = this.props;
        onSubmit && onSubmit(value);
    }
    /**
     * Renders Cancel button.
     *
     * @private
     * @returns {*} The Cancel button if enabled and dialog is not modal.
     */
    _renderCancelButton() {
        if (this.props.cancelDisabled || this.props.isModal) {
            return null;
        }
        const {
            t /* The following fixes a flow error: */ = _.identity
        } = this.props;
        return (
            <Button
                appearance = 'subtle'
                id = { CANCEL_BUTTON_ID }
                key = 'cancel'
                onClick = { this._onCancel }
                type = 'button'>
                { t(this.props.cancelTitleKey || 'dialog.Cancel') }
            </Button>
        );
    }
    /**
     * Renders component in dialog footer.
     *
     * @private
     * @returns {ReactElement}
     */
    _renderFooter() {
        // Filter out falsy (null) values because {@code ButtonGroup} will error
        // if passed in anything but buttons with valid type props.
        const buttons = [
            this._renderCancelButton(),
            this._renderOKButton()
        ].filter(Boolean);
        return (
            <footer className = 'modal-dialog-footer'>
                <ButtonGroup>
                    { buttons }
                </ButtonGroup>
            </footer>
        );
    }
    /**
     * Renders component in dialog header.
     *
     * @private
     * @returns {ReactElement}
     */
    _renderHeader() {
        const {
            t /* The following fixes a flow error: */ = _.identity
        } = this.props;
        return (
            <header>
                <h3>
                    { this.props.titleString || t(this.props.titleKey) }
                </h3>
            </header>
        );
    }
    /**
     * Renders OK button.
     *
     * @private
     * @returns {*} The OK button if enabled.
     */
    _renderOKButton() {
        if (this.props.submitDisabled) {
            return null;
        }
        const {
            t /* The following fixes a flow error: */ = _.identity
        } = this.props;
        return (
            <Button
                appearance = 'primary'
                form = 'modal-dialog-form'
                id = { OK_BUTTON_ID }
                isDisabled = { this.props.okDisabled }
                key = 'submit'
                onClick = { this._onSubmit }
                type = 'button'>
                { t(this.props.okTitleKey || 'dialog.Ok') }
            </Button>
        );
    }
    _setDialogElement: (?HTMLElement) => void;
    /**
     * Sets the instance variable for the div containing the component's dialog
     * element so it can be accessed directly.
     *
     * @param {HTMLElement} element - The DOM element for the component's
     * dialog.
     * @private
     * @returns {void}
     */
    _setDialogElement(element: ?HTMLElement) {
        this._dialogElement = element;
    }
    _onKeyDown: (Object) => void;
    /**
     * Handles 'Enter' key in the dialog to submit/hide dialog depending on
     * the available buttons and their disabled state.
     *
     * @param {Object} event - The key event.
     * @private
     * @returns {void}
     */
    _onKeyDown(event) {
        // If the event coming to the dialog has been subject to preventDefault
        // we don't handle it here.
        if (event.defaultPrevented) {
            return;
        }
        if (event.key === 'Enter') {
            event.preventDefault();
            event.stopPropagation();
            if (this.props.submitDisabled && !this.props.cancelDisabled) {
                this._onCancel();
            } else if (!this.props.okDisabled) {
                this._onSubmit();
            }
        }
    }
    /**
     * Updates focused button, if we have a reference to the dialog element.
     * Focus on available button if there is no focus already.
     *
     * @private
     * @returns {void}
     */
    _updateButtonFocus() {
        const dialogElement = this._dialogElement;
        if (dialogElement) {
            // if we have a focused element inside the dialog, skip changing
            // the focus
            if (dialogElement.contains(document.activeElement)) {
                return;
            }
            let buttonToFocus;
            if (this.props.submitDisabled) {
                buttonToFocus
                    = dialogElement.querySelector(`[id=${CANCEL_BUTTON_ID}]`);
            } else if (!this.props.okDisabled) {
                buttonToFocus
                    = dialogElement.querySelector(`[id=${OK_BUTTON_ID}]`);
            }
            if (buttonToFocus) {
                buttonToFocus.focus();
            }
        }
    }
}
export default translate(StatelessDialog);
 |