How to make line break for ToolTip titles in Material-UI

二次信任 提交于 2020-04-16 02:27:12

问题


I am using the ToolTip component, I have two texts for the title, just wonder is that possible to render them as two lines (each language in one line) rather than one line? How can I apply the expected style to this component?

Here is the code:

renderToolTip = tipText => {
    const { classes } = this.props;
    if (tipText) {
      return (
        <Tooltip
          title={tipText}
          placement="left"
          classes={{
            tooltip: classes.tooltip,
          }}>
          <IconButton>
            <InfoOutlined />
          </IconButton>
        </Tooltip>
      );
    }
    return null;
  };

languageList = ['English', 'Spanish']
languageList.map(language => this.renderToolTip(language));

Expected: line break for each text, Can anyone help?


回答1:


You can wrap your title with a span and add a style to it just like below:

<Tooltip
  title={<span style={{ whiteSpace: 'pre-line' }}>{tipText}</span>}
  placement="left"
  classes={{
    tooltip: classes.tooltip,
  }}>
    <IconButton>
      <InfoOutlined />
    </IconButton>
</Tooltip>

Now, when your text includes \n anywhere, it will break the line. For example you can have a text like the following:

Hey there,\nCome here

This will be rendered like:

Hey there,
Come here



回答2:


Yes, you can

Refer to the official document of materia-ui customized-tooltips

Try it online: https://stackblitz.com/run?file=demo.js


You can add customized HTML content via props title.

<Tooltip
  title={
    <>
      <Typography color="inherit">Line one</Typography>
      <Typography color="inherit">Line two</Typography>
    </>
  }
>
  <Button>HTML</Button>
</Tooltip>

Since you can write almost whatever you want inside of it, it shouldn't be a problem.



来源:https://stackoverflow.com/questions/60598842/how-to-make-line-break-for-tooltip-titles-in-material-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!