2017-12-05 03:43:08 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Tippy from 'tippy.js';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import cx from 'classnames';
|
2018-01-12 01:05:32 +08:00
|
|
|
import { ESCAPE } from '/imports/utils/keyCodes';
|
2017-12-05 03:43:08 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2017-12-08 00:11:34 +08:00
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
position: PropTypes.oneOf(['bottom']),
|
|
|
|
children: PropTypes.element.isRequired,
|
2017-12-12 02:47:34 +08:00
|
|
|
className: PropTypes.string,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
2017-12-08 00:11:34 +08:00
|
|
|
position: 'bottom',
|
2017-12-12 02:47:34 +08:00
|
|
|
className: null,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Tooltip extends Component {
|
2018-03-29 21:40:50 +08:00
|
|
|
static wait(show, event) {
|
|
|
|
const tooltipTarget = event.target;
|
|
|
|
const expandedEl = tooltipTarget.parentElement.querySelector('[aria-expanded="true"]');
|
|
|
|
const isTarget = expandedEl === tooltipTarget;
|
|
|
|
if (expandedEl && !isTarget) return;
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-12-08 00:11:34 +08:00
|
|
|
this.tippySelectorId = _.uniqueId('tippy-');
|
2017-12-05 03:43:08 +08:00
|
|
|
this.onShow = this.onShow.bind(this);
|
2018-01-12 01:05:32 +08:00
|
|
|
this.onHide = this.onHide.bind(this);
|
|
|
|
this.handleEscapeHide = this.handleEscapeHide.bind(this);
|
2018-01-11 01:07:40 +08:00
|
|
|
this.delay = [150, 50];
|
2017-12-12 02:47:34 +08:00
|
|
|
this.dynamicTitle = true;
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const {
|
2017-12-08 00:11:34 +08:00
|
|
|
position,
|
2017-12-05 03:43:08 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
position,
|
2017-12-12 02:47:34 +08:00
|
|
|
dynamicTitle: this.dynamicTitle,
|
2017-12-08 21:45:25 +08:00
|
|
|
delay: this.delay,
|
2017-12-05 03:43:08 +08:00
|
|
|
onShow: this.onShow,
|
2018-01-12 01:05:32 +08:00
|
|
|
onHide: this.onHide,
|
2018-03-29 21:40:50 +08:00
|
|
|
wait: Tooltip.wait,
|
2018-01-11 03:45:12 +08:00
|
|
|
touchHold: true,
|
2017-12-05 03:43:08 +08:00
|
|
|
};
|
2017-12-08 00:11:34 +08:00
|
|
|
this.tooltip = Tippy(`#${this.tippySelectorId}`, options);
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
|
|
|
onShow() {
|
2018-01-12 01:05:32 +08:00
|
|
|
document.addEventListener('keyup', this.handleEscapeHide);
|
2017-12-05 03:43:08 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 01:05:32 +08:00
|
|
|
onHide() {
|
|
|
|
document.removeEventListener('keyup', this.handleEscapeHide);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEscapeHide(e) {
|
|
|
|
if (e.keyCode !== ESCAPE) return;
|
|
|
|
this.tooltip.tooltips[0].hide();
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:43:08 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
title,
|
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const WrappedComponent = React.Children.only(children);
|
|
|
|
|
|
|
|
const WrappedComponentBound = React.cloneElement(WrappedComponent, {
|
|
|
|
...restProps,
|
|
|
|
title,
|
2017-12-08 00:11:34 +08:00
|
|
|
id: this.tippySelectorId,
|
2017-12-05 03:43:08 +08:00
|
|
|
className: cx(children.props.className, className),
|
|
|
|
});
|
|
|
|
|
|
|
|
return WrappedComponentBound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Tooltip;
|
|
|
|
|
|
|
|
Tooltip.defaultProps = defaultProps;
|
|
|
|
Tooltip.propTypes = propTypes;
|