DocSpace-client/packages/components/help-button/index.js

162 lines
3.8 KiB
JavaScript
Raw Normal View History

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";
2023-01-23 08:24:41 +00:00
import InfoReactSvgUrl from "PUBLIC_DIR/images/info.react.svg?url";
2023-01-11 15:08:01 +00:00
class HelpButton extends React.Component {
constructor(props) {
super(props);
this.state = {
hideTooltip: false,
};
this.ref = React.createRef();
this.refTooltip = React.createRef();
this.id = this.props.id || uniqueId();
}
afterShow = () => {
this.refTooltip.current.updatePosition();
handleAnyClick(true, this.handleClick);
2020-02-18 07:07:10 +00:00
if (this.state.hideTooltip) {
2020-02-18 07:07:10 +00:00
this.refTooltip.current.hideTooltip();
}
};
afterHide = () => {
if (!this.state.hideTooltip) {
handleAnyClick(false, this.handleClick);
}
};
handleClick = (e) => {
if (!this.ref.current.contains(e.target)) {
this.refTooltip.current.hideTooltip();
}
};
componentWillUnmount() {
handleAnyClick(false, this.handleClick);
}
onClick = () => {
this.setState({ hideTooltip: false });
};
render() {
const {
tooltipContent,
tooltipProps,
place,
offsetTop,
offsetBottom,
offsetRight,
offsetLeft,
iconName,
color,
getContent,
className,
dataTip,
2022-03-24 17:56:51 +00:00
tooltipMaxWidth,
style,
size,
} = this.props;
return (
<div ref={this.ref} style={style}>
2023-01-23 08:24:41 +00:00
<IconButton
theme={this.props.theme}
id={this.id}
className={`${className} help-icon`}
isClickable={true}
iconName={iconName}
size={size}
color={color}
data-for={this.id}
dataTip={dataTip}
onClick={this.onClick}
/>
2020-02-18 07:07:10 +00:00
{getContent ? (
<Tooltip
tooltipProps={tooltipProps}
2022-01-24 15:27:24 +00:00
theme={this.props.theme}
2020-02-18 07:07:10 +00:00
id={this.id}
reference={this.refTooltip}
effect="solid"
place={place}
offsetTop={offsetTop}
offsetBottom={offsetBottom}
offsetRight={offsetRight}
offsetLeft={offsetLeft}
afterShow={this.afterShow}
afterHide={this.afterHide}
getContent={getContent}
2022-03-24 17:56:51 +00:00
maxWidth={tooltipMaxWidth}
{...tooltipProps}
2020-02-18 07:07:10 +00:00
/>
) : (
2020-02-18 07:07:10 +00:00
<Tooltip
2022-01-24 15:27:24 +00:00
theme={this.props.theme}
2020-02-18 07:07:10 +00:00
id={this.id}
reference={this.refTooltip}
effect="solid"
place={place}
offsetRight={offsetRight}
offsetLeft={offsetLeft}
afterShow={this.afterShow}
afterHide={this.afterHide}
2022-04-27 09:25:05 +00:00
maxWidth={tooltipMaxWidth}
{...tooltipProps}
2020-02-18 07:07:10 +00:00
>
{tooltipContent}
</Tooltip>
)}
</div>
);
}
}
HelpButton.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
tooltipContent: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
tooltipProps: PropTypes.object,
offsetRight: PropTypes.number,
offsetLeft: PropTypes.number,
offsetTop: PropTypes.number,
offsetBottom: PropTypes.number,
2022-03-24 17:56:51 +00:00
tooltipMaxWidth: PropTypes.string,
tooltipId: PropTypes.string,
place: PropTypes.string,
iconName: PropTypes.string,
color: PropTypes.string,
dataTip: PropTypes.string,
getContent: PropTypes.func,
/** Accepts class */
className: PropTypes.string,
/** Accepts id */
id: PropTypes.string,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
size: PropTypes.number,
};
HelpButton.defaultProps = {
2023-01-11 15:08:01 +00:00
iconName: InfoReactSvgUrl,
place: "top",
offsetRight: 60,
offsetLeft: 0,
offsetTop: 0,
offsetBottom: 0,
className: "icon-button",
2022-08-05 00:07:29 +00:00
size: 12,
};
export default HelpButton;