- 组件总览
- 通用
- 布局
- 导航
- 数据录入
- 数据展示
- 反馈
- 其他
简单的文字提示气泡框。
鼠标移入则显示提示,移出消失,气泡浮层不承载复杂文本和操作。
可用来代替系统默认的 title
提示,提供一个 按钮/文字/操作
的文案解释。
import React from 'react';
import { Tooltip } from 'antd';
const App: React.FC = () => (
<Tooltip title="prompt text">
<span>Tooltip will show on mouse enter.</span>
</Tooltip>
);
export default App;
import { Button, Divider, Segmented, Tooltip } from 'antd';
import React, { useMemo, useState } from 'react';
const text = <span>prompt text</span>;
const buttonWidth = 70;
const gap = 8;
const btnProps = {
style: {
width: buttonWidth,
},
};
const App: React.FC = () => {
const options = ['Show', 'Hide', 'Center'];
const [arrow, setArrow] = useState('Show');
const mergedArrow = useMemo(() => {
if (arrow === 'Hide') {
return false;
}
if (arrow === 'Show') {
return true;
}
return {
pointAtCenter: true,
};
}, [arrow]);
return (
<div className="demo">
<Segmented
value={arrow}
options={options}
onChange={(val: string) => {
setArrow(val);
}}
/>
<Divider orientation="center">Content</Divider>
<div style={{ marginLeft: buttonWidth, display: 'flex', flexWrap: 'nowrap', columnGap: gap }}>
<Tooltip placement="topLeft" title={text} arrow={mergedArrow}>
<Button {...btnProps}>TL</Button>
</Tooltip>
<Tooltip placement="top" title={text} arrow={mergedArrow}>
<Button {...btnProps}>Top</Button>
</Tooltip>
<Tooltip placement="topRight" title={text} arrow={mergedArrow}>
<Button {...btnProps}>TR</Button>
</Tooltip>
</div>
<div
style={{
width: buttonWidth,
float: 'left',
display: 'flex',
flexDirection: 'column',
rowGap: gap,
}}
>
<Tooltip placement="leftTop" title={text} arrow={mergedArrow}>
<Button>LT</Button>
</Tooltip>
<Tooltip placement="left" title={text} arrow={mergedArrow}>
<Button>Left</Button>
</Tooltip>
<Tooltip placement="leftBottom" title={text} arrow={mergedArrow}>
<Button>LB</Button>
</Tooltip>
</div>
<div
style={{
width: buttonWidth,
marginLeft: buttonWidth * 4 + 24,
display: 'flex',
flexDirection: 'column',
rowGap: gap,
}}
>
<Tooltip placement="rightTop" title={text} arrow={mergedArrow}>
<Button>RT</Button>
</Tooltip>
<Tooltip placement="right" title={text} arrow={mergedArrow}>
<Button>Right</Button>
</Tooltip>
<Tooltip placement="rightBottom" title={text} arrow={mergedArrow}>
<Button>RB</Button>
</Tooltip>
</div>
<div
style={{
marginLeft: buttonWidth,
clear: 'both',
display: 'flex',
flexWrap: 'nowrap',
columnGap: gap,
}}
>
<Tooltip placement="bottomLeft" title={text} arrow={mergedArrow}>
<Button {...btnProps}>BL</Button>
</Tooltip>
<Tooltip placement="bottom" title={text} arrow={mergedArrow}>
<Button {...btnProps}>Bottom</Button>
</Tooltip>
<Tooltip placement="bottomRight" title={text} arrow={mergedArrow}>
<Button {...btnProps}>BR</Button>
</Tooltip>
</div>
</div>
);
};
export default App;
import { Button, Divider, Space, Tooltip } from 'antd';
import React from 'react';
const colors = [
'pink',
'red',
'yellow',
'orange',
'cyan',
'green',
'blue',
'purple',
'geekblue',
'magenta',
'volcano',
'gold',
'lime',
];
const customColors = ['#f50', '#2db7f5', '#87d068', '#108ee9'];
const App: React.FC = () => (
<>
<Divider orientation="left">Presets</Divider>
<Space wrap>
{colors.map((color) => (
<Tooltip title="prompt text" color={color} key={color}>
<Button>{color}</Button>
</Tooltip>
))}
</Space>
<Divider orientation="left">Custom</Divider>
<Space wrap>
{customColors.map((color) => (
<Tooltip title="prompt text" color={color} key={color}>
<Button>{color}</Button>
</Tooltip>
))}
</Space>
</>
);
export default App;
import { Button, Tooltip } from 'antd';
import React from 'react';
const text = <span>prompt text</span>;
const buttonWidth = 70;
const App: React.FC = () => (
<div>
<div style={{ marginLeft: buttonWidth, whiteSpace: 'nowrap' }}>
<Tooltip placement="topLeft" title={text}>
<Button>TL</Button>
</Tooltip>
<Tooltip placement="top" title={text}>
<Button>Top</Button>
</Tooltip>
<Tooltip placement="topRight" title={text}>
<Button>TR</Button>
</Tooltip>
</div>
<div style={{ width: buttonWidth, float: 'left' }}>
<Tooltip placement="leftTop" title={text}>
<Button>LT</Button>
</Tooltip>
<Tooltip placement="left" title={text}>
<Button>Left</Button>
</Tooltip>
<Tooltip placement="leftBottom" title={text}>
<Button>LB</Button>
</Tooltip>
</div>
<div style={{ width: buttonWidth, marginLeft: buttonWidth * 4 + 24 }}>
<Tooltip placement="rightTop" title={text}>
<Button>RT</Button>
</Tooltip>
<Tooltip placement="right" title={text}>
<Button>Right</Button>
</Tooltip>
<Tooltip placement="rightBottom" title={text}>
<Button>RB</Button>
</Tooltip>
</div>
<div style={{ marginLeft: buttonWidth, clear: 'both', whiteSpace: 'nowrap' }}>
<Tooltip placement="bottomLeft" title={text}>
<Button>BL</Button>
</Tooltip>
<Tooltip placement="bottom" title={text}>
<Button>Bottom</Button>
</Tooltip>
<Tooltip placement="bottomRight" title={text}>
<Button>BR</Button>
</Tooltip>
</div>
</div>
);
export default App;
#components-tooltip-demo-placement .ant-btn {
margin-left: 0;
margin-right: 8px;
margin-bottom: 8px;
width: 70px;
text-align: center;
padding: 0;
}
#components-tooltip-demo-placement .ant-btn {
margin-left: 8px;
margin-right: 0;
}
import { Button, Tooltip } from 'antd';
import React from 'react';
const App: React.FC = () => {
React.useEffect(() => {
document.documentElement.scrollTop = document.documentElement.clientHeight;
document.documentElement.scrollLeft = document.documentElement.clientWidth;
}, []);
return (
<div>
<div
style={{
width: '300vw',
height: '300vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<Tooltip title="Thanks for using antd. Have a nice day!" trigger="click" defaultOpen>
<Button>Scroll The Window</Button>
</Tooltip>
</div>
</div>
);
};
export default App;
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 提示文字 | ReactNode | () => ReactNode | - |
以下 API 为 Tooltip、Popconfirm、Popover 共享的 API。
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
align | 该值将合并到 placement 的配置中,设置参考 rc-tooltip | object | - | |
arrow | 修改箭头的显示状态以及修改箭头是否指向目标元素中心 | boolean | { pointAtCenter: boolean } | true | 5.2.0 |
autoAdjustOverflow | 气泡被遮挡时自动调整位置 | boolean | true | |
color | 背景颜色 | string | - | 4.3.0 |
defaultOpen | 默认是否显隐 | boolean | false | 4.23.0 |
destroyTooltipOnHide | 关闭后是否销毁 Tooltip | boolean | false | |
getPopupContainer | 浮层渲染父节点,默认渲染到 body 上 | (triggerNode: HTMLElement) => HTMLElement | () => document.body | |
mouseEnterDelay | 鼠标移入后延时多少才显示 Tooltip,单位:秒 | number | 0.1 | |
mouseLeaveDelay | 鼠标移出后延时多少才隐藏 Tooltip,单位:秒 | number | 0.1 | |
overlayClassName | 卡片类名 | string | - | |
overlayStyle | 卡片样式 | object | - | |
overlayInnerStyle | 卡片内容区域的样式对象 | object | - | |
placement | 气泡框位置,可选 top left right bottom topLeft topRight bottomLeft bottomRight leftTop leftBottom rightTop rightBottom | string | top | |
trigger | 触发行为,可选 hover | focus | click | contextMenu ,可使用数组设置多个触发行为 | string | string[] | hover | |
open | 用于手动控制浮层显隐,小于 4.23.0 使用 visible (为什么?) | boolean | false | 4.23.0 |
zIndex | 设置 Tooltip 的 z-index | number | - | |
onOpenChange | 显示隐藏的回调 | (open: boolean) => void | - | 4.23.0 |
请确保 Tooltip
的子元素能接受 onMouseEnter
、onMouseLeave
、onFocus
、onClick
事件。