2019-10-18 06:38:10 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import IconButton from "../icon-button";
|
|
|
|
import Tooltip from "../tooltip";
|
|
|
|
import { handleAnyClick } from "../../utils/event";
|
|
|
|
import uniqueId from "lodash/uniqueId";
|
2019-11-20 06:13:04 +00:00
|
|
|
import Aside from "../layout/sub-components/aside";
|
|
|
|
import { desktop } from "../../utils/device";
|
|
|
|
import Backdrop from "../backdrop";
|
|
|
|
import { Text } from "../text";
|
|
|
|
import throttle from "lodash/throttle";
|
|
|
|
import styled from "styled-components";
|
2019-10-18 06:38:10 +00:00
|
|
|
|
2019-11-20 06:13:04 +00:00
|
|
|
const Content = styled.div`
|
|
|
|
position: relative;
|
|
|
|
width: 100%;
|
|
|
|
background-color: #fff;
|
|
|
|
padding: 0 16px 16px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Header = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
border-bottom: 1px solid #dee2e6;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Body = styled.div`
|
|
|
|
position: relative;
|
|
|
|
padding: 16px 0;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const HeaderText = styled(Text.ContentHeader)`
|
|
|
|
max-width: 500px;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
|
|
|
`;
|
2019-10-18 06:38:10 +00:00
|
|
|
class HelpButton extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-11-20 06:13:04 +00:00
|
|
|
this.state = { isOpen: false, displayType: this.getTypeByWidth() };
|
2019-10-18 06:38:10 +00:00
|
|
|
this.ref = React.createRef();
|
|
|
|
this.refTooltip = React.createRef();
|
|
|
|
this.id = uniqueId();
|
2019-11-20 06:13:04 +00:00
|
|
|
|
|
|
|
this.throttledResize = throttle(this.resize, 300);
|
2019-10-18 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
afterShow = () => {
|
|
|
|
this.refTooltip.current.updatePosition();
|
|
|
|
//console.log(`afterShow ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
|
|
|
this.setState({ isOpen: true }, () => {
|
|
|
|
handleAnyClick(true, this.handleClick);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
afterHide = () => {
|
|
|
|
//console.log(`afterHide ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
|
|
|
if (this.state.isOpen) {
|
|
|
|
this.setState({ isOpen: false }, () => {
|
|
|
|
handleAnyClick(false, this.handleClick);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
handleClick = e => {
|
|
|
|
//console.log(`handleClick ${this.props.tooltipId} isOpen=${this.state.isOpen}`, this.ref, e);
|
|
|
|
|
|
|
|
if (!this.ref.current.contains(e.target)) {
|
|
|
|
//console.log(`hideTooltip() tooltipId=${this.props.tooltipId}`, this.refTooltip.current);
|
|
|
|
this.refTooltip.current.hideTooltip();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-20 06:13:04 +00:00
|
|
|
onClose = () => {
|
|
|
|
this.setState({ isOpen: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener("resize", this.throttledResize);
|
|
|
|
}
|
|
|
|
|
2019-10-18 06:38:10 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
handleAnyClick(false, this.handleClick);
|
2019-11-20 06:13:04 +00:00
|
|
|
window.removeEventListener("resize", this.throttledResize);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.displayType !== prevProps.displayType) {
|
|
|
|
this.setState({ displayType: this.getTypeByWidth() });
|
|
|
|
}
|
|
|
|
if (this.state.isOpen && this.state.displayType === "aside") {
|
|
|
|
window.addEventListener("popstate", this.popstate, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
popstate = () => {
|
|
|
|
window.removeEventListener("popstate", this.popstate, false);
|
|
|
|
this.onClose();
|
|
|
|
window.history.go(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
resize = () => {
|
|
|
|
if (this.props.displayType !== "auto") return;
|
|
|
|
const type = this.getTypeByWidth();
|
|
|
|
if (type === this.state.displayType) return;
|
|
|
|
this.setState({ displayType: type });
|
|
|
|
};
|
|
|
|
|
|
|
|
getTypeByWidth = () => {
|
|
|
|
if (this.props.displayType !== "auto") return this.props.displayType;
|
|
|
|
return window.innerWidth < desktop.match(/\d+/)[0] ? "aside" : "dropdown";
|
|
|
|
};
|
|
|
|
|
|
|
|
onClick = () => {
|
|
|
|
this.setState({isOpen: !this.state.isOpen});
|
2019-10-18 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-11-20 06:13:04 +00:00
|
|
|
const { isOpen, displayType } = this.state;
|
|
|
|
const {
|
|
|
|
tooltipContent,
|
|
|
|
place,
|
|
|
|
offsetRight,
|
|
|
|
offsetLeft,
|
|
|
|
zIndex,
|
|
|
|
HelpButtonHeaderContent
|
|
|
|
} = this.props;
|
2019-10-18 06:38:10 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={this.ref}>
|
|
|
|
<IconButton
|
|
|
|
id={this.id}
|
|
|
|
className="icon-button"
|
|
|
|
isClickable={true}
|
|
|
|
iconName="QuestionIcon"
|
|
|
|
size={13}
|
2019-11-20 06:13:04 +00:00
|
|
|
onClick={this.onClick}
|
2019-10-18 06:38:10 +00:00
|
|
|
/>
|
2019-11-20 06:13:04 +00:00
|
|
|
{displayType === "dropdown" ? (
|
|
|
|
<Tooltip
|
|
|
|
id={this.id}
|
|
|
|
reference={this.refTooltip}
|
|
|
|
effect="solid"
|
|
|
|
place={place}
|
|
|
|
offsetRight={offsetRight}
|
|
|
|
offsetLeft={offsetLeft}
|
|
|
|
afterShow={this.afterShow}
|
|
|
|
afterHide={this.afterHide}
|
|
|
|
>
|
|
|
|
{tooltipContent}
|
|
|
|
</Tooltip>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<Backdrop onClick={this.onClose} visible={isOpen} zIndex={zIndex} />
|
|
|
|
<Aside visible={isOpen} scale={false} zIndex={zIndex}>
|
|
|
|
<Content>
|
2019-11-25 08:48:36 +00:00
|
|
|
{
|
|
|
|
HelpButtonHeaderContent &&
|
|
|
|
<Header>
|
|
|
|
<HeaderText>
|
|
|
|
<Text.Body isBold={true} fontSize={21}>
|
|
|
|
{HelpButtonHeaderContent}
|
|
|
|
</Text.Body>
|
|
|
|
</HeaderText>
|
|
|
|
</Header>
|
|
|
|
}
|
2019-11-20 06:13:04 +00:00
|
|
|
<Body>{tooltipContent}</Body>
|
|
|
|
</Content>
|
|
|
|
</Aside>
|
|
|
|
</>
|
|
|
|
)}
|
2019-10-18 06:38:10 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpButton.propTypes = {
|
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
|
|
|
PropTypes.node
|
|
|
|
]),
|
|
|
|
tooltipContent: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
|
2019-10-21 05:59:47 +00:00
|
|
|
offsetRight: PropTypes.number,
|
2019-10-18 06:38:10 +00:00
|
|
|
tooltipMaxWidth: PropTypes.number,
|
2019-10-18 12:13:08 +00:00
|
|
|
tooltipId: PropTypes.string,
|
2019-10-21 09:05:45 +00:00
|
|
|
place: PropTypes.string,
|
2019-11-20 06:13:04 +00:00
|
|
|
offsetLeft: PropTypes.number,
|
|
|
|
zIndex: PropTypes.number,
|
|
|
|
displayType: PropTypes.oneOf(["dropdown", "aside", "auto"]),
|
|
|
|
HelpButtonHeaderContent: PropTypes.string
|
2019-10-18 06:38:10 +00:00
|
|
|
};
|
|
|
|
|
2019-10-18 12:13:08 +00:00
|
|
|
HelpButton.defaultProps = {
|
|
|
|
place: "top",
|
2019-10-21 09:05:45 +00:00
|
|
|
offsetRight: 120,
|
2019-11-20 06:13:04 +00:00
|
|
|
offsetLeft: 0,
|
|
|
|
zIndex: 310,
|
|
|
|
displayType: "auto"
|
|
|
|
};
|
2019-10-18 12:13:08 +00:00
|
|
|
|
2019-10-18 06:38:10 +00:00
|
|
|
export default HelpButton;
|