Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI: How to show search icon in input field in react?

I am using material ui. I want to show search icon in input field as shown in image enter image description here

I am using this API

Here is my code

I am able to show TextField but I am not able to add a search icon. Could you please add how to add ?

 <TextField id="standard-bare" defaultValue="Bare" margin="normal" />
like image 250
user944513 Avatar asked Mar 03 '19 15:03

user944513


People also ask

How do I add a search icon in Reactjs?

After opening react-icons, now from menu select category and name of icon you want to add. After clicking, on right hand side you will see many icons and there names.


Video Answer


2 Answers

You need to use Input Adornments.

For example:

// 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>
    )
  }}
/>

Here is a demo:

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>
like image 102
mehamasum Avatar answered Sep 20 '22 09:09

mehamasum


You simply need to import inputAdornment

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

and add InputProps to textField like this

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

plz find the attached img for demo of your required solution.

enter image description here

like image 24
Irfan Alam Avatar answered Sep 19 '22 09:09

Irfan Alam