How to use icon in mui autocomplete in react js?
February 13, 2023Hi Friends 👋,
Welcome To aGuideHub!
To use icon in mui autocomplete in React js, you can use this code InputProps={{...params.InputProps,}}
in TextField, it is adding the endAdornment
property to the InputProps
object. The endadornment
property specifies that the refreshicon should be at the end of the TextField. It will put the icon in mui autocomplete in react js.
Today, I am going to show you, how to use icon in mui autocomplete in react js.
Installation
Install the following packages to use icon in mui autocomplete in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
MUI material use icon in mui autocomplete 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 RefreshIcon
, in this case, in TextField, it is adding the endAdornment
property to the InputProps
object. The endadornment
property specifies that the refreshicon 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 RefreshIcon from '@mui/icons-material/Refresh';
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">
<RefreshIcon />
</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 used the icon in mui autocomplete.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program use icon in mui autocomplete. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍