Skip to content

Commit

Permalink
refactor: modify address code & add APIS from docs & add test (#507)
Browse files Browse the repository at this point in the history
* refactor: modify address code & add APIS from docs & add test

* modify address test

* modify code

* feat: revert code
  • Loading branch information
shanchuan1 authored Jan 19, 2024
1 parent 73888cc commit 75a85d6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-jars-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ant-design/web3': patch
---

refactor: modify address code & add APIS from docs & add test
14 changes: 14 additions & 0 deletions packages/web3/src/address/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ describe('Address', () => {
);
});

it('should display custom tooltip if tooltip is set custom', async () => {
const { baseElement } = render(
<Address
ellipsis
address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'}
tooltip={<span>hello</span>}
/>,
);
fireEvent.mouseEnter(baseElement.querySelector('.ant-web3-address-text')!);
await vi.waitFor(() => {
expect(baseElement.querySelector('.ant-tooltip-inner')?.textContent).toBe('hello');
});
});

it('should show copy icon after 2s', async () => {
const { baseElement } = render(
<Address address="0x21CDf0974d53a6e96eF05d7B324a9803735fFd3B" copyable />,
Expand Down
19 changes: 19 additions & 0 deletions packages/web3/src/address/demos/customTooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Address } from '@ant-design/web3';
import { Space } from 'antd';

const App: React.FC = () => {
return (
<Space>
<Address ellipsis address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'} tooltip />
<Address
ellipsis
address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'}
tooltip={<span>hello</span>}
/>
<Address ellipsis address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'} tooltip={'hi'} />
<Address ellipsis address={'3ea2cfd153b8d8505097b81c87c11f5d05097c18'} tooltip={false} />
</Space>
);
};

export default App;
7 changes: 6 additions & 1 deletion packages/web3/src/address/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ group:

<code src="./demos/format.tsx"></code>

## Custom Tooltip

<code src="./demos/customTooltip.tsx"></code>

## API

| Property | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| ellipsis | Address clipping strategy | `boolean \| { headClip?: number, tailClip?: number }` | `{ headClip: 6, tailClip: 4 }` | - |
| copyable | Address copyable | `boolean` | `false` | - |
| address | Address | `string` | - | - |
| tooltip | Show tooltip when hover address | `boolean \|`[Tooltip.title](https://ant.design/components/tooltip-cn#api) | Displays the current full address | - |
| tooltip | Show tooltip when hover address | `boolean \|`[Tooltip.title](https://ant.design/components/tooltip-cn#api) | `true ` | - |
| format | Address format | `boolean \| (input: string) => ReactNode` | `false` | - |
| locale | Multilingual settings | `Locale["address"]` | - | - |
39 changes: 23 additions & 16 deletions packages/web3/src/address/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode } from 'react';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import React, { isValidElement, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { CheckOutlined, CopyOutlined } from '@ant-design/icons';
import type { Locale } from '@ant-design/web3-common';
import type { TooltipProps } from 'antd';
Expand All @@ -25,7 +25,7 @@ export interface AddressProps {
}

export const Address: React.FC<React.PropsWithChildren<AddressProps>> = (props) => {
const { ellipsis, address, copyable, tooltip, format = false, children, locale } = props;
const { ellipsis, address, copyable, tooltip = true, format = false, children, locale } = props;
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('web3-address');
const { wrapSSR, hashId } = useStyle(prefixCls);
Expand Down Expand Up @@ -63,15 +63,32 @@ export const Address: React.FC<React.PropsWithChildren<AddressProps>> = (props)
if (!address) {
return null;
}

const filledAddress = fillWith0x(address);

const mergedTooltip = () => {
if (isValidElement(tooltip) || typeof tooltip === 'string') {
return tooltip;
}
if (tooltip) {
return filledAddress;
}
return tooltip;
};

const formattedAddress = mergedFormat(filledAddress);
const displayTooltip = tooltip === undefined || tooltip === true ? filledAddress : tooltip;

const handleOutlinedChange = () => {
writeCopyText(filledAddress).then(() => {
setCopied(true);
timerRef.current = setTimeout(() => {
setCopied(false);
}, 2000);
});
};

return wrapSSR(
<Space className={classNames(prefixCls, hashId)}>
<Tooltip title={displayTooltip}>
<Tooltip title={mergedTooltip()}>
<span className={`${prefixCls}-text`}>
{children ??
(isEllipsis
Expand All @@ -84,17 +101,7 @@ export const Address: React.FC<React.PropsWithChildren<AddressProps>> = (props)
{copied ? (
<CheckOutlined title={messages.copiedTips} />
) : (
<CopyOutlined
title={messages.copyTips}
onClick={() => {
writeCopyText(filledAddress).then(() => {
setCopied(true);
timerRef.current = setTimeout(() => {
setCopied(false);
}, 2000);
});
}}
/>
<CopyOutlined title={messages.copyTips} onClick={handleOutlinedChange} />
)}
</>
)}
Expand Down
7 changes: 6 additions & 1 deletion packages/web3/src/address/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ group:

<code src="./demos/format.tsx"></code>

## 自定义Tooltip

<code src="./demos/customTooltip.tsx"></code>

## API

| 属性 | 描述 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| ellipsis | 地址裁剪策略 | `boolean \| { headClip?: number, tailClip?: number }` | `{ headClip: 6, tailClip: 4 }` | - |
| copyable | 是否可复制 | `boolean` | `false` | - |
| address | 地址 | `string` | - | - |
| tooltip | 鼠标移入地址时展示提示 | `boolean \|` [Tooltip.title](https://ant.design/components/tooltip-cn#api) | 展示当前完整地址 | - |
| tooltip | 鼠标移入地址时展示提示 | `boolean \|` [Tooltip.title](https://ant.design/components/tooltip-cn#api) | `true ` | - |
| format | 地址格式化 | `boolean \| (input: string) => ReactNode` | `false` | - |
| locale | 多语言文案设置 | `Locale["address"]` | - | - |

0 comments on commit 75a85d6

Please sign in to comment.