How to remove clear button from mui autocomplete in react js?
February 16, 2023Hi Friends 👋,
Welcome To aGuideHub!
To remove clear button from mui autocomplete in React js, you have to put disableClearable
property inside autocomplete. It will remove the clear button from autocomplete. if you’re using mui autocomplete with textfield type: "search"
. Then you have to remove the type attribute
from textfield.
Today, I am going to show you, how to remove clear button from mui autocomplete in react js.
Installation
Install the following packages to remove clear button from 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 remove clear button from mui autocomplete example.
The below code is an example of, to remove the button from autocomplete, you have to put disableClearable
property inside autocomplete. It will remove the clear button from autocomplete. if you’re using mui autocomplete with textfield type: "search"
. Then you have to remove the type attribute
from textfield.
App.js
import * as React from "react";
import TextField from "@mui/material/TextField";
import Stack from "@mui/material/Stack";
import Autocomplete from "@mui/material/Autocomplete";
export default function FreeSolo() {
return (
<Stack spacing={2} sx={{ width: 300 }}>
<Autocomplete
id="free-solo-demo"
freeSolo
disableClearable
options={top100Films.map((option) => option.title)}
renderInput={(params) => <TextField {...params} label="freeSolo" />}
/>
<Autocomplete
freeSolo
id="free-solo-2-demo"
disableClearable
options={top100Films.map((option) => option.title)}
renderInput={(params) => (
<TextField
{...params}
label="Search input"
InputProps={{
...params.InputProps
// type: "search"
}}
/>
)}
/>
</Stack>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: "The Shawshank Redemption", year: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 },
{ title: "The Dark Knight", year: 2008 },
{ title: "12 Angry Men", year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: "Pulp Fiction", year: 1994 },
{
title: "The Lord of the Rings: The Return of the King",
year: 2003
},
{ title: "The Good, the Bad and the Ugly", year: 1966 },
{ title: "Fight Club", year: 1999 },
{
title: "The Lord of the Rings: The Fellowship of the Ring",
year: 2001
},
{
title: "Star Wars: Episode V - The Empire Strikes Back",
year: 1980
},
{ title: "Forrest Gump", year: 1994 },
{ title: "Inception", year: 2010 },
{
title: "The Lord of the Rings: The Two Towers",
year: 2002
},
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: "Goodfellas", year: 1990 },
{ title: "The Matrix", year: 1999 },
{ title: "Seven Samurai", year: 1954 },
{
title: "Star Wars: Episode IV - A New Hope",
year: 1977
},
{ title: "Monty Python and the Holy Grail", year: 1975 }
];
In the above code example, I have used the @mui/material
component and removed clear button from mui autocomplete.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program removed clear button from mui autocomplete. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍