[英]Tooltip / Popper.js - Resize after adding tooltip title dynamically
I'm using Material-UI's <Tooltip />
component to show dynamically fetched data.我正在使用 Material-UI 的
<Tooltip />
组件来显示动态获取的数据。 I trigger the AJAX call to fetch the data in the onOpen
callback.我触发了 AJAX 调用以在
onOpen
回调中获取数据。 Initially I'm showing a loading spinner but I replace that with the data fetched.最初我展示了一个加载微调器,但我用获取的数据替换了它。
My problem is that the tooltip grows downwards with the new content, such that it goes under my cursor which makes the tooltip think I hovered it again, triggering another fetch for data.我的问题是工具提示随着新内容向下增长,以至于它在我的光标下移动,这使得工具提示认为我再次悬停它,触发另一个数据获取。
Example how I'm using it:例如我如何使用它:
<Tooltip
title={
users ? (
<Grid container direction="column" spacing={8}>
{users.map(user => (
<Grid item key={user}>
{user}
</Grid>
))}
</Grid>
) : <Loading />
}
>
<span>hover me</span>
</Tooltip>
Codesandbox: https://codesandbox.io/s/yq5j9qky7v代码沙盒: https ://codesandbox.io/s/yq5j9qky7v
Looking at the Popper.js implementation, they have a method called scheduleUpdate
to trigger redraw of the tooltip (adjust position and size) https://github.com/FezVrasta/popper.js/blob/master/docs/_includes/popper-documentation.md#popperscheduleupdate查看 Popper.js 实现,他们有一个名为
scheduleUpdate
的方法来触发工具提示的重绘(调整位置和大小) https://github.com/FezVrasta/popper.js/blob/master/docs/_includes/popper-文档.md#popperscheduleupdate
My question is, is it possible to tap into that using only the Material-UI Tooltip component wrapper?我的问题是,是否可以仅使用 Material-UI Tooltip 组件包装器来利用它?
Thanks in advance!提前致谢!
我遇到了同样的问题,在我更新了 Popper 的内容后通过调用修复了它:
window.dispatchEvent(new Event("resize") )
i packed the tooltip component (and its childre) in its own component and then used useEffect
to update some useState
variable and set the title to the useState
variable.我将工具提示组件(及其子组件)
useEffect
到自己的组件中,然后使用useEffect
更新一些useState
变量并将标题设置为useState
变量。 This way only changes in value
trigger the remote API query.这种方式只有
value
变化会触发远程 API 查询。
looks a bit like:看起来有点像:
const MyInput = (props) => {
const [icdInfo, setIcdInfo] = useState({code: '', text: ''});
const {id, value, placement, ...rest} = props;
useEffect(() => {
const fetchDescription = async (code) => {
return await myApi.get(`${apiUrl}/${code}`);
};
fetchDescription(value)
.then((r) => {
setIcdInfo({code: icd_code, text: r.data.text || 'Unknown'});
})
.catch((e) => {
setIcdInfo({code: '', text: 'Error'});
});
return () => {};
}, [value, icdInfo]);
return (
<Tooltip title={icdInfo.text} >
<Child {...rest} />
</Tooltip>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.