How to make mui autocomplete with search icon in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To make mui autocomplete with a search icon in React js, first, import the search icon in mui library. you can use textfield input props with an endAdornment, in the endAdornment pass your icon. It will show the icon autocomplete.

Today, I am going to show you, how to make mui autocomplete with search icon in react js.

Installation

Install the following packages to use mui autocomplete with search icon in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

MUI material make mui autocomplete with search icon example.

The below code is an example of Autocomplete is expanding the InputProps object from the params argument passed to the renderInput function to create the search icon, in this case, in TextField, it is adding the endAdornment property to the InputProps object. The endadornment property specifies that the search icon should be at the end of the TextField.

App.js

import * as React from "react";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import SearchIcon from "@mui/icons-material/Search";
import InputAdornment from "@mui/material/InputAdornment";

export default function ComboBox() {
  return (
    <Autocomplete
      disablePortal
      id="combo-box-demo"
      options={top100Films}
      role="list-box"
      sx={{ width: 300 }}
      renderInput={(params) => (
        <TextField
          {...params}
          label="Movie"
          InputProps={{
            ...params.InputProps,
            endAdornment: (
              <InputAdornment position="end">
                <SearchIcon />
              </InputAdornment>
            )
          }}
        />
      )}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { label: "The Shawshank Redemption", year: 1994 },
  { label: "The Godfather", year: 1972 },
  { label: "The Godfather: Part II", year: 1974 },
  { label: "The Dark Knight", year: 2008 },
  { label: "12 Angry Men", year: 1957 },
  { label: "Schindler's List", year: 1993 },
  { label: "Pulp Fiction", year: 1994 },
  {
    label: "The Lord of the Rings: The Return of the King",
    year: 2003
  },
  { label: "The Good, the Bad and the Ugly", year: 1966 },
  { label: "Fight Club", year: 1999 },
  {
    label: "The Lord of the Rings: The Fellowship of the Ring",
    year: 2001
  },
  {
    label: "Star Wars: Episode V - The Empire Strikes Back",
    year: 1980
  },
  { label: "Forrest Gump", year: 1994 },
  { label: "Inception", year: 2010 },
  {
    label: "The Lord of the Rings: The Two Towers",
    year: 2002
  },
  { label: "One Flew Over the Cuckoo's Nest", year: 1975 },
  { label: "Goodfellas", year: 1990 },
  { label: "The Matrix", year: 1999 },
  { label: "Seven Samurai", year: 1954 },

  { label: "Monty Python and the Holy Grail", year: 1975 }
];

In the above code example, I have used the @mui/material component and made mui autocomplete with search icon.

Check the output of the above code example.

React, Mui, search, icon

Here, we are provided code sandbox links for the above program make mui autocomplete with search icon. Then you can use whenever you want and do the changes as per your requirements.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts