2023-09-13 15:12:23 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Popover } from 'reactstrap';
|
|
|
|
import PropTypes from 'prop-types';
|
2025-03-01 02:12:48 +00:00
|
|
|
import { KeyCodes } from '../constants';
|
|
|
|
import { getEventClassName } from '../utils/dom';
|
2023-09-13 15:12:23 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
target: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
|
|
|
|
boundariesElement: PropTypes.object,
|
|
|
|
innerClassName: PropTypes.string,
|
|
|
|
popoverClassName: PropTypes.string,
|
|
|
|
children: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2025-03-01 02:12:48 +00:00
|
|
|
hidePopover: PropTypes.func.isRequired,
|
|
|
|
hidePopoverWithEsc: PropTypes.func,
|
2023-09-13 15:12:23 +00:00
|
|
|
hideArrow: PropTypes.bool,
|
2025-03-01 02:12:48 +00:00
|
|
|
canHidePopover: PropTypes.bool,
|
2023-09-13 15:12:23 +00:00
|
|
|
placement: PropTypes.string,
|
|
|
|
modifiers: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2025-03-01 02:12:48 +00:00
|
|
|
class CustomizePopover extends React.Component {
|
2023-09-13 15:12:23 +00:00
|
|
|
|
2025-03-01 02:12:48 +00:00
|
|
|
popoverRef = null;
|
2023-09-13 15:12:23 +00:00
|
|
|
isSelectOpen = false;
|
|
|
|
|
|
|
|
componentDidMount() {
|
2023-12-19 04:13:49 +00:00
|
|
|
document.addEventListener('mousedown', this.onMouseDown);
|
2023-09-13 15:12:23 +00:00
|
|
|
document.addEventListener('keydown', this.onKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2023-12-19 04:13:49 +00:00
|
|
|
document.removeEventListener('mousedown', this.onMouseDown);
|
2023-09-13 15:12:23 +00:00
|
|
|
document.removeEventListener('keydown', this.onKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyDown = (e) => {
|
2025-03-10 06:27:08 +00:00
|
|
|
const { canHidePopover = true, hidePopoverWithEsc } = this.props;
|
2025-03-01 02:12:48 +00:00
|
|
|
if (e.keyCode === KeyCodes.Escape && typeof hidePopoverWithEsc === 'function' && !this.isSelectOpen) {
|
2023-09-13 15:12:23 +00:00
|
|
|
e.preventDefault();
|
2025-03-01 02:12:48 +00:00
|
|
|
hidePopoverWithEsc();
|
2023-09-13 15:12:23 +00:00
|
|
|
} else if (e.keyCode === KeyCodes.Enter) {
|
|
|
|
// Resolve the default behavior of the enter key when entering formulas is blocked
|
2025-03-01 02:12:48 +00:00
|
|
|
if (canHidePopover) return;
|
2023-09-13 15:12:23 +00:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onMouseDown = (e) => {
|
2025-03-10 06:27:08 +00:00
|
|
|
const { canHidePopover = true } = this.props;
|
|
|
|
if (!canHidePopover) return;
|
2025-03-01 02:12:48 +00:00
|
|
|
if (this.popoverRef && e && getEventClassName(e).indexOf('popover') === -1 && !this.popoverRef.contains(e.target)) {
|
|
|
|
this.props.hidePopover(e);
|
2023-09-13 15:12:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onPopoverInsideClick = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2025-03-10 06:27:08 +00:00
|
|
|
target, boundariesElement, innerClassName, popoverClassName, hideArrow = true, modifiers,
|
|
|
|
placement = 'bottom-start',
|
2023-09-13 15:12:23 +00:00
|
|
|
} = this.props;
|
|
|
|
let additionalProps = {};
|
|
|
|
if (boundariesElement) {
|
|
|
|
additionalProps.boundariesElement = boundariesElement;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Popover
|
|
|
|
placement={placement}
|
|
|
|
isOpen={true}
|
|
|
|
target={target}
|
|
|
|
fade={false}
|
|
|
|
hideArrow={hideArrow}
|
|
|
|
innerClassName={innerClassName}
|
|
|
|
className={popoverClassName}
|
|
|
|
modifiers={modifiers}
|
|
|
|
{...additionalProps}
|
|
|
|
>
|
2025-03-01 02:12:48 +00:00
|
|
|
<div ref={ref => this.popoverRef = ref} onClick={this.onPopoverInsideClick}>
|
2023-09-13 15:12:23 +00:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-01 02:12:48 +00:00
|
|
|
CustomizePopover.propTypes = propTypes;
|
2023-09-13 15:12:23 +00:00
|
|
|
|
2025-03-01 02:12:48 +00:00
|
|
|
export default CustomizePopover;
|