Skip to content

Commit 2b7b5c7

Browse files
maximebonhommeMaxime Bonhomme
andauthored
fix: use Text for Address instead of Input (#132)
* Address component should not use an input element to display the address - we are now using Text - removed onclick types as we are not using event argument anymore * changeset patch created * update Address tests * replace input wording by addressContainer for consistency Co-authored-by: Maxime Bonhomme <[email protected]>
1 parent a209db4 commit 2b7b5c7

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

.changeset/tiny-teachers-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/components': patch
3+
---
4+
5+
Address component now uses Text component instead of unnecesary Input

packages/components/src/components/Address/Address.test.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import { jest } from '@jest/globals';
3-
import { render, fireEvent, waitFor } from '@testing-library/react';
3+
import { render, fireEvent, waitFor, getByTestId } from '@testing-library/react';
44

55
import { Address } from './Address';
66

77
/**
88
* We need to mock the Clipboard API by creating a global navigator object.
9-
*
9+
*
1010
* We're assigning an empty function to `writeText`
1111
* as we're only testing if it has been called with specific argument.
1212
*/
@@ -38,20 +38,19 @@ describe('Address copiable prop true', () => {
3838

3939
it('uses writeText from Clipboard API', async () => {
4040
const { container } = render(<Address copiable value='taylorswift.eth' />);
41-
const input = container.querySelector('input') as HTMLElement;
41+
const addressContainer = getByTestId(container, 'address-container');
4242

4343
jest.spyOn(navigator.clipboard, 'writeText');
4444

45-
fireEvent.click(input);
45+
fireEvent.click(addressContainer);
4646

4747
await waitFor(() => {
4848
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('taylorswift.eth');
4949
});
5050
});
51-
51+
5252
it('checks the length of the address when shortened', () => {
5353
const { container } = render(<Address value='0x00000000000000' shortened />);
54-
const addressInput = container.querySelector('input') as HTMLInputElement;
55-
expect(addressInput).toHaveValue('0x00...0000');
54+
expect(container.textContent).toContain('0x00...0000');
5655
});
5756
});

packages/components/src/components/Address/Address.tsx

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
FormControl,
3-
FormErrorMessage,
4-
Input,
5-
InputGroup,
6-
InputRightElement,
7-
} from '@chakra-ui/react';
1+
import { Box, Flex, FormControl, FormErrorMessage, Text } from '@chakra-ui/react';
82
import { CopyIcon, CheckIcon } from '@chakra-ui/icons';
93
import React, { useState, useEffect } from 'react';
104

@@ -23,38 +17,26 @@ export interface AddressProps {
2317
shortened?: boolean;
2418
}
2519

26-
interface EventTarget {
27-
value: string;
28-
}
29-
30-
interface SyntheticEvent {
31-
currentTarget: EventTarget;
32-
}
33-
3420
/**
3521
* A component to display an address
3622
*/
3723
export const Address: React.FC<AddressProps> = ({ value, copiable = false, shortened = false }) => {
3824
const [error, setError] = useState<null | string>(null);
3925
const [copied, setCopied] = useState<boolean>(false);
4026
let feedbackTimeOut: ReturnType<typeof setTimeout>;
41-
let displayAddress: string;
27+
let displayAddress: string = value;
4228

4329
if (shortened) {
44-
if (value.includes('.eth')) {
45-
displayAddress = value;
46-
} else if (value === '' || value === 'Not connected') {
30+
if (value.includes('.eth') || value === '' || value === 'Not connected') {
4731
displayAddress = value;
4832
} else {
4933
displayAddress = `${value.substring(0, 4)}...${value.substring(
5034
value.length - 4
5135
)}`.toLowerCase();
5236
}
53-
} else {
54-
displayAddress = value;
5537
}
5638

57-
const handleClick = async (event: SyntheticEvent): Promise<void> => {
39+
const handleClick = async (): Promise<void> => {
5840
if (copiable && value) {
5941
try {
6042
await navigator.clipboard.writeText(value);
@@ -76,20 +58,19 @@ export const Address: React.FC<AddressProps> = ({ value, copiable = false, short
7658

7759
return (
7860
<FormControl isInvalid={!!error}>
79-
<InputGroup>
61+
<Flex
62+
data-testid='address-container'
63+
alignItems='center'
64+
cursor={copiable ? 'pointer' : 'initial'}
65+
onClick={handleClick}
66+
>
67+
<Text>{displayAddress}</Text>
8068
{copiable && (
81-
<InputRightElement
82-
pointerEvents='none'
83-
children={copied ? <CheckIcon color='green.500' /> : <CopyIcon color='gray.300' />}
84-
/>
69+
<Box ml='auto'>
70+
{copied ? <CheckIcon color='green.500' /> : <CopyIcon color='gray.300' />}
71+
</Box>
8572
)}
86-
<Input
87-
onClick={handleClick}
88-
value={displayAddress}
89-
cursor={copiable ? 'pointer' : 'initial'}
90-
readOnly
91-
/>
92-
</InputGroup>
73+
</Flex>
9374
<FormErrorMessage>{error}</FormErrorMessage>
9475
</FormControl>
9576
);

0 commit comments

Comments
 (0)