Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updated props for address component #2833

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "j2iSyKR8FljepPZvGpP82aJlN4lq/LbCOmEkj18F8OM=",
"shasum": "zA6fni0b6B+ELhkMRiw6vcAgKj1/9A/Rm+dxkPY/oxM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "SHtsXe9bzVAg3N1wepgK06JIiZvDKoKxC+Acys6fzY4=",
"shasum": "diJJzn5l3lSAKQvHldlYmQXjTcO/IDnLOnxH7kGmkW0=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
22 changes: 22 additions & 0 deletions packages/snaps-sdk/src/jsx/components/Address.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,26 @@ describe('Address', () => {
},
});
});

it('renders an address with customized props', () => {
const result = (
<Address
address="0x1234567890123456789012345678901234567890"
truncate={true}
displayName={true}
avatar={false}
/>
);

expect(result).toStrictEqual({
type: 'Address',
key: null,
props: {
address: '0x1234567890123456789012345678901234567890',
truncate: true,
displayName: true,
avatar: false,
},
});
});
});
13 changes: 13 additions & 0 deletions packages/snaps-sdk/src/jsx/components/Address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { createSnapComponent } from '../component';
*
* @property address - The (Ethereum) address to display. This should be a
* valid Ethereum address, starting with `0x`, or a valid CAIP-10 address.
* @property truncate - Whether to truncate the address. Defaults to `true`.
* @property displayName - Whether to show the account name. Defaults to `false`.
* @property avatar - Whether to show the address avatar. Defaults to `true`.
*/
GuillaumeRx marked this conversation as resolved.
Show resolved Hide resolved
export type AddressProps = {
address: `0x${string}` | CaipAccountId;
truncate?: boolean | undefined;
displayName?: boolean | undefined;
avatar?: boolean | undefined;
};

const TYPE = 'Address';
Expand All @@ -22,13 +28,20 @@ const TYPE = 'Address';
* @param props - The props of the component.
* @param props.address - The address to display. This should be a
* valid Ethereum address, starting with `0x`, or a valid CAIP-10 address.
* @param props.truncate - Whether to truncate the address. Defaults to `true`.
* @param props.displayName - Whether to show the account name. Defaults to `false`.
* @param props.avatar - Whether to show the address avatar. Defaults to `true`.
* @returns An address element.
* @example
* <Address address="0x1234567890123456789012345678901234567890" />
* @example
* <Address address="eip155:1:0x1234567890123456789012345678901234567890" />
* @example
* <Address address="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6" />
* @example
* <Address address="0x1234567890123456789012345678901234567890" truncate={false} avatar={false} />
* @example
* <Address address="0x1234567890123456789012345678901234567890" displayName={true} />
*/
export const Address = createSnapComponent<AddressProps, typeof TYPE>(TYPE);

Expand Down
35 changes: 35 additions & 0 deletions packages/snaps-sdk/src/jsx/validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,26 @@ describe('AddressStruct', () => {
<Address address="eip155:1:0x1234567890abcdef1234567890abcdef12345678" />,
<Address address="bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6" />,
<Address address="cosmos:cosmoshub-3:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0" />,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
truncate={false}
avatar={false}
/>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
displayName={true}
/>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
displayName={true}
avatar={false}
/>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
truncate={true}
displayName={false}
avatar={true}
GuillaumeRx marked this conversation as resolved.
Show resolved Hide resolved
/>,
])('validates an address element', (value) => {
expect(is(value, AddressStruct)).toBe(true);
});
Expand All @@ -518,6 +538,21 @@ describe('AddressStruct', () => {
<Row label="label">
<Image src="<svg />" alt="alt" />
</Row>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
// @ts-expect-error - Invalid props.
truncate="wrong-prop"
/>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
// @ts-expect-error - Invalid props.
displayName="false"
/>,
<Address
address="0x1234567890abcdef1234567890abcdef12345678"
// @ts-expect-error - Invalid props.
avatar="wrong-prop"
/>,
])('does not validate "%p"', (value) => {
expect(is(value, AddressStruct)).toBe(false);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ export const FormattingStruct: Describe<StandardFormattingElement> = typedUnion(
*/
export const AddressStruct: Describe<AddressElement> = element('Address', {
address: nullUnion([HexChecksumAddressStruct, CaipAccountIdStruct]),
truncate: optional(boolean()),
displayName: optional(boolean()),
avatar: optional(boolean()),
GuillaumeRx marked this conversation as resolved.
Show resolved Hide resolved
});

/**
Expand Down
Loading