【问题标题】:Add icon inside text input in Material UI and React在 Material UI 和 React 的文本输入中添加图标
【发布时间】:2020-05-17 10:22:51
【问题描述】:

我有一个这种形式的文本输入:

  <FormControl className='searchOrder'>
    <input
      className='form-control'
      type='text'
      placeholder='Search order'
      aria-label='Search'
      value={this.number}
      input={<Input />}
      onChange={this.handleChangeOrderNumber}
    />
  </FormControl>

它在功能上运行良好,但我想在输入的左侧添加一个搜索图标,如下所示:

我不知道在哪里添加该代码 sn-p,我尝试了内部输入,但它抛出错误,所以我把它放在后面,但屏幕上没有出现任何图标。

  <FormControl className='searchOrder'>
    <input
      className='form-control'
      type='text'
      placeholder='Search order'
      aria-label='Search'
      value={this.number}
      input={<Input />}
      onChange={this.handleChangeOrderNumber}
    />
    <span>
      <i class='fas fa-search'></i>
    </span>
  </FormControl>

有什么办法解决这个问题吗?

如果相关,searchOrder css 类:

.searchOrder {
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 20%;
  max-width: 250px;
  height: 20%;
  left: 0px;
  top: 0px;
  background: #ffffff;
  border: 1px solid #d9d9d9;
  box-sizing: border-box;
  border-radius: 4px;
}

更新:

我也尝试过 InputAdornment,但图标仍然没有出现。

  <FormControl className='searchOrder'>
    <InputLabel htmlFor='input-with-icon-adornment'></InputLabel>
    <input
      className='form-control'
      type='text'
      id='input-with-icon-adornment'
      placeholder='Search order'
      aria-label='Search'
      value={this.number}
      input={<Input />}
      onChange={this.handleChangeOrderNumber}
      startAdornment={
        <InputAdornment position='start'>
          <SearchIcon />
        </InputAdornment>
      }
    />
  </FormControl>

【问题讨论】:

  • @VivekDoshi 我试着这样做,他们的例子工作正常,但在我的情况下它仍然不起作用。我也用那个尝试更新了这个问题。它不与我那里的其他属性兼容吗?

标签: javascript html css reactjs material-ui


【解决方案1】:

只需使用材质 ui Input 这样的组件:

 <Input
        id="input-with-icon-adornment"
        startAdornment={
          <InputAdornment position="start">
            <SearchIcon />
          </InputAdornment>
        }
  />

阅读更多关于它here

【讨论】:

  • 我试着这样做,他们的例子工作正常,但在我的情况下它仍然不起作用。我也用那个尝试更新了这个问题。它不与我那里的其他属性兼容吗?
  • 你不能在input元素中使用startAdornment prop,你需要使用Input来自import Input from '@material-ui/core/Input';的组件
猜你喜欢
  • 1970-01-01
  • 2022-11-11
  • 2019-09-18
  • 1970-01-01
  • 2021-09-27
  • 2021-02-02
  • 2017-08-03
  • 2023-03-04
  • 2021-03-22
相关资源
最近更新 更多