reactjs - 文本字段 : helperText spanning multiple lines

标签 reactjs material-ui

我有 TextFieldhelperText .我想做 helperText跨越多行。为此,我需要某种换行符。 Material-UI 渲染 <p>{ helperText }</p> .因为在 html 中,段落中的换行符由 <br/> 给出我试图将其添加到我的文本中。然而,这只是添加了 <br/>进入正文。我也尝试定义 helperText在组件外,一次用 <br/>和一次 \n .但这也不起作用。
如何在 helperText 中正确添加换行符?
我的代码示例:

<TextField
    name={"help message"}
    value={data_field.helper_text}
    onChange={(event) => handleDataChange("helper_text", event.target.value)}
    helperText={"The value will be shown be shown below the field, just like this text is.<br> This <br/> text should go in the second line."}
/>
结果示例:
enter image description here

最佳答案

helperText props 可以接受 ReactNode ,所以你可以添加一个 <br/> 元素来换行,如下所示:

<TextField
  fullWidth
  helperText={
    <>
      The value will be shown be shown below the field, just like this text
      is.
      <br />
      This
      <br /> text should go in the second line.
    </>
  }
  {...props}
/>
Edit 67264236/textfield-helpertext-spanning-multiple-lines
@Vaibhav 建议的另一个解决方案是在包含 helperText 的元素上设置宽度:
import { styled } from '@mui/material/styles';
import MuiTextField from '@mui/material/TextField';

const TextField = styled(MuiTextField)({
  '& .MuiFormHelperText-root': {
    width: 250,
  },
});
<TextField
  fullWidth
  helperText="The value will be shown be shown below the field, just like this text is. This text should go in the second line."
  label="Outlined"
  variant="outlined"
/>
Edit 67264236/textfield-helpertext-spanning-multiple-lines (forked)

关于reactjs - 文本字段 : helperText spanning multiple lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67264236/

相关文章:

javascript - Axios onUploadProgress 在我的机器上只触发一次

amazon-web-services - cloudfront 指向 s3 上托管的旧版 React

facebook - React Native 和 Parse.FacebookUtils

javascript - 为什么 React/Redux 会重置所有事件处理程序,即使 `state` 没有改变

reactjs - 直接在material-ui的主题配置中支持渐变

javascript - 如何不通过整个响应映射对象

css - 抽屉组件上的类名不起作用

css - 将 onHover 样式应用于 MuiButton 中的元素

css - 更改 material-ui 复选框的样式

reactjs - 如何在React中的material-UI选择框中设置默认值(API调用后)?