From 3abfd7783593fc9492732390997e60a3fcaa3ef9 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Sun, 6 Aug 2023 21:04:31 -0500 Subject: [PATCH] Convert CopyableInput to a function component. --- .../modules/IDE/components/CopyableInput.jsx | 138 +++++++++--------- 1 file changed, 67 insertions(+), 71 deletions(-) diff --git a/client/modules/IDE/components/CopyableInput.jsx b/client/modules/IDE/components/CopyableInput.jsx index 1fa9f6bec3..2d7b5036f2 100644 --- a/client/modules/IDE/components/CopyableInput.jsx +++ b/client/modules/IDE/components/CopyableInput.jsx @@ -1,95 +1,91 @@ import PropTypes from 'prop-types'; -import React from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import Clipboard from 'clipboard'; import classNames from 'classnames'; -import { withTranslation } from 'react-i18next'; +import { useTranslation } from 'react-i18next'; import ShareIcon from '../../../images/share.svg'; -class CopyableInput extends React.Component { - constructor(props) { - super(props); - this.onMouseLeaveHandler = this.onMouseLeaveHandler.bind(this); - } +const CopyableInput = ({ label, value, hasPreviewLink }) => { + const { t } = useTranslation(); - componentDidMount() { - this.clipboard = new Clipboard(this.input, { - target: () => this.input - }); + const [isCopied, setIsCopied] = useState(false); - this.clipboard.on('success', (e) => { - this.tooltip.classList.add('tooltipped'); - this.tooltip.classList.add('tooltipped-n'); - }); - } + const inputRef = useRef(null); - componentWillUnmount() { - this.clipboard.destroy(); - } + useEffect(() => { + const input = inputRef.current; - onMouseLeaveHandler() { - this.tooltip.classList.remove('tooltipped'); - this.tooltip.classList.remove('tooltipped-n'); - } + if (!input) return; // should never happen - render() { - const { label, value, hasPreviewLink } = this.props; - const copyableInputClass = classNames({ - 'copyable-input': true, - 'copyable-input--with-preview': hasPreviewLink + const clipboard = new Clipboard(input, { + target: () => input }); - return ( -
-
{ - this.tooltip = element; - }} - onMouseLeave={this.onMouseLeaveHandler} - > - -
- {hasPreviewLink && ( - - + + clipboard.on('success', () => { + setIsCopied(true); + }); + + // eslint-disable-next-line consistent-return + return () => { + clipboard.destroy(); + }; + }, [inputRef, setIsCopied]); + + return ( +
+
setIsCopied(false)} + > +
- ); - } -} + {hasPreviewLink && ( + + + )} +
+ ); +}; CopyableInput.propTypes = { label: PropTypes.string.isRequired, value: PropTypes.string.isRequired, - hasPreviewLink: PropTypes.bool, - t: PropTypes.func.isRequired + hasPreviewLink: PropTypes.bool }; CopyableInput.defaultProps = { hasPreviewLink: false }; -export default withTranslation()(CopyableInput); +export default CopyableInput;