Make sure that "X" is always displayed on Autocomplete for removing content

Is there a way to keep the "x" always visible in MUI Autocomplete instead of only showing it on hover? I want the X icon to be displayed constantly, not just when hovering over the dropdown.

To better illustrate my question, you can refer to this example: https://codesandbox.io/s/combobox-material-demo-forked-obbuu9

In the current setup, the X appears when you hover over the dropdown and allows you to delete the content. But is there a way to make sure that X is always present regardless of mouse interaction?

Answer №1

Make a simple modification to the sx property like this:

sx={{
    width: 300,        
    "& button.MuiButtonBase-root" : {
      visibility: "visible"
    }
  }}

Answer №2

Explore the possibilities of CSS with this example:

const ExampleComponent = () => {
  return (
    <Autocomplete
      disablePortal
      id="combo-box-demo"
      options={top100Films}
      value={top100Films[0]}
      sx={{
        width: 300,
        "& button[title='Clear']": {
          visibility: "visible"
        }
      }}
      renderInput={(params) => <TextField {...params} label="Movie" />}
    />
  );
}

Check out the live demo on codesandbox: https://codesandbox.io/s/combobox-material-demo-forked-7u1noe?file=/demo.js:137-511

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pass Target Value Along with onClick Event in React

Is there a way to have one Display function that can control two Popper elements from Material UI independently? I attempted to assign different targets to each Popper element, but couldn't make it work using event.target.target. How can I achieve thi ...

What could be the reason behind Typescript's unexpected behavior when handling the severity prop in Material UI Alerts?

Trying to integrate Typescript into my react project and encountering a particular error: Type 'string' is not assignable to type 'Color | undefined'. The issue arises when I have the following setup... const foo = {stuff:"succes ...

Tips for quietly printing a PDF document in reactjs?

const pdfURL = "anotherurl.com/document.pdf"; const handleDirectPrint = (e: React.FormEvent) => { e.preventDefault(); const newWin: Window | null = window.open(pdfURL); if (newWin) { newWin.onload = () => ...

Leveraging Nextjs Link alongside MUI Link or MUI Button within a different functional component (varieties)

Currently in my development setup, I am utilizing Next.js (10.2) and Material-UI (MUI) with Typescript. In the process, I have implemented a custom Link component: Link.tsx (/components) [...] On top of that, I have created another iteration which functi ...

Ensure that the Map reference object is accessible when the first modal window is displayed or opened

My issue involves a table where selecting a row should display the item's location on a map using react-map-gl in a Dialog component. However, upon clicking the row, the mapref returns null during the initial render, even though it should provide mapr ...

Encountering an anomaly in persistent redux while using NEXT.JS

I recently made some updates to my ecommerce store, including adding login and register options. To ensure that user tokens are saved even after a page refresh, I decided to implement redux-persist. However, I encountered an issue where the design breaks a ...

Is there a way to reach the redux store from an action creator?

I am currently exploring the concept of chaining actions together within my application. When the SET_CURRENT_USER action is triggered, I want it to not only modify the state by setting the current user, but also initiate a series of other side-effect task ...

Using ReactJS Fetch for API calls across various components is a common practice

As a beginner in reactJS, I am looking for advice on how to efficiently manage API calls in one centralized location. Is there a way to easily change the URL and query string by passing parameters and receiving the returned data? For example, in Angular ...

Display some text after a delay of 3 seconds using setTimeOut function in ReactJS

When using React JS, I encountered an issue where I am able to display text in the console after 2 seconds, but it is not appearing in the DOM. const items = document.getElementById("items"); const errorDisplay = () => { setTimeout(function () { item ...

Date selection feature in Material UI causing application malfunction when using defaultValue attribute with Typescript

Recently, I discovered the amazing functionality of the Material UI library and decided to try out their date pickers. Everything seemed fine at first, but now I'm facing an issue that has left me puzzled. Below is a snippet of my code (which closely ...

Interactive sliding panel in Material Design UI

I am experiencing an issue with the Swipeable drawer feature from Material UI. In a normal Drawer, it is not necessary to add a function for onOpen. So my question is, what should I include there? container={container} variant=" ...

What is the advantage of using require() over url/path for displaying images in react native?

What is the rationale behind using require() rather than directly using the URL or path of an image to display images in React Native? Is there a specific advantage to using require()? ...

Having trouble removing tag elements after a successful API call? Let me show you how it's done!

Utilizing React-TagEditor with the latest version of react has been a seamless experience. However, I've encountered an issue when trying to clear the added tags after saving all records - the tags remain uncleared. Can anyone provide guidance on how ...

The specified instant cannot be located in 'moment' while attempting to import {Moment} from 'moment' module

Struggling in a reactJS project with typescript to bring in moment alongside the type Moment Attempted using import moment, { Moment } from 'moment' This approach triggers ESLint warnings: ESLint: Moment not found in 'moment'(import/n ...

Struggling with passing data to a child component in React

I am currently working on a gallery project where users can click on an image to view it in a separate component using props. The images are sourced from a hardcoded array, and the challenge lies in connecting this data to the component accurately. I have ...

Utilize import and export statements to transfer an HTML tag between two JavaScript files

I have two HTML files linked to two JS files. I want to save an HTML tag from HTML1 with JS1 in a Variable and export it. Then import it in the JS2 file and use it in HTML2 I have tried many ways but nothing seems to work, something as simple as this Exp ...

Implementing an AutoComplete feature within the AppBar component in Material UI using ReactJS

Recently, I've been exploring ReactJS and managed to create a functional AppBar with a nested TextField. Everything was running smoothly until I decided to replace the TextField with an AutoField. Surprisingly, there were no error messages, but now my ...

Error Message: A key is being provided to the classes property that is not implemented in the current context

Trying to customize Material-UI styles with makeStyles() as outlined in the documentation but encountering a warning when passing a classname in the parent component that is not specified in useStyles. The warning message reads: Warning: Material-UI: th ...

How to trigger a function upon receiving data from withTracker in React Meteor?

When retrieving data from the server and using withTracker, I am facing an issue. Here is a snippet of my code: export default withTracker(() => { let towndatasub = Meteor.subscribe("userTownDataPublisher",Meteor.userId()); let resourcedatas ...

Encounter issues with launching React app in vscode due to ESLint version

I am facing a challenging problem that I cannot seem to solve on my own. I have been using ESLint in VSCode for all of my projects without any issues. However, when I recently created a new React app and tried to run it using npm start or yarn start, it s ...