Created
June 9, 2020 08:15
-
-
Save tandat2209/3ff49e863792a4904c3018a8bc66c81e to your computer and use it in GitHub Desktop.
OverflowTooltip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactResizeDetector from 'react-resize-detector'; | |
import PropTypes from 'prop-types'; | |
import ReactTooltip from 'react-tooltip'; | |
export default class OverflowTooltip extends React.Component { | |
constructor(props) { | |
super(props); | |
this.node = null; | |
this.onResize = this.onResize.bind(this); | |
} | |
onResize() { | |
if (this.node.offsetWidth < this.node.scrollWidth) { | |
this.node.setAttribute('data-tip', this.props.children); | |
} else if (this.node.hasAttribute('data-tip')) { | |
this.node.removeAttribute('data-tip'); | |
} | |
ReactTooltip.rebuild(); | |
} | |
render() { | |
const { children, element, ...others } = this.props; | |
const Container = element || 'div'; | |
return ( | |
<Container | |
{...others} | |
ref={node => { | |
this.node = node; | |
}} | |
> | |
{children} | |
<ReactResizeDetector | |
handleWidth | |
handleHeight | |
onResize={this.onResize} | |
/> | |
</Container> | |
); | |
} | |
} | |
OverflowTooltip.propTypes = { | |
children: PropTypes.string.isRequired, | |
element: PropTypes.string | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment