Skip to content

Commit 0f83657

Browse files
feat: Added wallet address and balance co-display mode (#1392)
* feat(connect-button): Added wallet address and balance co-display mode * chore: Add changelog * fix: Retain the previous logic when no new properties are provided * Update .changeset/hot-parrots-laugh.md --------- Co-authored-by: 愚指导 <[email protected]>
1 parent c2ad169 commit 0f83657

File tree

8 files changed

+82
-28
lines changed

8 files changed

+82
-28
lines changed

Diff for: .changeset/hot-parrots-laugh.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ant-design/web3-common': minor
3+
'@ant-design/web3': minor
4+
---
5+
6+
feat(connect-button): Added wallet address and balance co-display mode

Diff for: packages/common/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export type WalletMetadata = {
249249

250250
export type Balance = BalanceMetadata & {
251251
value?: bigint;
252+
coverAddress?: boolean;
252253
};
253254

254255
export interface ConnectorTriggerProps {

Diff for: packages/web3/src/connect-button/__tests__/balance.test.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,39 @@ describe('ConnectButton', () => {
4949
);
5050
expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH');
5151
});
52+
53+
it('show address when balance-coveraddress is false', () => {
54+
const { baseElement } = render(
55+
<ConnectButton
56+
account={{
57+
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
58+
}}
59+
balance={{
60+
symbol: 'ETH',
61+
decimals: 18,
62+
value: 1230000000000000000n,
63+
coverAddress: false,
64+
}}
65+
/>,
66+
);
67+
expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('0x21CD...Fd3B 1.23 ETH');
68+
});
69+
70+
it('show name when balance-coveraddress is false', () => {
71+
const { baseElement } = render(
72+
<ConnectButton
73+
account={{
74+
address: '0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B',
75+
name: 'wanderingearth.eth',
76+
}}
77+
balance={{
78+
symbol: 'ETH',
79+
decimals: 18,
80+
value: 1230000000000000000n,
81+
coverAddress: false,
82+
}}
83+
/>,
84+
);
85+
expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('wanderingearth.eth 1.23 ETH');
86+
});
5287
});

Diff for: packages/web3/src/connect-button/__tests__/profile-modal.test.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ describe('ProfileModal', () => {
9191
expect(
9292
baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-web3-address')
9393
?.textContent,
94+
).toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B');
95+
96+
expect(
97+
baseElement.querySelector(
98+
'.ant-web3-connect-button-profile-modal .ant-web3-crypto-price-balance',
99+
)?.textContent,
94100
).toBe('1.23 ETH');
95101
});
96102
});

Diff for: packages/web3/src/connect-button/connect-button.tsx

+18-13
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,27 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
5858
const [messageApi, contextHolder] = message.useMessage();
5959
const [showMenu, setShowMenu] = useState(false);
6060

61+
const { coverAddress = true } = typeof balance !== 'object' ? { coverAddress: true } : balance;
6162
const needSign = !!(sign?.signIn && account?.status === ConnectStatus.Connected && account);
6263
let buttonText: React.ReactNode = intl.getMessage(intl.messages.connect);
6364
if (account) {
64-
buttonText =
65-
account?.name && !balance ? (
66-
account?.name
67-
) : (
68-
<Address
69-
tooltip={false}
70-
ellipsis
71-
address={account.address}
72-
addressPrefix={addressPrefixProp}
73-
>
74-
{balance ? <CryptoPrice icon {...balance} /> : undefined}
75-
</Address>
76-
);
65+
buttonText = (
66+
<>
67+
{(!balance || !coverAddress) &&
68+
(account?.name ? (
69+
account.name
70+
) : (
71+
<Address
72+
tooltip={false}
73+
ellipsis
74+
address={account.address}
75+
addressPrefix={addressPrefixProp}
76+
/>
77+
))}
78+
{balance && !coverAddress && <Divider type="vertical" />}
79+
{balance && <CryptoPrice icon {...balance} />}
80+
</>
81+
);
7782
}
7883

7984
const buttonProps: ButtonProps = {

Diff for: packages/web3/src/connect-button/index.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ After configuring the `quickConnect` property, the installed wallets and univers
7676

7777
### Balance
7878

79-
| Property | Description | Type | Default | Version |
80-
| -------- | -------------- | ------------------ | ------- | ------- |
81-
| value | Balance | `bigint \| number` | - | - |
82-
| symbol | Token symbol | `string` | - | - |
83-
| decimals | Token decimals | `number` | - | - |
79+
| Property | Description | Type | Default | Version |
80+
| ------------ | --------------- | ------------------ | ------- | ------- |
81+
| value | Balance | `bigint \| number` | - | - |
82+
| symbol | Token symbol | `string` | - | - |
83+
| decimals | Token decimals | `number` | - | - |
84+
| coverAddress | Covered address | `boolean` | `true` | - |
8485

8586
### ConnectButtonTooltipProps
8687

Diff for: packages/web3/src/connect-button/index.zh-CN.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ coverDark: https://mdn.alipayobjects.com/huamei_mutawc/afts/img/A*S4cyQ7OCxDUAAA
7777

7878
### Balance
7979

80-
| 属性 | 描述 | 类型 | 默认值 | 版本 |
81-
| -------- | -------- | ------------------ | ------ | ---- |
82-
| value | 余额 | `bigint \| number` | - | - |
83-
| symbol | 代币符号 | `string` | - | - |
84-
| decimals | 代币精度 | `number` | - | - |
80+
| 属性 | 描述 | 类型 | 默认值 | 版本 |
81+
| ------------ | -------- | ------------------ | ------ | ---- |
82+
| value | 余额 | `bigint \| number` | - | - |
83+
| symbol | 代币符号 | `string` | - | - |
84+
| decimals | 代币精度 | `number` | - | - |
85+
| coverAddress | 覆盖地址 | `boolean` | `true` | - |
8586

8687
### ConnectButtonTooltipProps
8788

Diff for: packages/web3/src/connect-button/profile-modal.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,16 @@ export const ProfileModal: React.FC<ProfileModalProps> = ({
8787
<Space align="center" direction="vertical">
8888
{avatar ? <Avatar {...avatar} /> : null}
8989
{name ? <div className={classNames(`${prefixCls}-name`, __hashId__)}>{name}</div> : null}
90-
{address ? (
90+
{balance && <CryptoPrice {...balance} />}
91+
{address && (
9192
<Address
9293
ellipsis={false}
9394
address={address}
9495
type={name ? 'secondary' : undefined}
9596
tooltip={false}
9697
addressPrefix={addressPrefix}
97-
>
98-
{balance && <CryptoPrice {...balance} />}
99-
</Address>
100-
) : null}
98+
/>
99+
)}
101100
</Space>
102101
</Modal>
103102
</>

0 commit comments

Comments
 (0)