File tree Expand file tree Collapse file tree 3 files changed +48
-5
lines changed
packages/components/src/components/Address Expand file tree Collapse file tree 3 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -11,13 +11,24 @@ export default {
11
11
12
12
export const Default = ( ) => < Address value = 'testaddress.eth' /> ;
13
13
14
- const AddressUsingProvider = ( ) => {
14
+ export const DefaultShortenedWithENS = ( ) => < Address shortened value = 'testaddress.eth' /> ;
15
+
16
+ export const DefaultShortenedWithHexAddress = ( ) => (
17
+ < Address shortened value = '0x7Be8076f4EA4A4AD08075C2508e481d6C946D12b' />
18
+ ) ;
19
+
20
+ type AddressProps = {
21
+ shortened ?: boolean ;
22
+ } ;
23
+
24
+ const AddressUsingProvider = ( props : AddressProps ) => {
15
25
const { connected, connectWallet, connection } = useWallet ( ) ;
16
26
17
27
return (
18
28
< >
19
29
< Address
20
30
value = { connected ? connection . ens || connection . userAddress || '' : 'Not connected' }
31
+ shortened = { props . shortened }
21
32
/>
22
33
< Button onClick = { connectWallet } > Connect wallet</ Button >
23
34
</ >
@@ -29,3 +40,9 @@ export const WithWallet = () => (
29
40
< AddressUsingProvider />
30
41
</ Provider >
31
42
) ;
43
+
44
+ export const WithWalletShortened = ( ) => (
45
+ < Provider network = { NETWORKS . rinkeby } >
46
+ < AddressUsingProvider shortened />
47
+ </ Provider >
48
+ ) ;
Original file line number Diff line number Diff line change @@ -5,7 +5,13 @@ import { Address } from './Address';
5
5
6
6
describe ( 'Address' , ( ) => {
7
7
it ( 'renders without throwing' , ( ) => {
8
- const { container } = render ( < Address value = " taylorswift.eth" /> ) ;
8
+ const { container } = render ( < Address value = ' taylorswift.eth' shortened = { false } /> ) ;
9
9
expect ( container ) . toBeInTheDocument ( ) ;
10
10
} ) ;
11
+
12
+ it ( 'checks the length of the address when shortened' , ( ) => {
13
+ const { container } = render ( < Address value = '0x00000000000000' shortened /> ) ;
14
+ const addressInput = container . querySelector ( 'input' ) as HTMLInputElement ;
15
+ expect ( addressInput ) . toHaveValue ( '0x00...0000' ) ;
16
+ } ) ;
11
17
} ) ;
Original file line number Diff line number Diff line change 1
1
import { Input } from '@chakra-ui/react' ;
2
- import React from 'react' ;
2
+ import React , { FC } from 'react' ;
3
3
4
4
export interface AddressProps {
5
5
/**
6
6
* The address to display
7
7
*/
8
8
value : string ;
9
+ /**
10
+ * Shorten the address if it does not resolve to an ENS name
11
+ */
12
+ shortened ?: boolean ;
9
13
}
10
14
11
15
/**
12
16
* A component to display an address
13
17
*/
14
- export const Address : React . FC < AddressProps > = ( { value } ) => {
15
- return < Input value = { value } /> ;
18
+ export const Address : FC < AddressProps > = ( { value, shortened = false } ) => {
19
+ let displayAddress : string ;
20
+
21
+ if ( shortened ) {
22
+ if ( value . includes ( '.eth' ) ) {
23
+ displayAddress = value ;
24
+ } else if ( value === '' || value === 'Not connected' ) {
25
+ displayAddress = value ;
26
+ } else {
27
+ displayAddress = `${ value . substring ( 0 , 4 ) } ...${ value . substring (
28
+ value . length - 4
29
+ ) } `. toLowerCase ( ) ;
30
+ }
31
+ } else {
32
+ displayAddress = value ;
33
+ }
34
+
35
+ return < Input value = { displayAddress } /> ;
16
36
} ;
You can’t perform that action at this time.
0 commit comments