[英]Tooltip inside TextInput label is not working. Material-UI + React
I want to use an Outlined style of a TextField who's label must contain a tooltip icon with some text我想使用TextField的 Outlined 样式,其标签必须包含带有一些文本的工具提示图标
Please refer to Sandbox for a live demo请参考Sandbox进行现场演示
Code excerpt:代码摘录:
const IconWithTooltip = () => (
<Tooltip title="Text explaining stuff">
<HelpIcon />
</Tooltip>
);
const Example = () => {
return (
<div>
<FormControl variant="outlined">
<InputLabel htmlFor="with-label">
FormControl with label
<IconWithTooltip />
</InputLabel>
<OutlinedInput
id="with-label"
startAdornment={<InputAdornment position="start">$</InputAdornment>}
/>
</FormControl>
<TextField
label={
<div>
TextFiled
<IconWithTooltip />
</div>
}
variant="outlined"
/>
Just icon with tooltop
<IconWithTooltip />
</div>
);
};
Problem: When hovering over the (?) icon tooltip does not appear.问题:将鼠标悬停在 (?) 图标上时不出现工具提示。 I have tried coding the input in 2 different ways using FormControl and TextInput but none works.
我曾尝试使用 FormControl 和 TextInput 以 2 种不同的方式对输入进行编码,但均无效。 Am i missing something?
我错过了什么吗?
As indicated by Nimmi in a comment, this is due to pointer-events: none
in the CSS for the label .正如 Nimmi 在评论中指出的那样,这是由于标签的CSS 中的
pointer-events: none
。
Changing this in the manner shown below does allow the tooltip to work, but you should NOT do this .以下面显示的方式更改此设置确实允许工具提示起作用,但您不应该这样做。 This causes the label to be clickable.
这会导致标签可点击。 When
pointer-events
is none
, a click on the label passes through to the input and causes it to receive focus.当
pointer-events
为none
,对标签的点击会传递到输入并使其获得焦点。 When pointer-events
is auto
, the click stops on the label and does not bring focus to the input.当
pointer-events
为auto
,单击将停止在标签上并且不会将焦点移至输入。
You may want to look into leveraging helper text (shown below the input) as a place to incorporate the tooltip.您可能希望研究利用帮助文本(显示在输入下方)作为合并工具提示的地方。
<TextField
InputLabelProps={{ style: { pointerEvents: "auto" } }}
label={
<div>
TextFiled
<IconWithTooltip />
</div>
}
variant="outlined"
type="text"
/>
Related documentation:相关文档:
You can avoid the InputLabelProps and make the tooltip work in the input adornment with the tooltip PopperProps:您可以避免使用 InputLabelProps 并使用工具提示 PopperProps 使工具提示在输入装饰中工作:
<Tooltip
title="Tooltip message"
arrow
PopperProps={{
disablePortal: true,
popperOptions: {
positionFixed: true,
modifiers: {
preventOverflow: {
enabled: true,
boundariesElement: 'window'
}
}
}
}}
>
<Info color="error" />
</Tooltip>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.