Skip to content

Commit

Permalink
feat: optimized connect-button code (#481)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
shanchuan1 and thinkasany authored Jan 12, 2024
1 parent 31e802c commit 79a91ff
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-singers-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ant-design/web3': patch
---

refactor: Optimization ConnectButton code
19 changes: 19 additions & 0 deletions packages/web3/src/connect-button/__tests__/tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ describe('ConnectButton', () => {
expect(baseElement.querySelector('.anticon-copy')).toBeNull();
});

it('Enable copyable in tooltip', () => {
const { baseElement } = render(
<ConnectButton
account={{ address: '3ea2cfd153b8d8505097b81c87c11f5d05097c18' }}
tooltip={{ open: true, copyable: true }}
/>,
);
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(
<ConnectButton
Expand All @@ -62,6 +76,11 @@ describe('ConnectButton', () => {
expect(baseElement.querySelector('.ant-tooltip')).toBeNull();
});

it('should not display tooltip when without tooltip in tooltip', () => {
const { baseElement } = render(<ConnectButton />);
expect(baseElement.querySelector('.ant-tooltip')).toBeNull();
});

it('should copy text after click copy icon', async () => {
const { baseElement } = render(
<ConnectButton
Expand Down
87 changes: 40 additions & 47 deletions packages/web3/src/connect-button/connect-button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext, useMemo, useState } from 'react';
import { CopyOutlined, LoginOutlined, UserOutlined } from '@ant-design/icons';
import type { Chain } from '@ant-design/web3-common';
import type { ButtonProps } from 'antd';
import { Avatar, Button, ConfigProvider, Divider, Dropdown, message, Space } from 'antd';
import type { MenuItemType } from 'antd/es/menu/hooks/useItems';
Expand All @@ -8,9 +9,12 @@ import classNames from 'classnames';
import { Address } from '../address';
import { CryptoPrice } from '../crypto-price';
import useIntl from '../hooks/useIntl';
import type { IntlType } from '../hooks/useIntl';
import { fillWith0x, writeCopyText } from '../utils';
import { ChainSelect } from './chain-select';
import type { ChainSelectProps } from './chain-select';
import type { ConnectButtonProps, ConnectButtonTooltipProps } from './interface';
import type { ProfileModalProps } from './profile-modal';
import { ProfileModal } from './profile-modal';
import { useStyle } from './style';
import { ConnectButtonTooltip } from './tooltip';
Expand Down Expand Up @@ -73,21 +77,39 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
...restProps,
};

const renderChainSelect = () => {
if (availableChains && availableChains.length > 1) {
return (
<ChainSelect
hashId={hashId}
onSwitchChain={onSwitchChain}
currentChain={chain}
chains={availableChains}
/>
);
}
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 ? (
<div className={`${prefixCls}-chain-icon`}>{chain?.icon}</div>
) : (
<UserOutlined className={`${prefixCls}-default-icon`} />
),
},
balance,
modalProps: typeof profileModal === 'object' ? profileModal : undefined,
};

const chainSelect =
availableChains && availableChains.length > 1 ? <ChainSelect {...chainProps} /> : null;

const buttonInnerText = (
<div className={`${prefixCls}-content`}>
Expand All @@ -114,36 +136,6 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
<Button {...buttonProps}>{buttonInnerText}</Button>
);

const profileModalContent = (
<ProfileModal
intl={intl}
open={profileOpen}
__hashId__={hashId}
onDisconnect={() => {
setProfileOpen(false);
onDisconnectClick?.();
}}
onClose={() => {
setProfileOpen(false);
}}
address={account?.address}
name={account?.name}
avatar={
avatar ?? {
icon: chain?.icon ? (
<div className={`${prefixCls}-chain-icon`}>{chain?.icon}</div>
) : (
<UserOutlined className={`${prefixCls}-default-icon`} />
),
}
}
balance={balance}
modalProps={typeof profileModal === 'object' ? profileModal : undefined}
/>
);

let content = buttonContent;

const defaultMenuItems: MenuItemType[] = useMemo(
() => [
{
Expand Down Expand Up @@ -194,8 +186,8 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
return combinedItems;
}, [actionsMenu, defaultMenuItems, account]);

if (mergedMenuItems.length > 0) {
content = (
const content =
mergedMenuItems.length > 0 ? (
<Dropdown
open={showMenu}
onOpenChange={setShowMenu}
Expand All @@ -206,8 +198,9 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
>
{buttonContent}
</Dropdown>
) : (
buttonContent
);
}

const mergedTooltipCopyable: ConnectButtonTooltipProps['copyable'] =
typeof tooltip === 'object' ? tooltip.copyable !== false : !!tooltip;
Expand All @@ -234,7 +227,7 @@ export const ConnectButton: React.FC<ConnectButtonProps> = (props) => {
) : (
content
)}
{profileModalContent}
<ProfileModal {...profileModalProps} />
</>
);

Expand Down
38 changes: 20 additions & 18 deletions packages/web3/src/connect-button/profile-modal.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,26 +41,28 @@ export const ProfileModal: React.FC<ProfileModalProps> = ({
const prefixCls = getPrefixCls('web3-connect-button-profile-modal');
const [messageApi, contextHolder] = message.useMessage();

const footer = (
<div className={classNames(`${prefixCls}-footer`, __hashId__)}>
{address ? (
<Button
onClick={() => {
writeCopyText(address).then(() => {
messageApi.success(intl.getMessage(intl.messages.addressCopied));
});
}}
>
{intl.getMessage(intl.messages.copyAddress)}
</Button>
) : null}
<Button onClick={onDisconnect}>{intl.getMessage(intl.messages.disconnect)}</Button>
</div>
);

return (
<>
{contextHolder}
<Modal
footer={
<div className={classNames(`${prefixCls}-footer`, __hashId__)}>
{address ? (
<Button
onClick={() => {
writeCopyText(address).then(() => {
messageApi.success(intl.getMessage(intl.messages.addressCopied));
});
}}
>
{intl.getMessage(intl.messages.copyAddress)}
</Button>
) : null}
<Button onClick={onDisconnect}>{intl.getMessage(intl.messages.disconnect)}</Button>
</div>
}
footer={footer}
width={280}
{...modalProps}
onCancel={onClose}
Expand Down

0 comments on commit 79a91ff

Please sign in to comment.