Skip to content

Commit f8ca178

Browse files
authored
feat(copy): remove title/hideTooltip, add tooltip (#479)
* feat(copy): remove title/hideTooltip, add tooltip * feat(copy): remove toTooltipProps/LabelTooltipType to utils * fix(copy): reset init tooltip value
1 parent cafad70 commit f8ca178

File tree

6 files changed

+87
-30
lines changed

6 files changed

+87
-30
lines changed

src/copy/__tests__/copy.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('test Copy', () => {
3030
it('should render with custom button', () => {
3131
const user = userEvent.setup({ writeToClipboard: true });
3232
const { getByText } = render(
33-
<Copy text={mockText} button={<button>测试复制文本</button>} hideTooltip />
33+
<Copy text={mockText} button={<button>测试复制文本</button>} />
3434
);
3535
const customButton = getByText('测试复制文本');
3636

@@ -46,4 +46,28 @@ describe('test Copy', () => {
4646
expect(value).toEqual(mockText);
4747
});
4848
});
49+
50+
it('should render with tooltip title', () => {
51+
const mockCopy = jest.fn();
52+
render(<Copy text={mockText} onCopy={(text) => mockCopy(text)} tooltip="复制文本" />);
53+
setTimeout(() => {
54+
expect(document.body.querySelector('.ant-tooltip')).toBeInTheDocument();
55+
expect(document.body.querySelector('.ant-tooltip-inner')?.innerHTML).toBe('复制文本');
56+
}, 0);
57+
});
58+
59+
it('should render with tooltip object', () => {
60+
const mockCopy = jest.fn();
61+
render(
62+
<Copy
63+
text={mockText}
64+
onCopy={(text) => mockCopy(text)}
65+
tooltip={{ title: '复制文本' }}
66+
/>
67+
);
68+
setTimeout(() => {
69+
expect(document.body.querySelector('.ant-tooltip')).toBeInTheDocument();
70+
expect(document.body.querySelector('.ant-tooltip-inner')?.innerHTML).toBe('复制文本');
71+
}, 0);
72+
});
4973
});

src/copy/demos/basic.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
import React from 'react';
2-
import { Copy } from 'dt-react-component';
2+
import { Space } from 'antd';
3+
import { BlockHeader, Copy } from 'dt-react-component';
34

45
const text =
56
'基于 ant-design 的 React UI 组件库。 主要用于中,后台产品。我们的目标是满足更具体的业务场景组件。 当然,我们也有基于原生 javascript 实现的业务组件,例如ContextMenu,KeyEventListener等.';
67

78
export default () => {
89
return (
9-
<div>
10-
<Copy text={text} title="复制该文本" />
11-
<p>{text}</p>
12-
</div>
10+
<Space direction="vertical">
11+
<div>
12+
<BlockHeader title="使用 tooltip 对象" showBackground={false} size="small" />
13+
<Copy text={text} tooltip={{ title: '使用 tooltip 对象,复制该文本' }} />
14+
<p>{text}</p>
15+
</div>
16+
<div>
17+
<BlockHeader title="使用 React.ReactNode" showBackground={false} size="small" />
18+
<Copy text={text} tooltip="使用 React.ReactNode,复制该文本" />
19+
<p>{text}</p>
20+
</div>
21+
<div>
22+
<BlockHeader
23+
title={`使用 ()=>React.ReactNode`}
24+
showBackground={false}
25+
size="small"
26+
/>
27+
<Copy text={text} tooltip={() => `使用 ()=>React.ReactNode,复制该文本`} />
28+
<p>{text}</p>
29+
</div>
30+
</Space>
1331
);
1432
};

src/copy/demos/custom.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ const text =
66

77
export default () => {
88
return (
9-
<div>
10-
<Copy text={text} button={<a>复制文本</a>} hideTooltip />
11-
<p>{text}</p>
12-
</div>
9+
<>
10+
<div>
11+
<Copy text={text} button={<a>复制文本</a>} />
12+
<p>{text}</p>
13+
</div>
14+
<div>
15+
<Copy text={text} button={<a>复制文本</a>} tooltip={false} />
16+
<p>{text}</p>
17+
</div>
18+
</>
1319
);
1420
};

src/copy/index.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ demo:
1414

1515
## 示例
1616

17-
<code src='./demos/basic.tsx' title="点击按钮,进行复制"></code>
18-
<code src='./demos/custom.tsx' title="自定义按钮" description='通过hideTooltip属性,可以隐藏默认的提示'></code>
17+
<code src='./demos/basic.tsx' title="点击按钮,进行复制" description='不同方式给 Tooltip 赋值'></code>
18+
<code src='./demos/custom.tsx' title="自定义按钮" description='tooltip 设置假值不展示,默认展示复制'></code>
1919

2020
### API
2121

22-
| 参数 | 说明 | 类型 | 默认值 |
23-
| ----------- | --------------------- | ------------------------ | ----------------------------------- |
24-
| text | 需要复制的文本 | `string` | - |
25-
| title | 在 Tooltip 展示的文本 | `string` | `复制` |
26-
| hideTooltip | 是否隐藏 Tooltip | `boolean` | `false` |
27-
| button | 自定义按钮 | `React.ReactNode` | `<CopyOutlined />` |
28-
| onCopy | 复制后的回调函数 | `(text: string) => void` | `() => message.success('复制成功')` |
22+
| 参数 | 说明 | 类型 | 默认值 |
23+
| --------- | ---------------- | --------------------------------------- | ----------------------------------- |
24+
| text | 需要复制的文本 | `string` | - |
25+
| tooltip | 配置提示信息 | `TooltipProps['title'] \| TooltipProps` | `复制` |
26+
| button | 自定义按钮 | `React.ReactNode` | `<CopyOutlined />` |
27+
| style | 样式 | `React.CSSProperties` | -- |
28+
| className | 样式 | `string` | -- |
29+
| onCopy | 复制后的回调函数 | `(text: string) => void` | `() => message.success('复制成功')` |

src/copy/index.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import { message, Tooltip } from 'antd';
44
import classNames from 'classnames';
55
import useClippy from 'use-clippy';
66

7+
import { LabelTooltipType, toTooltipProps } from '../utils';
78
import './style.scss';
89

910
export interface ICopyProps {
1011
text: string;
11-
title?: ReactNode;
1212
button?: ReactNode;
13-
hideTooltip?: boolean;
1413
style?: CSSProperties;
1514
className?: string;
15+
tooltip?: LabelTooltipType;
1616
onCopy?: (text: string) => void;
1717
}
1818

1919
const Copy: React.FC<ICopyProps> = (props) => {
2020
const {
2121
button = <CopyOutlined className="dtc-copy__default-icon" />,
2222
text,
23-
title = '复制',
24-
hideTooltip,
23+
tooltip = '复制',
2524
style,
2625
className,
2726
onCopy = () => message.success('复制成功'),
@@ -43,13 +42,9 @@ const Copy: React.FC<ICopyProps> = (props) => {
4342
</span>
4443
);
4544

46-
return !hideTooltip ? (
47-
<Tooltip placement="right" title={title}>
48-
{renderCopyButton()}
49-
</Tooltip>
50-
) : (
51-
renderCopyButton()
52-
);
45+
const tooltipProps = toTooltipProps(tooltip);
46+
47+
return <Tooltip {...tooltipProps}>{renderCopyButton()}</Tooltip>;
5348
};
5449

5550
export default Copy;

src/utils/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { TooltipProps } from 'antd';
3+
4+
export type LabelTooltipType = TooltipProps | TooltipProps['title'];
5+
6+
export function toTooltipProps(tooltip: LabelTooltipType): TooltipProps | null {
7+
if (tooltip !== null && typeof tooltip === 'object' && !React.isValidElement(tooltip)) {
8+
return tooltip as TooltipProps;
9+
}
10+
return {
11+
title: tooltip,
12+
};
13+
}

0 commit comments

Comments
 (0)