I’m trying to add a tooltip to a label prop of the TextField. And I need to make this work on touch screen, so I handle the open and close event manually.
But the tooltip appears as two different boxes
It looks weirder when I use ipad screen.
Anyone have idea about this?
Here is the sample https://codesandbox.io/s/draft-mui-tooltip-in-label-k9py2m?file=/src/App.tsx
2
Answers
I had the same problem as you when I tried to add a tooltip to the label of an outlined
TextField
. It turns out that thelabel
prop is not really thelabel
component that you see on the screen. It’s just used for some internal calculations, and the actual label component isInputLabel
, which you can’t customize in aTextField
.So what I did was to create my own
TextField
usingFormControl
,Tooltip
andOutlinedInput
components. This way, I could put thelabel
element inside aTooltip
component and pass it toFormControl
. I also had to pass the same label value toOutlinedInput
to keep the layout consistent.For example:
This worked for me and I was able to add a
tooltip
to thelabel
without any issues. You can check out the full example here: codesandbox.ioThis is basically how Material UI’s
TextField
works behind the scenes, so you’re not missing out on anything by doing this. If you want to learn more about howTextField
is composed of different components, you can read this section of the docs: https://mui.com/material-ui/react-text-field/#componentsI believe the component in
label
isn’t considered as part of the vDOM tree and you end up with multipleTooltip
elements. Here I moved theTooltip
outside but kept its child inside thelabel
.https://codesandbox.io/s/draft-mui-tooltip-in-label-forked-hc9vvv?file=/src/App.tsx