-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
AccountSelector
component (#2764)
This PR adds an `AccountSelector` component. Fixes: #2752
- Loading branch information
1 parent
0014ff6
commit 73d00a0
Showing
7 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/snaps-sdk/src/jsx/components/form/AccountSelector.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { AccountSelector } from './AccountSelector'; | ||
|
||
describe('AccountSelector', () => { | ||
it('returns an account selector element', () => { | ||
const result = ( | ||
<AccountSelector | ||
name="account" | ||
title="From account" | ||
chainId="bip122:p2wpkh" | ||
selectedAddress="bc1qc8dwyqua9elc3mzcxk93c70kjz8tcc92x0a8a6" | ||
/> | ||
); | ||
|
||
expect(result).toStrictEqual({ | ||
type: 'AccountSelector', | ||
props: { | ||
name: 'account', | ||
title: 'From account', | ||
chainId: 'bip122:p2wpkh', | ||
selectedAddress: 'bc1qc8dwyqua9elc3mzcxk93c70kjz8tcc92x0a8a6', | ||
}, | ||
key: null, | ||
}); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
packages/snaps-sdk/src/jsx/components/form/AccountSelector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { CaipAccountAddress, CaipChainId } from '@metamask/utils'; | ||
|
||
import { createSnapComponent } from '../../component'; | ||
|
||
/** | ||
* The props of the {@link AccountSelector} component. | ||
* | ||
* @property name - The name of the account selector. This is used to identify the | ||
* state in the form data. | ||
* @property title - The title of the account selector. This is displayed in the UI. | ||
* @property chainId - The chain ID of the account selector. This should be a valid CAIP-2 chain ID. | ||
* @property selectedAddress - The default selected address of the account selector. This should be a | ||
* valid CAIP-10 account address. | ||
*/ | ||
export type AccountSelectorProps = { | ||
name: string; | ||
title: string; | ||
chainId: CaipChainId; | ||
selectedAddress: CaipAccountAddress; | ||
}; | ||
|
||
const TYPE = 'AccountSelector'; | ||
|
||
/** | ||
* An account selector component, which is used to create an account selector. | ||
* | ||
* This component does not accept any children. | ||
* | ||
* @param props - The props of the component. | ||
* @param props.name - The name of the account selector field. This is used to identify the | ||
* state in the form data. | ||
* @param props.title - The title of the account selector field. This is displayed in the UI. | ||
* @param props.chainId - The chain ID of the account selector. This should be a valid CAIP-2 chain ID. | ||
* @param props.selectedAddress - The selected address of the account selector. This should be a | ||
* valid CAIP-10 account address. | ||
* @returns An account selector element. | ||
* @example | ||
* <AccountSelector name="account" title="From account" chainId="eip155:1" selectedAddress="0x1234567890123456789012345678901234567890" /> | ||
* @example | ||
* <AccountSelector name="account" title="From account" chainId="bip122:000000000019d6689c085ae165831e93" selectedAddress="128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6" /> | ||
*/ | ||
export const AccountSelector = createSnapComponent< | ||
AccountSelectorProps, | ||
typeof TYPE | ||
>(TYPE); | ||
|
||
/** | ||
* An account selector element. | ||
* | ||
* @see AccountSelector | ||
*/ | ||
export type AccountSelectorElement = ReturnType<typeof AccountSelector>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters