From 79a91ff103e2697fe9e2e0c762f2be170b00cf4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=B1=E5=B7=9D?= <1121021693@qq.com> Date: Fri, 12 Jan 2024 16:49:05 +0800 Subject: [PATCH] feat: optimized connect-button code (#481) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: optimized connect-button code * feat: 优化代码规范 * feat: revert interface code * feat: Optimization ConnectButton code * feat: revert code * feat: revert code * Update connect-button.tsx * feat: increase ConnectButton test * feat: revert connect-button code --------- Co-authored-by: thinkasany <480968828@qq.com> --- .changeset/thick-singers-deny.md | 5 ++ .../connect-button/__tests__/tooltip.test.tsx | 19 ++++ .../src/connect-button/connect-button.tsx | 87 +++++++++---------- .../web3/src/connect-button/profile-modal.tsx | 38 ++++---- 4 files changed, 84 insertions(+), 65 deletions(-) create mode 100644 .changeset/thick-singers-deny.md diff --git a/.changeset/thick-singers-deny.md b/.changeset/thick-singers-deny.md new file mode 100644 index 000000000..1c66df499 --- /dev/null +++ b/.changeset/thick-singers-deny.md @@ -0,0 +1,5 @@ +--- +'@ant-design/web3': patch +--- + +refactor: Optimization ConnectButton code diff --git a/packages/web3/src/connect-button/__tests__/tooltip.test.tsx b/packages/web3/src/connect-button/__tests__/tooltip.test.tsx index 4597d0e00..ac29aede2 100644 --- a/packages/web3/src/connect-button/__tests__/tooltip.test.tsx +++ b/packages/web3/src/connect-button/__tests__/tooltip.test.tsx @@ -44,6 +44,20 @@ describe('ConnectButton', () => { expect(baseElement.querySelector('.anticon-copy')).toBeNull(); }); + it('Enable copyable in tooltip', () => { + const { baseElement } = render( + , + ); + expect(baseElement.querySelector('.ant-tooltip')).not.toBeNull(); + expect(baseElement.querySelector('.ant-tooltip-inner')?.textContent?.trim()).toBe( + '0x3ea2cfd153b8d8505097b81c87c11f5d05097c18', + ); + expect(baseElement.querySelector('.anticon-copy')).toBeTruthy(); + }); + it('custom title in tooltip', () => { const { baseElement } = render( { expect(baseElement.querySelector('.ant-tooltip')).toBeNull(); }); + it('should not display tooltip when without tooltip in tooltip', () => { + const { baseElement } = render(); + expect(baseElement.querySelector('.ant-tooltip')).toBeNull(); + }); + it('should copy text after click copy icon', async () => { const { baseElement } = render( = (props) => { ...restProps, }; - const renderChainSelect = () => { - if (availableChains && availableChains.length > 1) { - return ( - - ); - } - return null; + const chainProps: ChainSelectProps = { + hashId, + onSwitchChain, + currentChain: chain, + chains: availableChains as Chain[], }; - const chainSelect = renderChainSelect(); + const profileModalProps: ProfileModalProps = { + intl, + open: profileOpen, + __hashId__: hashId, + onDisconnect: () => { + setProfileOpen(false); + onDisconnectClick?.(); + }, + onClose: () => { + setProfileOpen(false); + }, + address: account?.address, + name: account?.name, + avatar: avatar ?? { + icon: chain?.icon ? ( +
{chain?.icon}
+ ) : ( + + ), + }, + balance, + modalProps: typeof profileModal === 'object' ? profileModal : undefined, + }; + + const chainSelect = + availableChains && availableChains.length > 1 ? : null; const buttonInnerText = (
@@ -114,36 +136,6 @@ export const ConnectButton: React.FC = (props) => { ); - const profileModalContent = ( - { - setProfileOpen(false); - onDisconnectClick?.(); - }} - onClose={() => { - setProfileOpen(false); - }} - address={account?.address} - name={account?.name} - avatar={ - avatar ?? { - icon: chain?.icon ? ( -
{chain?.icon}
- ) : ( - - ), - } - } - balance={balance} - modalProps={typeof profileModal === 'object' ? profileModal : undefined} - /> - ); - - let content = buttonContent; - const defaultMenuItems: MenuItemType[] = useMemo( () => [ { @@ -194,8 +186,8 @@ export const ConnectButton: React.FC = (props) => { return combinedItems; }, [actionsMenu, defaultMenuItems, account]); - if (mergedMenuItems.length > 0) { - content = ( + const content = + mergedMenuItems.length > 0 ? ( = (props) => { > {buttonContent} + ) : ( + buttonContent ); - } const mergedTooltipCopyable: ConnectButtonTooltipProps['copyable'] = typeof tooltip === 'object' ? tooltip.copyable !== false : !!tooltip; @@ -234,7 +227,7 @@ export const ConnectButton: React.FC = (props) => { ) : ( content )} - {profileModalContent} + ); diff --git a/packages/web3/src/connect-button/profile-modal.tsx b/packages/web3/src/connect-button/profile-modal.tsx index 67b105cbc..b594a3464 100644 --- a/packages/web3/src/connect-button/profile-modal.tsx +++ b/packages/web3/src/connect-button/profile-modal.tsx @@ -1,8 +1,8 @@ import React, { useContext } from 'react'; import { Address } from '@ant-design/web3'; import type { Balance } from '@ant-design/web3-common'; -import { Avatar, Button, ConfigProvider, message, Modal, Space, type AvatarProps } from 'antd'; -import type { ModalProps } from 'antd'; +import { Avatar, Button, ConfigProvider, message, Modal, Space } from 'antd'; +import type { AvatarProps, ModalProps } from 'antd'; import classNames from 'classnames'; import { CryptoPrice } from '../crypto-price'; @@ -41,26 +41,28 @@ export const ProfileModal: React.FC = ({ const prefixCls = getPrefixCls('web3-connect-button-profile-modal'); const [messageApi, contextHolder] = message.useMessage(); + const footer = ( +
+ {address ? ( + + ) : null} + +
+ ); + return ( <> {contextHolder} - {address ? ( - - ) : null} - -
- } + footer={footer} width={280} {...modalProps} onCancel={onClose}