-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add copy button component (#11736)
## **Description** Adding re-usable copy button component and some code cleanup. ## **Related issues** Fixes: #11735 ## **Manual testing steps** 1. Run storybook locally 2. Check copy button component ## **Screenshots/Recordings** <img width="382" alt="Screenshot 2024-10-10 at 4 29 36 PM" src="https://github.com/user-attachments/assets/7d5e08ef-37b8-4236-9d6c-19a2afc3881f"> ## **Pre-merge author checklist** - [X] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
12 changed files
with
135 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
8 changes: 8 additions & 0 deletions
8
app/components/Views/confirmations/components/UI/CopyButton/CopyButton.stories.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react-native'; | ||
|
||
import CopyButton from './CopyButton'; | ||
|
||
storiesOf('Confirmations / CopyButton', module) | ||
.addDecorator((getStory) => getStory()) | ||
.add('Default', () => <CopyButton copyText="DUMMY" />); |
20 changes: 20 additions & 0 deletions
20
app/components/Views/confirmations/components/UI/CopyButton/CopyButton.test.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react-native'; | ||
|
||
import ClipboardManager from '../../../../../../core/ClipboardManager'; | ||
import CopyButton from './CopyButton'; | ||
|
||
jest.mock('../../../../../../core/ClipboardManager'); | ||
|
||
describe('CopyButton', () => { | ||
it('should match snapshot', async () => { | ||
const container = render(<CopyButton copyText={'DUMMY'} />); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('should copy text to clipboard when pressed', async () => { | ||
const { getByTestId } = render(<CopyButton copyText={'DUMMY'} />); | ||
fireEvent.press(getByTestId('copyButtonTestId')); | ||
expect(ClipboardManager.setString).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
app/components/Views/confirmations/components/UI/CopyButton/CopyButton.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
import ButtonIcon, { | ||
ButtonIconSizes, | ||
} from '../../../../../../component-library/components/Buttons/ButtonIcon'; | ||
import ClipboardManager from '../../../../../../core/ClipboardManager'; | ||
import { | ||
IconColor, | ||
IconName, | ||
} from '../../../../../../component-library/components/Icons/Icon'; | ||
|
||
interface CopyButtonProps { | ||
copyText: string; | ||
testID?: string; | ||
} | ||
|
||
const CopyButton = ({ copyText, testID }: CopyButtonProps) => { | ||
const [copied, setCopied] = useState(false); | ||
|
||
const copyMessage = useCallback(async () => { | ||
await ClipboardManager.setString(copyText); | ||
setCopied(true); | ||
}, [copyText, setCopied]); | ||
|
||
return ( | ||
<ButtonIcon | ||
iconColor={IconColor.Alternative} | ||
size={ButtonIconSizes.Sm} | ||
onPress={copyMessage} | ||
iconName={copied ? IconName.CopySuccess : IconName.Copy} | ||
testID={testID ?? 'copyButtonTestId'} | ||
/> | ||
); | ||
}; | ||
|
||
export default CopyButton; |
36 changes: 36 additions & 0 deletions
36
...nents/Views/confirmations/components/UI/CopyButton/__snapshots__/CopyButton.test.tsx.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`CopyButton should match snapshot 1`] = ` | ||
<TouchableOpacity | ||
accessible={true} | ||
activeOpacity={1} | ||
disabled={false} | ||
onPress={[Function]} | ||
onPressIn={[Function]} | ||
onPressOut={[Function]} | ||
style={ | ||
{ | ||
"alignItems": "center", | ||
"borderRadius": 8, | ||
"height": 24, | ||
"justifyContent": "center", | ||
"opacity": 1, | ||
"width": 24, | ||
} | ||
} | ||
testID="copyButtonTestId" | ||
> | ||
<SvgMock | ||
color="#6a737d" | ||
height={16} | ||
name="Copy" | ||
style={ | ||
{ | ||
"height": 16, | ||
"width": 16, | ||
} | ||
} | ||
width={16} | ||
/> | ||
</TouchableOpacity> | ||
`; |
1 change: 1 addition & 0 deletions
1
app/components/Views/confirmations/components/UI/CopyButton/index.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './CopyButton'; |
This file contains 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
This file contains 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