Skip to content
43 changes: 18 additions & 25 deletions src/components/CopyTextToClipboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useCallback} from 'react';
import PropTypes from 'prop-types';
import * as Expensicons from './Icon/Expensicons';
import PressableWithDelayToggle from './PressableWithDelayToggle';
Expand All @@ -20,32 +20,25 @@ const defaultProps = {
textStyles: [],
};

class CopyTextToClipboard extends React.Component {
constructor(props) {
super(props);

this.copyToClipboard = this.copyToClipboard.bind(this);
}

copyToClipboard() {
Clipboard.setString(this.props.text);
}

render() {
return (
<PressableWithDelayToggle
text={this.props.text}
tooltipText={this.props.translate('reportActionContextMenu.copyToClipboard')}
tooltipTextChecked={this.props.translate('reportActionContextMenu.copied')}
icon={Expensicons.Copy}
textStyles={this.props.textStyles}
onPress={this.copyToClipboard}
/>
);
}
}
const CopyTextToClipboard = (props) => {
const copyToClipboard = useCallback(() => {
Clipboard.setString(props.text);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nhminhduc @PauloGasparSv Do you guys agree if we wrap this with useCallback since it has dependencies of props.text?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree

}, [props.text]);

return (
<PressableWithDelayToggle
text={props.text}
tooltipText={props.translate('reportActionContextMenu.copyToClipboard')}
tooltipTextChecked={props.translate('reportActionContextMenu.copied')}
icon={Expensicons.Copy}
textStyles={props.textStyles}
onPress={copyToClipboard}
/>
);
};

CopyTextToClipboard.propTypes = propTypes;
CopyTextToClipboard.defaultProps = defaultProps;
CopyTextToClipboard.displayName = 'CopyTextToClipboard';

export default withLocalize(CopyTextToClipboard);