代码之家  ›  专栏  ›  技术社区  ›  user944513

如何在react的输入字段中显示搜索图标?

  •  0
  • user944513  · 技术社区  · 7 年前

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

    我正在使用 this API

    Here 是我的密码

    我可以展示 TextField 但我无法添加搜索图标。 你能加上怎么加吗?

     <TextField id="standard-bare" defaultValue="Bare" margin="normal" />
    
    0 回复  |  直到 7 年前
        1
  •  3
  •   mehamasum    7 年前

    你需要使用 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>
        2
  •  4
  •   Irfan Alam    7 年前

    你只需要输入政府

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

    像这样将inputprops添加到textfield

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

    请找到所附的IMG,以演示您所需的解决方案。

    enter image description here