diff --git a/src/components/TextEllipsis/index.jsx b/src/components/TextEllipsis/index.jsx index d2e3a1795..42da83146 100644 --- a/src/components/TextEllipsis/index.jsx +++ b/src/components/TextEllipsis/index.jsx @@ -4,66 +4,42 @@ import React from 'react'; import Popover from '../Popover'; import './styles.scss'; -class TextEllipsisComponent extends React.PureComponent { - static propTypes = { - children: PropTypes.node.isRequired, - /** - * Can use `placement` and `trigger` props from Popover to control popover. - */ - popoverProps: PropTypes.shape(_.pick(Popover.propTypes, ['placement', 'trigger'])), - }; +const TextEllipsis = ({ popoverProps, children }) => { + const containerRef = React.useRef(); + const [truncated, setTruncated] = React.useState(false); - static defaultProps = { - popoverProps: { - placement: 'top', - trigger: 'hover', - }, - }; + React.useLayoutEffect(() => { + const nextTruncateState = containerRef.current.scrollWidth > containerRef.current.clientWidth; - constructor(props) { - super(props); - - this.container = React.createRef(); - } - - state = { - truncated: false, - }; - - componentDidMount() { - this.setTruncate(); - } - - componentDidUpdate() { - this.setTruncate(); - } - - setTruncate() { - const nextTruncateState = this.container.current.scrollWidth > this.container.current.clientWidth; - if (this.state.truncated !== nextTruncateState) { - this.setState({ - truncated: nextTruncateState, - }); + if (truncated !== nextTruncateState) { + setTruncated(nextTruncateState); } - } - - render() { - const { popoverProps } = this.props; - const { truncated } = this.state; - - return ( - -
- {this.props.children} -
-
- ); - } -} - -export default TextEllipsisComponent; + }, [truncated]); + + return ( + +
+ {children} +
+
+ ); +}; + +TextEllipsis.propTypes = { + children: PropTypes.node.isRequired, + popoverProps: PropTypes.shape(_.pick(Popover.propTypes, ['placement', 'trigger'])), +}; + +TextEllipsis.defaultProps = { + popoverProps: { + placement: 'top', + trigger: 'hover', + }, +}; + +export default TextEllipsis;