// @flow import * as React from 'react'; import classNames from 'classnames'; import omit from 'lodash/omit'; import uniqueId from 'lodash/uniqueId'; import { defineMessages, injectIntl } from 'react-intl'; import IconClose from '../../icons/general/IconClose'; const ALERT_TYPE = 'alert'; const DIALOG_TYPE = 'dialog'; const messages = defineMessages({ closeModalText: { defaultMessage: 'Close Modal', description: 'Button to close modal', id: 'boxui.modalDialog.closeModalText', }, }); type Props = { children: React.Node, className?: string, closeButtonProps: Object, intl: Object, modalRef?: Function, onRequestClose?: Function, title?: React.Node, type?: 'alert' | 'dialog', }; class ModalDialog extends React.Component { static defaultProps = { type: DIALOG_TYPE, closeButtonProps: {}, }; componentWillMount() { this.modalID = uniqueId('modal'); } /** * Handles clicking on the close button * @param {SyntheticMouseEvent} event * @return {void} */ onCloseButtonClick = (event: SyntheticMouseEvent) => { const { onRequestClose } = this.props; if (onRequestClose) { onRequestClose(event); } }; modalID: string; /** * Renders a button if onRequestClose is passed in * @return {ReactElement|null} - Returns the button, or null if the button shouldn't be rendered */ renderCloseButton() { const { closeButtonProps, onRequestClose, intl } = this.props; const { formatMessage } = intl; if (!onRequestClose) { return null; } return ( // eslint-disable-next-line react/button-has-type ); } renderContent() { const { children, type } = this.props; if (type !== ALERT_TYPE) { return
{children}
; } const elements = React.Children.toArray(children); if (elements.length !== 2) { throw new Error('Alert modal must have exactly two children: A message and '); } return (

{elements[0]}

{elements[1]}
); } render() { const { className, modalRef, title, type, ...rest // Useful for resin tagging, and other misc tags such as a11y } = this.props; const isAlertType = type === ALERT_TYPE; const divProps = omit(rest, ['children', 'closeButtonProps', 'onRequestClose', 'intl']); divProps.role = isAlertType ? 'alertdialog' : 'dialog'; divProps['aria-labelledby'] = `${this.modalID}-label`; if (isAlertType) { divProps['aria-describedby'] = `${this.modalID}-desc`; } return (
{this.renderCloseButton()}

{title}

{this.renderContent()}
); } } export { ModalDialog as ModalDialogBase }; export default injectIntl(ModalDialog);