【问题标题】:Custom Text Field with icon at start of the Text Field with the variant type "outlined" in material-ui在 Material-ui 中变体类型为“概述”的文本字段开头带有图标的自定义文本字段
【发布时间】:2021-12-17 18:10:30
【问题描述】:

大家

我一直在尝试使用material-uiTextField 组件的开头实现一个图标。 我相信读者对这个e Material UI 库有所了解。我坚持在这个项目中使用版本 4。

我确实找到了一些实现这一目标的方法,并且我确实尝试过。但它在某些条件下起作用。也就是说,如果我尝试为 TextFieldnotchedOutline 设置样式,就像给它一个 borderRadius em> 或图标不可见的东西。虽然在检查了元素之后,我确实在那里找到了它。我确实尝试改变颜色和一切,但没有任何效果。

这是我需要的:

Image Describing my need

这是我得到的:

The coded output image

这是我尝试过的代码。

import {....,TextField,InputAdornment } from "@material-ui/core";
......... 

<TextField
    variant="outlined"
    InputProps={{
        startAdornment: (<InputAdornment position="start"><img src={someImage} /></InputAdornment>)
        }}
    classes={{
      root classes.textField,
    }}
    fullWidth
/>

........

谁能帮我解决这个问题。

谢谢!!

【问题讨论】:

    标签: reactjs react-hooks material-ui


    【解决方案1】:

    您在&lt;InputAdornment/&gt; 组件中作为孩子使用的图像有问题。确保将其导入correctly。但是,根据文档,&lt;InputAdornment/&gt; 期望为孩子 - The content of the component, normally an IconButton or string.

    因此,您应该安装@material-ui/icons package 并在img 上使用它们的图标:

    import MailIcon from '@material-ui/icons/Mail';
    
    ...
    
    InputProps={{
                startAdornment: <InputAdornment position="start"><MailIcon/></InputAdornment>,
              }}
    
    

    Demo

    【讨论】:

    • 嘿!谢谢您的答复。是的,我确实尝试了一切,实际上,我是从 Icon 本身开始的。但是,问题仍然存在。我可以在代码沙箱中获得一些输入吗??
    • 我创建了一个工作演示并将其添加到我的答案中,请查看
    • 是的,现在您可以更改边框半径并在我的应用程序中检查图标正在消失。
    • 是的,成功了!!谢谢!实际上,我在颜色上犯了一个错误。天哪
    • 只是需要另一个帮助!我希望该标签以相同的方式制作动画。就像普通的轮廓 textFiled 一样,即使在有一个开始图标之后,它也会从占位符位置到顶部工作
    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2020-11-30
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    相关资源
    最近更新 更多