Skip to content

Commit 2d6575e

Browse files
authored
Add support for Address in Card title. (#2894)
This PR adds support for the `Address` component as a title in the `Card` component.
1 parent 0b19875 commit 2d6575e

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
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": "LciQX9cjRXeSErEsKRm9OEFW8jAq9doqaa9b/TfGN3Y=",
10+
"shasum": "8LxymXn6+X9URWzkmurIZEyCypzF3OUm53FLjlNW0/I=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 1 addition & 1 deletion
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": "83SohbQ4Vp/wI2lFXL6tyuvWy7bLcRSL3yZikZEZrAg=",
10+
"shasum": "hYGGCiIVhwOlDnwIyfpkscAd5bc2kVAyzXMq3UC6ORQ=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Address } from './Address';
12
import { Card } from './Card';
23

34
describe('Card', () => {
@@ -24,4 +25,42 @@ describe('Card', () => {
2425
},
2526
});
2627
});
28+
29+
it('renders a card with an address as title', () => {
30+
const result = (
31+
<Card
32+
image="<svg />"
33+
title={
34+
<Address
35+
address="0x1234567890123456789012345678901234567890"
36+
displayName
37+
avatar={false}
38+
/>
39+
}
40+
description="Description"
41+
value="$1200"
42+
extra="0.12 ETH"
43+
/>
44+
);
45+
46+
expect(result).toStrictEqual({
47+
type: 'Card',
48+
key: null,
49+
props: {
50+
image: '<svg />',
51+
title: {
52+
key: null,
53+
props: {
54+
address: '0x1234567890123456789012345678901234567890',
55+
avatar: false,
56+
displayName: true,
57+
},
58+
type: 'Address',
59+
},
60+
description: 'Description',
61+
value: '$1200',
62+
extra: '0.12 ETH',
63+
},
64+
});
65+
});
2766
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSnapComponent } from '../component';
2+
import type { AddressElement } from './Address';
23

34
/**
45
* The props of the {@link Card} component.
@@ -11,7 +12,7 @@ import { createSnapComponent } from '../component';
1112
*/
1213
export type CardProps = {
1314
image?: string | undefined;
14-
title: string;
15+
title: string | AddressElement;
1516
description?: string | undefined;
1617
value: string;
1718
extra?: string | undefined;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,19 @@ describe('CardStruct', () => {
701701
value="$1200"
702702
extra="0.12 ETH"
703703
/>,
704+
<Card
705+
image="<svg />"
706+
title={
707+
<Address
708+
address="0x1234567890123456789012345678901234567890"
709+
displayName
710+
avatar={false}
711+
/>
712+
}
713+
description="Description"
714+
value="$1200"
715+
extra="0.12 ETH"
716+
/>,
704717
])('validates a card element', (value) => {
705718
expect(is(value, CardStruct)).toBe(true);
706719
});

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,27 @@ export const DropdownStruct: Describe<DropdownElement> = element('Dropdown', {
339339
children: children([OptionStruct]),
340340
});
341341

342+
/**
343+
* A struct for the {@link AddressElement} type.
344+
*/
345+
export const AddressStruct: Describe<AddressElement> = element('Address', {
346+
address: nullUnion([HexChecksumAddressStruct, CaipAccountIdStruct]),
347+
truncate: optional(boolean()),
348+
displayName: optional(boolean()),
349+
avatar: optional(boolean()),
350+
});
351+
342352
/**
343353
* A struct for the {@link CardElement} type.
344354
*/
345355
export const CardStruct: Describe<CardElement> = element('Card', {
346356
image: optional(string()),
347-
title: string(),
357+
title: selectiveUnion((value) => {
358+
if (typeof value === 'object') {
359+
return AddressStruct;
360+
}
361+
return string();
362+
}),
348363
description: optional(string()),
349364
value: string(),
350365
extra: optional(string()),
@@ -538,16 +553,6 @@ export const FormattingStruct: Describe<StandardFormattingElement> = typedUnion(
538553
[BoldStruct, ItalicStruct],
539554
);
540555

541-
/**
542-
* A struct for the {@link AddressElement} type.
543-
*/
544-
export const AddressStruct: Describe<AddressElement> = element('Address', {
545-
address: nullUnion([HexChecksumAddressStruct, CaipAccountIdStruct]),
546-
truncate: optional(boolean()),
547-
displayName: optional(boolean()),
548-
avatar: optional(boolean()),
549-
});
550-
551556
/**
552557
* A struct for the {@link AvatarElement} type.
553558
*/

0 commit comments

Comments
 (0)