Saturday, November 23, 2024
Google search engine
HomeLanguagesHow to add function to the clear button icon in Material UI...

How to add function to the clear button icon in Material UI Autocomplete ?

Material UI is a popular UI component library for React applications, providing a wide range of pre-built components that follow the Material Design guidelines. One of the components offered by Material UI is the Autocomplete component, which allows users to select values from a predefined list or search for specific options. In this article, we will explore how to add a function to the clear button icon in the Material UI Autocomplete component.

The Autocomplete component in Material UI comes with a built-in clear button icon that appears when a value is selected or input text is present. Clicking the clear button icon clears the selected value or the entered text. By default, the clear button only resets the component’s internal state without triggering any custom functionality.

To add a function to the clear button icon, we need to override the default behavior and provide our own implementation.

Creating React Application And Installing Module:

Step 1: Create a React application using the following command.

npx create-react-app foldername

Step 2: After creating your project folder, i.e., folder name, move to it using the following command:

cd foldername

Step 3: Install required modules

npm install @material-ui/core
npm install @material-ui/lab

Project Structure: 

Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

Javascript




import React, { Component } from 'react';
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
 
class App extends Component {
 
    constructor(props) {
        super(props);
        this.state = {
            list: [
                { title: "Title 1" },
                { title: "Title 2" },
                { title: "Title 3" },
                { title: "Title 4" },
            ]
        }
    }
 
    componentDidMount() {
        // Take the Reference of Close Button
        const close = document.getElementsByClassName(
            "MuiAutocomplete-clearIndicator"
        )[0];
 
        // Add a Click Event Listener to the button
        close.addEventListener("click", () => {
            alert("Add your Own Functionality Here...");
        });
    }
 
    render() {
        return (
            <Autocomplete
                options={this.state.list}
                getOptionLabel={(option) => option.title}
                style={{ width: 300 }}
                renderInput={(params) => (
                    <TextField {...params} label="Combo box" variant="outlined" />
                )}
            />
        );
    }
 
}
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments