From 99bbff837208229dfc932839d86a2bea47b605d7 Mon Sep 17 00:00:00 2001 From: Duc Nguyen Date: Sat, 3 Jun 2023 01:42:56 +0300 Subject: [PATCH] migrate CopyTextToClipboard to functional component --- src/components/CopyTextToClipboard.js | 77 ++++++++++++--------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/src/components/CopyTextToClipboard.js b/src/components/CopyTextToClipboard.js index fbf46be3ee08..bfa6c8a0af43 100644 --- a/src/components/CopyTextToClipboard.js +++ b/src/components/CopyTextToClipboard.js @@ -1,5 +1,5 @@ -import React from 'react'; -import {Pressable} from 'react-native'; +import React, { useCallback } from 'react'; +import { Pressable } from 'react-native'; import PropTypes from 'prop-types'; import Text from './Text'; import * as Expensicons from './Icon/Expensicons'; @@ -11,15 +11,14 @@ import Tooltip from './Tooltip'; import styles from '../styles/styles'; import * as StyleUtils from '../styles/StyleUtils'; import variables from '../styles/variables'; -import withLocalize, {withLocalizePropTypes} from './withLocalize'; -import withDelayToggleButtonState, {withDelayToggleButtonStatePropTypes} from './withDelayToggleButtonState'; +import withLocalize, { withLocalizePropTypes } from './withLocalize'; +import withDelayToggleButtonState, { withDelayToggleButtonStatePropTypes } from './withDelayToggleButtonState'; const propTypes = { /** The text to display and copy to the clipboard */ text: PropTypes.string.isRequired, /** Styles to apply to the text */ - // eslint-disable-next-line react/forbid-prop-types textStyles: PropTypes.arrayOf(PropTypes.object), ...withLocalizePropTypes, @@ -31,48 +30,40 @@ const defaultProps = { textStyles: [], }; -class CopyTextToClipboard extends React.Component { - constructor(props) { - super(props); - - this.copyToClipboard = this.copyToClipboard.bind(this); - } +CopyTextToClipboard.propTypes = propTypes; +CopyTextToClipboard.defaultProps = defaultProps; - copyToClipboard() { - if (this.props.isDelayButtonStateComplete) { +const CopyTextToClipboard = ({ text, textStyles, translate, isDelayButtonStateComplete, toggleDelayButtonState }) => { + const copyToClipboard = useCallback(() => { + if (isDelayButtonStateComplete) { return; } - Clipboard.setString(this.props.text); - this.props.toggleDelayButtonState(true); - } + Clipboard.setString(text); + toggleDelayButtonState(true); + }, [isDelayButtonStateComplete, text, toggleDelayButtonState]); - render() { - return ( - - {`${this.props.text} `} - - - {({hovered, pressed}) => ( - - )} - - - - ); - } + return ( + + {`${text} `} + + + {({ hovered, pressed }) => ( + + )} + + + + ); } -CopyTextToClipboard.propTypes = propTypes; -CopyTextToClipboard.defaultProps = defaultProps; - export default compose(withLocalize, withDelayToggleButtonState)(CopyTextToClipboard);