Material-UI:如何在输入字段中显示搜索图标?

问题描述 投票:0回答:2

我正在使用material ui。我想在输入字段中显示search icon,如图像enter image description here所示

我正在使用this API

Here是我的代码

我能够显示TextField但我无法添加搜索图标。你可以加一下如何添加吗?

 <TextField id="standard-bare" defaultValue="Bare" margin="normal" />
reactjs redux material-design material-ui material
2个回答
3
投票

你需要使用Input Adornments

例如:

// imports
import IconButton from "@material-ui/core/IconButton";
import InputAdornment from "@material-ui/core/InputAdornment";
import SearchIcon from "@material-ui/icons/Search";

// render

<TextField
  label="With normal TextField"
  InputProps={{
    endAdornment: (
      <InputAdornment>
        <IconButton>
          <SearchIcon />
        </IconButton>
      </InputAdornment>
    )
  }}
/>

这是一个演示:

const {
  TextField,
  InputAdornment,
  IconButton,
  SearchIcon,
  Icon
} = window['material-ui'];

class App extends React.Component {
  render() {
    return (
      <TextField
        label="With normal TextField"
        InputProps={{
          endAdornment: (
            <InputAdornment>
              <IconButton>
                <Icon>search</Icon>
              </IconButton>
            </InputAdornment>
          )
        }}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

<div id="root"></div>

4
投票

您只需要导入inputAdornment

import InputAdornment from '@material-ui/core/InputAdornment';

并将InputProps添加到textField中

InputProps={{
  endAdornment: (
    <InputAdornment position="start">
      <SearchIcon />
    </InputAdornment>
   )
  }}

请查看附图,以获得所需解决方案的演示。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.