From 4aad680f2d52d9d4d8a5b53c61d9d1d4c0abab66 Mon Sep 17 00:00:00 2001 From: xwq <2042616571@qq.com> Date: Tue, 15 Apr 2025 19:45:22 +0800 Subject: [PATCH 1/4] feat(connect-button): Added wallet address and balance co-display mode --- packages/common/src/types.ts | 1 + .../wagmi-provider/__tests__/balance.test.tsx | 2 +- .../connect-button/__tests__/balance.test.tsx | 20 ++++++++++++- .../__tests__/profile-modal.test.tsx | 6 ++++ .../src/connect-button/connect-button.tsx | 30 +++++++++++-------- packages/web3/src/connect-button/index.md | 11 +++---- .../web3/src/connect-button/index.zh-CN.md | 11 +++---- .../web3/src/connect-button/profile-modal.tsx | 9 +++--- 8 files changed, 60 insertions(+), 30 deletions(-) diff --git a/packages/common/src/types.ts b/packages/common/src/types.ts index 5a622ded7..9fcf2eccb 100644 --- a/packages/common/src/types.ts +++ b/packages/common/src/types.ts @@ -249,6 +249,7 @@ export type WalletMetadata = { export type Balance = BalanceMetadata & { value?: bigint; + coverAddress?: boolean; }; export interface ConnectorTriggerProps { diff --git a/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx b/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx index 49c805e50..b5eb84bbd 100644 --- a/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx +++ b/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx @@ -62,7 +62,7 @@ describe('WagmiWeb3ConfigProvider balance', () => { ); const { baseElement } = render(); expect(baseElement.querySelector('.ant-web3-connect-button-text')?.textContent).toBe( - ' 1.23 WETH', + '0x21CD...Fd3B 1.23 WETH', ); expect(baseElement.querySelector('.ant-web3-icon-ethereum-filled')).toBeTruthy(); }); diff --git a/packages/web3/src/connect-button/__tests__/balance.test.tsx b/packages/web3/src/connect-button/__tests__/balance.test.tsx index dfbed7f30..009df196b 100644 --- a/packages/web3/src/connect-button/__tests__/balance.test.tsx +++ b/packages/web3/src/connect-button/__tests__/balance.test.tsx @@ -30,7 +30,7 @@ describe('ConnectButton', () => { }} />, ); - expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH'); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('0x21CD...Fd3B 1.23 ETH'); }); it('show balance when has name', () => { @@ -47,6 +47,24 @@ describe('ConnectButton', () => { }} />, ); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('wanderingearth.eth 1.23 ETH'); + }); + + it('hidden address when balance-coveraddress is true', () => { + const { baseElement } = render( + , + ); expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH'); }); }); diff --git a/packages/web3/src/connect-button/__tests__/profile-modal.test.tsx b/packages/web3/src/connect-button/__tests__/profile-modal.test.tsx index afc3fb629..db3d72533 100644 --- a/packages/web3/src/connect-button/__tests__/profile-modal.test.tsx +++ b/packages/web3/src/connect-button/__tests__/profile-modal.test.tsx @@ -91,6 +91,12 @@ describe('ProfileModal', () => { expect( baseElement.querySelector('.ant-web3-connect-button-profile-modal .ant-web3-address') ?.textContent, + ).toBe('0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B'); + + expect( + baseElement.querySelector( + '.ant-web3-connect-button-profile-modal .ant-web3-crypto-price-balance', + )?.textContent, ).toBe('1.23 ETH'); }); }); diff --git a/packages/web3/src/connect-button/connect-button.tsx b/packages/web3/src/connect-button/connect-button.tsx index a60e5fcda..0e7f8702b 100644 --- a/packages/web3/src/connect-button/connect-button.tsx +++ b/packages/web3/src/connect-button/connect-button.tsx @@ -61,19 +61,23 @@ export const ConnectButton: React.FC = (props) => { const needSign = !!(sign?.signIn && account?.status === ConnectStatus.Connected && account); let buttonText: React.ReactNode = intl.getMessage(intl.messages.connect); if (account) { - buttonText = - account?.name && !balance ? ( - account?.name - ) : ( -
- {balance ? : undefined} -
- ); + buttonText = ( + <> + {!balance?.coverAddress && + (account?.name ? ( + account.name + ) : ( +
+ ))} + {balance && !balance.coverAddress && } + {balance && } + + ); } const buttonProps: ButtonProps = { diff --git a/packages/web3/src/connect-button/index.md b/packages/web3/src/connect-button/index.md index 852cd99a1..616a08dd9 100644 --- a/packages/web3/src/connect-button/index.md +++ b/packages/web3/src/connect-button/index.md @@ -76,11 +76,12 @@ After configuring the `quickConnect` property, the installed wallets and univers ### Balance -| Property | Description | Type | Default | Version | -| -------- | -------------- | ------------------ | ------- | ------- | -| value | Balance | `bigint \| number` | - | - | -| symbol | Token symbol | `string` | - | - | -| decimals | Token decimals | `number` | - | - | +| Property | Description | Type | Default | Version | +| ------------ | --------------- | ------------------ | ------- | ------- | +| value | Balance | `bigint \| number` | - | - | +| symbol | Token symbol | `string` | - | - | +| decimals | Token decimals | `number` | - | - | +| coverAddress | Covered address | `boolean` | - | - | ### ConnectButtonTooltipProps diff --git a/packages/web3/src/connect-button/index.zh-CN.md b/packages/web3/src/connect-button/index.zh-CN.md index 2245ccf13..3132fe4b5 100644 --- a/packages/web3/src/connect-button/index.zh-CN.md +++ b/packages/web3/src/connect-button/index.zh-CN.md @@ -77,11 +77,12 @@ coverDark: https://mdn.alipayobjects.com/huamei_mutawc/afts/img/A*S4cyQ7OCxDUAAA ### Balance -| 属性 | 描述 | 类型 | 默认值 | 版本 | -| -------- | -------- | ------------------ | ------ | ---- | -| value | 余额 | `bigint \| number` | - | - | -| symbol | 代币符号 | `string` | - | - | -| decimals | 代币精度 | `number` | - | - | +| 属性 | 描述 | 类型 | 默认值 | 版本 | +| ------------ | -------- | ------------------ | ------ | ---- | +| value | 余额 | `bigint \| number` | - | - | +| symbol | 代币符号 | `string` | - | - | +| decimals | 代币精度 | `number` | - | - | +| coverAddress | 覆盖地址 | `boolean` | - | - | ### ConnectButtonTooltipProps diff --git a/packages/web3/src/connect-button/profile-modal.tsx b/packages/web3/src/connect-button/profile-modal.tsx index 396e1e766..4ec59f310 100644 --- a/packages/web3/src/connect-button/profile-modal.tsx +++ b/packages/web3/src/connect-button/profile-modal.tsx @@ -87,17 +87,16 @@ export const ProfileModal: React.FC = ({ {avatar ? : null} {name ?
{name}
: null} - {address ? ( + {balance && } + {address && (
- {balance && } -
- ) : null} + /> + )}
From dc49b6f49944577863a74c4caf9460f89ba5bb75 Mon Sep 17 00:00:00 2001 From: xwq <2042616571@qq.com> Date: Tue, 15 Apr 2025 20:43:02 +0800 Subject: [PATCH 2/4] chore: Add changelog --- .changeset/hot-parrots-laugh.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/hot-parrots-laugh.md diff --git a/.changeset/hot-parrots-laugh.md b/.changeset/hot-parrots-laugh.md new file mode 100644 index 000000000..b10d055bf --- /dev/null +++ b/.changeset/hot-parrots-laugh.md @@ -0,0 +1,7 @@ +--- +'@ant-design/web3-common': patch +'@ant-design/web3-wagmi': patch +'@ant-design/web3': patch +--- + +feat(connect-button): Added wallet address and balance co-display mode From a9f19be898eb3f1ebbb497e65fc42c188a2abfa4 Mon Sep 17 00:00:00 2001 From: xwq <2042616571@qq.com> Date: Wed, 16 Apr 2025 22:21:14 +0800 Subject: [PATCH 3/4] fix: Retain the previous logic when no new properties are provided --- .changeset/hot-parrots-laugh.md | 6 ++--- .../wagmi-provider/__tests__/balance.test.tsx | 2 +- .../connect-button/__tests__/balance.test.tsx | 27 +++++++++++++++---- .../src/connect-button/connect-button.tsx | 5 ++-- packages/web3/src/connect-button/index.md | 2 +- .../web3/src/connect-button/index.zh-CN.md | 2 +- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.changeset/hot-parrots-laugh.md b/.changeset/hot-parrots-laugh.md index b10d055bf..3232e6681 100644 --- a/.changeset/hot-parrots-laugh.md +++ b/.changeset/hot-parrots-laugh.md @@ -1,7 +1,7 @@ --- -'@ant-design/web3-common': patch -'@ant-design/web3-wagmi': patch -'@ant-design/web3': patch +'@ant-design/web3-common': minor +'@ant-design/web3-wagmi': minor +'@ant-design/web3': minor --- feat(connect-button): Added wallet address and balance co-display mode diff --git a/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx b/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx index b5eb84bbd..49c805e50 100644 --- a/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx +++ b/packages/wagmi/src/wagmi-provider/__tests__/balance.test.tsx @@ -62,7 +62,7 @@ describe('WagmiWeb3ConfigProvider balance', () => { ); const { baseElement } = render(); expect(baseElement.querySelector('.ant-web3-connect-button-text')?.textContent).toBe( - '0x21CD...Fd3B 1.23 WETH', + ' 1.23 WETH', ); expect(baseElement.querySelector('.ant-web3-icon-ethereum-filled')).toBeTruthy(); }); diff --git a/packages/web3/src/connect-button/__tests__/balance.test.tsx b/packages/web3/src/connect-button/__tests__/balance.test.tsx index 009df196b..ac93fe1b2 100644 --- a/packages/web3/src/connect-button/__tests__/balance.test.tsx +++ b/packages/web3/src/connect-button/__tests__/balance.test.tsx @@ -30,7 +30,7 @@ describe('ConnectButton', () => { }} />, ); - expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('0x21CD...Fd3B 1.23 ETH'); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH'); }); it('show balance when has name', () => { @@ -47,10 +47,27 @@ describe('ConnectButton', () => { }} />, ); - expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('wanderingearth.eth 1.23 ETH'); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH'); }); - it('hidden address when balance-coveraddress is true', () => { + it('show address when balance-coveraddress is false', () => { + const { baseElement } = render( + , + ); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('0x21CD...Fd3B 1.23 ETH'); + }); + + it('show name when balance-coveraddress is false', () => { const { baseElement } = render( { symbol: 'ETH', decimals: 18, value: 1230000000000000000n, - coverAddress: true, + coverAddress: false, }} />, ); - expect(baseElement.querySelector('.ant-btn')?.textContent).toBe(' 1.23 ETH'); + expect(baseElement.querySelector('.ant-btn')?.textContent).toBe('wanderingearth.eth 1.23 ETH'); }); }); diff --git a/packages/web3/src/connect-button/connect-button.tsx b/packages/web3/src/connect-button/connect-button.tsx index 0e7f8702b..b21dbc99b 100644 --- a/packages/web3/src/connect-button/connect-button.tsx +++ b/packages/web3/src/connect-button/connect-button.tsx @@ -58,12 +58,13 @@ export const ConnectButton: React.FC = (props) => { const [messageApi, contextHolder] = message.useMessage(); const [showMenu, setShowMenu] = useState(false); + const { coverAddress = true } = typeof balance !== 'object' ? { coverAddress: true } : balance; const needSign = !!(sign?.signIn && account?.status === ConnectStatus.Connected && account); let buttonText: React.ReactNode = intl.getMessage(intl.messages.connect); if (account) { buttonText = ( <> - {!balance?.coverAddress && + {(!balance || !coverAddress) && (account?.name ? ( account.name ) : ( @@ -74,7 +75,7 @@ export const ConnectButton: React.FC = (props) => { addressPrefix={addressPrefixProp} /> ))} - {balance && !balance.coverAddress && } + {balance && !coverAddress && } {balance && } ); diff --git a/packages/web3/src/connect-button/index.md b/packages/web3/src/connect-button/index.md index 616a08dd9..1ed5f6acc 100644 --- a/packages/web3/src/connect-button/index.md +++ b/packages/web3/src/connect-button/index.md @@ -81,7 +81,7 @@ After configuring the `quickConnect` property, the installed wallets and univers | value | Balance | `bigint \| number` | - | - | | symbol | Token symbol | `string` | - | - | | decimals | Token decimals | `number` | - | - | -| coverAddress | Covered address | `boolean` | - | - | +| coverAddress | Covered address | `boolean` | `true` | - | ### ConnectButtonTooltipProps diff --git a/packages/web3/src/connect-button/index.zh-CN.md b/packages/web3/src/connect-button/index.zh-CN.md index 3132fe4b5..5591903b4 100644 --- a/packages/web3/src/connect-button/index.zh-CN.md +++ b/packages/web3/src/connect-button/index.zh-CN.md @@ -82,7 +82,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_mutawc/afts/img/A*S4cyQ7OCxDUAAA | value | 余额 | `bigint \| number` | - | - | | symbol | 代币符号 | `string` | - | - | | decimals | 代币精度 | `number` | - | - | -| coverAddress | 覆盖地址 | `boolean` | - | - | +| coverAddress | 覆盖地址 | `boolean` | `true` | - | ### ConnectButtonTooltipProps From caa12605651b86af9661c9370b7609791f3f72bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=84=9A=E6=8C=87=E5=AF=BC?= Date: Thu, 17 Apr 2025 10:09:46 +0800 Subject: [PATCH 4/4] Update .changeset/hot-parrots-laugh.md --- .changeset/hot-parrots-laugh.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/hot-parrots-laugh.md b/.changeset/hot-parrots-laugh.md index 3232e6681..78c6e6381 100644 --- a/.changeset/hot-parrots-laugh.md +++ b/.changeset/hot-parrots-laugh.md @@ -1,6 +1,5 @@ --- '@ant-design/web3-common': minor -'@ant-design/web3-wagmi': minor '@ant-design/web3': minor ---