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

时间:2019-03-03 15:20:36

标签: reactjs redux material-design material-ui material

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

我正在使用this API

Here是我的代码

我可以显示TextField,但不能添加搜索图标。 您能否添加添加方式?

 <TextField id="standard-bare" defaultValue="Bare" margin="normal" />

2 个答案:

答案 0 :(得分:2)

您需要使用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>

答案 1 :(得分:2)

您只需要导入inputAdornment

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

像这样将InputProps添加到textField

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

plz找到随附的img,以获取所需解决方案的演示。

enter image description here