2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-05-17 06:13:08 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2023-07-18 01:19:05 +08:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
2021-11-11 03:10:35 +08:00
|
|
|
import Styled from './styles';
|
2017-04-05 22:25:26 +08:00
|
|
|
|
2019-05-17 06:13:08 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
modalClose: {
|
|
|
|
id: 'app.modal.close',
|
|
|
|
description: 'Close',
|
|
|
|
},
|
|
|
|
modalCloseDescription: {
|
|
|
|
id: 'app.modal.close.description',
|
|
|
|
description: 'Disregards changes and closes the modal',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-04-05 22:25:26 +08:00
|
|
|
const propTypes = {
|
2018-12-03 14:01:19 +08:00
|
|
|
title: PropTypes.string,
|
2017-04-05 22:25:26 +08:00
|
|
|
dismiss: PropTypes.shape({
|
2019-05-24 23:32:31 +08:00
|
|
|
callback: PropTypes.func,
|
2017-04-05 22:25:26 +08:00
|
|
|
}),
|
2022-11-11 02:09:21 +08:00
|
|
|
headerPosition: PropTypes.string,
|
2023-07-18 01:19:05 +08:00
|
|
|
shouldCloseOnOverlayClick: PropTypes.bool,
|
|
|
|
shouldShowCloseButton: PropTypes.bool,
|
|
|
|
overlayClassName: PropTypes.string,
|
|
|
|
modalisOpen: PropTypes.bool,
|
2017-04-05 22:25:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
2023-07-18 01:19:05 +08:00
|
|
|
title: '',
|
|
|
|
dismiss: {
|
|
|
|
callback: null,
|
|
|
|
},
|
2017-04-19 03:06:51 +08:00
|
|
|
shouldCloseOnOverlayClick: true,
|
2019-02-27 01:40:01 +08:00
|
|
|
shouldShowCloseButton: true,
|
2022-11-09 01:51:36 +08:00
|
|
|
overlayClassName: 'modalOverlay',
|
2022-11-11 02:09:21 +08:00
|
|
|
headerPosition: 'inner',
|
2023-07-18 01:19:05 +08:00
|
|
|
modalisOpen: false,
|
2017-04-05 22:25:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ModalSimple extends Component {
|
2019-01-15 04:23:46 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2023-07-18 01:19:05 +08:00
|
|
|
this.modalRef = React.createRef();
|
2019-01-15 04:23:46 +08:00
|
|
|
this.handleDismiss = this.handleDismiss.bind(this);
|
2023-07-18 01:19:05 +08:00
|
|
|
this.handleRequestClose = this.handleRequestClose.bind(this);
|
|
|
|
this.handleOutsideClick = this.handleOutsideClick.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
document.addEventListener('mousedown', this.handleOutsideClick, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('mousedown', this.handleOutsideClick, false);
|
2019-01-15 04:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleDismiss() {
|
2023-07-18 01:19:05 +08:00
|
|
|
const { modalHide, dismiss } = this.props;
|
2019-06-22 01:47:10 +08:00
|
|
|
if (!dismiss || !modalHide) return;
|
2019-06-22 01:39:12 +08:00
|
|
|
modalHide(dismiss.callback);
|
2019-01-15 04:23:46 +08:00
|
|
|
}
|
|
|
|
|
2023-07-18 01:19:05 +08:00
|
|
|
handleRequestClose(event) {
|
|
|
|
const { onRequestClose } = this.props;
|
|
|
|
const closeModal = onRequestClose || this.handleDismiss;
|
|
|
|
|
|
|
|
closeModal();
|
|
|
|
|
|
|
|
if (event && event.type === 'click') {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (document.activeElement) {
|
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOutsideClick(e) {
|
|
|
|
const { modalisOpen } = this.props;
|
|
|
|
if (this.modalRef.current && !this.modalRef.current.contains(e.target) && modalisOpen) {
|
|
|
|
this.handleRequestClose(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 22:25:26 +08:00
|
|
|
render() {
|
|
|
|
const {
|
2022-11-24 22:53:30 +08:00
|
|
|
id,
|
2019-05-17 06:13:08 +08:00
|
|
|
intl,
|
2017-04-05 22:25:26 +08:00
|
|
|
title,
|
2018-12-03 14:01:19 +08:00
|
|
|
hideBorder,
|
2017-04-05 22:25:26 +08:00
|
|
|
dismiss,
|
2017-04-19 03:06:51 +08:00
|
|
|
className,
|
|
|
|
modalisOpen,
|
2019-01-11 06:58:42 +08:00
|
|
|
onRequestClose,
|
2019-02-27 01:40:01 +08:00
|
|
|
shouldShowCloseButton,
|
2019-05-09 03:13:05 +08:00
|
|
|
contentLabel,
|
2022-11-11 02:09:21 +08:00
|
|
|
headerPosition,
|
2022-03-01 03:46:13 +08:00
|
|
|
'data-test': dataTest,
|
2023-07-18 01:19:05 +08:00
|
|
|
children,
|
2017-10-11 06:08:51 +08:00
|
|
|
...otherProps
|
2017-04-05 22:25:26 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
2021-11-11 03:10:35 +08:00
|
|
|
<Styled.SimpleModal
|
2023-07-18 01:19:05 +08:00
|
|
|
id={id || 'simpleModal'}
|
2017-04-19 03:06:51 +08:00
|
|
|
isOpen={modalisOpen}
|
2021-11-11 03:10:35 +08:00
|
|
|
className={className}
|
2023-07-18 01:19:05 +08:00
|
|
|
onRequestClose={this.handleRequestClose}
|
2019-05-24 23:32:31 +08:00
|
|
|
contentLabel={title || contentLabel}
|
2023-06-15 09:36:18 +08:00
|
|
|
dataTest={dataTest}
|
2017-04-19 03:06:51 +08:00
|
|
|
{...otherProps}
|
|
|
|
>
|
2023-07-18 02:04:04 +08:00
|
|
|
<FocusTrap active={modalisOpen} focusTrapOptions={{ initialFocus: false }}>
|
2023-07-18 01:19:05 +08:00
|
|
|
<div ref={this.modalRef}>
|
|
|
|
<Styled.Header
|
|
|
|
hideBorder={hideBorder}
|
|
|
|
headerPosition={headerPosition}
|
|
|
|
shouldShowCloseButton={shouldShowCloseButton}
|
|
|
|
modalDismissDescription={intl.formatMessage(intlMessages.modalCloseDescription)}
|
|
|
|
closeButtonProps={{
|
|
|
|
label: intl.formatMessage(intlMessages.modalClose),
|
|
|
|
'aria-label': `${intl.formatMessage(intlMessages.modalClose)} ${title || contentLabel}`,
|
|
|
|
onClick: this.handleRequestClose,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{title || ''}
|
|
|
|
</Styled.Header>
|
|
|
|
<Styled.Content>
|
|
|
|
{children}
|
|
|
|
</Styled.Content>
|
|
|
|
</div>
|
|
|
|
</FocusTrap>
|
2021-11-11 03:10:35 +08:00
|
|
|
</Styled.SimpleModal>
|
2017-04-05 22:25:26 +08:00
|
|
|
);
|
|
|
|
}
|
2017-10-11 06:08:51 +08:00
|
|
|
}
|
2017-04-05 22:25:26 +08:00
|
|
|
|
|
|
|
ModalSimple.propTypes = propTypes;
|
|
|
|
ModalSimple.defaultProps = defaultProps;
|
|
|
|
|
2023-03-21 21:00:46 +08:00
|
|
|
export default injectIntl(ModalSimple);
|