Skip to content

Commit a916c19

Browse files
authored
Add updated props for address component (#2833)
Update props of address component to get it work in more flexible way. Integration PR for `metamask-extension`: MetaMask/metamask-extension#27798 Related ticket: #2758
1 parent a4e654a commit a916c19

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "j2iSyKR8FljepPZvGpP82aJlN4lq/LbCOmEkj18F8OM=",
10+
"shasum": "zA6fni0b6B+ELhkMRiw6vcAgKj1/9A/Rm+dxkPY/oxM=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "SHtsXe9bzVAg3N1wepgK06JIiZvDKoKxC+Acys6fzY4=",
10+
"shasum": "diJJzn5l3lSAKQvHldlYmQXjTcO/IDnLOnxH7kGmkW0=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-sdk/src/jsx/components/Address.test.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,26 @@ describe('Address', () => {
1414
},
1515
});
1616
});
17+
18+
it('renders an address with customized props', () => {
19+
const result = (
20+
<Address
21+
address="0x1234567890123456789012345678901234567890"
22+
truncate={true}
23+
displayName={true}
24+
avatar={false}
25+
/>
26+
);
27+
28+
expect(result).toStrictEqual({
29+
type: 'Address',
30+
key: null,
31+
props: {
32+
address: '0x1234567890123456789012345678901234567890',
33+
truncate: true,
34+
displayName: true,
35+
avatar: false,
36+
},
37+
});
38+
});
1739
});

packages/snaps-sdk/src/jsx/components/Address.ts

+13
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import { createSnapComponent } from '../component';
77
*
88
* @property address - The (Ethereum) address to display. This should be a
99
* valid Ethereum address, starting with `0x`, or a valid CAIP-10 address.
10+
* @property truncate - Whether to truncate the address. Defaults to `true`.
11+
* @property displayName - Whether to show the account name. Defaults to `false`.
12+
* @property avatar - Whether to show the address avatar. Defaults to `true`.
1013
*/
1114
export type AddressProps = {
1215
address: `0x${string}` | CaipAccountId;
16+
truncate?: boolean | undefined;
17+
displayName?: boolean | undefined;
18+
avatar?: boolean | undefined;
1319
};
1420

1521
const TYPE = 'Address';
@@ -22,13 +28,20 @@ const TYPE = 'Address';
2228
* @param props - The props of the component.
2329
* @param props.address - The address to display. This should be a
2430
* valid Ethereum address, starting with `0x`, or a valid CAIP-10 address.
31+
* @param props.truncate - Whether to truncate the address. Defaults to `true`.
32+
* @param props.displayName - Whether to show the account name. Defaults to `false`.
33+
* @param props.avatar - Whether to show the address avatar. Defaults to `true`.
2534
* @returns An address element.
2635
* @example
2736
* <Address address="0x1234567890123456789012345678901234567890" />
2837
* @example
2938
* <Address address="eip155:1:0x1234567890123456789012345678901234567890" />
3039
* @example
3140
* <Address address="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6" />
41+
* @example
42+
* <Address address="0x1234567890123456789012345678901234567890" truncate={false} avatar={false} />
43+
* @example
44+
* <Address address="0x1234567890123456789012345678901234567890" displayName={true} />
3245
*/
3346
export const Address = createSnapComponent<AddressProps, typeof TYPE>(TYPE);
3447

packages/snaps-sdk/src/jsx/validation.test.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ describe('AddressStruct', () => {
492492
<Address address="eip155:1:0x1234567890abcdef1234567890abcdef12345678" />,
493493
<Address address="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6" />,
494494
<Address address="cosmos:cosmoshub-3:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0" />,
495+
<Address
496+
address="0x1234567890abcdef1234567890abcdef12345678"
497+
truncate={false}
498+
avatar={false}
499+
/>,
500+
<Address
501+
address="0x1234567890abcdef1234567890abcdef12345678"
502+
displayName={true}
503+
/>,
504+
<Address
505+
address="0x1234567890abcdef1234567890abcdef12345678"
506+
displayName={true}
507+
avatar={false}
508+
/>,
509+
<Address
510+
address="0x1234567890abcdef1234567890abcdef12345678"
511+
truncate={true}
512+
displayName={false}
513+
avatar={true}
514+
/>,
495515
])('validates an address element', (value) => {
496516
expect(is(value, AddressStruct)).toBe(true);
497517
});
@@ -518,6 +538,21 @@ describe('AddressStruct', () => {
518538
<Row label="label">
519539
<Image src="<svg />" alt="alt" />
520540
</Row>,
541+
<Address
542+
address="0x1234567890abcdef1234567890abcdef12345678"
543+
// @ts-expect-error - Invalid props.
544+
truncate="wrong-prop"
545+
/>,
546+
<Address
547+
address="0x1234567890abcdef1234567890abcdef12345678"
548+
// @ts-expect-error - Invalid props.
549+
displayName="false"
550+
/>,
551+
<Address
552+
address="0x1234567890abcdef1234567890abcdef12345678"
553+
// @ts-expect-error - Invalid props.
554+
avatar="wrong-prop"
555+
/>,
521556
])('does not validate "%p"', (value) => {
522557
expect(is(value, AddressStruct)).toBe(false);
523558
});

packages/snaps-sdk/src/jsx/validation.ts

+3
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ export const FormattingStruct: Describe<StandardFormattingElement> = typedUnion(
543543
*/
544544
export const AddressStruct: Describe<AddressElement> = element('Address', {
545545
address: nullUnion([HexChecksumAddressStruct, CaipAccountIdStruct]),
546+
truncate: optional(boolean()),
547+
displayName: optional(boolean()),
548+
avatar: optional(boolean()),
546549
});
547550

548551
/**

0 commit comments

Comments
 (0)