What is the best way to eliminate the second x button, located on the left side, within a TextField contained in an Autocomplete

My search bar utilizes Autocomplete from material ui to suggest options, and it includes a TextField for inputting text.

The issue I'm facing is that when typing in the TextField, I notice two clear buttons (x button) - one on the left of the loading screen and another on the right. Once the loading screen disappears, these two buttons are displayed next to each other. I find the extra button on the left unnecessary and visually unappealing, but I'm unsure why it appears there.

https://i.stack.imgur.com/moeih.png

https://i.stack.imgur.com/CMwBG.png

Search.jsx:

<div className={searchClasses.search}>
      <Autocomplete
        options={isEmpty ? [] : suggestionsList}
        freeSolo
        style={{width: "100%"}}
        getOptionLabel={(option) => option.title}
        renderInput={(params) => (
          <TextField
            {...params}
            variant="outlined"
            margin="normal"
            required
            fullWidth
            autoFocus
            loading={loading}
            style={{margin: 0}}
            classes={{ notchedOutline: classes.input }}
            onChange={handleOnChange}
            onKeyDown={e => handleKeyDown(e)}
            placeholder="Search..."
            type="search"
            InputProps={{
              ...params.InputProps,
              startAdornment: (
                <InputAdornment position="start">
                  <SearchIcon />
                </InputAdornment>
              ),
              endAdornment: (
                <React.Fragment>
                  {loading ? <CircularProgress color="inherit" size={20} /> : null}
                  {params.InputProps.endAdornment}
                </React.Fragment>
              ),
              classes: { notchedOutline: classes.noBorder }
            }}
          />
        )}
        renderOption={(option, { inputValue }) => {
          const matches = match(option.title, inputValue);
          const parts = parse(option.title, matches);

          return (
            <div>
              {parts.map((part, index) => (
                <span
                  key={index}
                  style={{ fontWeight: part.highlight ? 700 : 400 }}
                >
                  {part.text}
                </span>
              ))}
            </div>
          );
        }}
      />
</div>

Answer №1

Solution

To remove the default cancel button in Webkit-based browsers like Chrome and Safari when using a

<input type="search">
, you can create a new CSS stylesheet named styles.css and include the following code:

input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none;
}

After creating this stylesheet, import it at the beginning of your Search.jsx file:

import "./styles.css";

/* Your custom code goes here ... */

Explanation

The reason why the "X" cancel button appears is because Webkit-based browsers automatically add this feature to all

<input type="search">
elements. By specifying the type="search" for your TextField component, it triggers the browser to display the "X" button.

To hide this default "X" button, we use the ::-webkit-search-cancel-button pseudo-element selector in our styles.css. This selector targets all default "X" buttons within

<input type="search">
elements and applies -webkit-appearance: none; to hide them from view.

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

Experiencing issues with sending log messages symbolicatedStack on react native and expo

After updating to the latest versions of react (0.62.2) and expo (37.0.12), most things were working fine. However, I kept encountering an error screen that would pop up each time I reloaded the app or shortly after an error occurred. console.error: There ...

The localhost server in my Next.js project is currently not running despite executing the 'npm run dev' command. When trying to access the site, it displays an error message saying "This site can't be

I attempted to install Next.js on my computer and followed the documentation provided to set up a Next.js project. You can find the documentation here. The steps I took were: Ran 'npx create-next-app@latest' Was prompted for the name of my proj ...

Setting Default Values for Multi-select in React-select Async

What is the method for setting default selected values in React-select Async Multi-select? Below is a sample form utilizing react-hook-form and react-select: <form onSubmit={handleSubmit(onSubmit)} > {updateError && renderError(updateError)} ...

Encountering difficulties while attempting to import a module in Next.js

My attempt to import the Zoom Web SDK module into my Next.js project encountered an error due to the undefined window object. Simply trying to import the websdk module resulted in this unexpected issue https://i.stack.imgur.com/NDRoC.png I am using Next ...

the next development and build process becomes frozen indefinitely

Each time I try to execute pnpm dev, it seems to hang at ready - started server on 0.0.0.0:3000, url: http://localhost:3000 without displaying any errors. However, the server does not actually start at localhost:3000 because the page doesn't load when ...

Tips for achieving expansion of solely the clicked item and not the whole row

I am trying to create a card that contains a cocktail recipe. The card initially displays just the title, and when you click on a button, it should expand to show the full menu and description. The issue I'm facing is that when I click on one element, ...

React: Struggling to retrieve the current inputbox event value

I have encountered an issue while working on a customized input box in react. Despite my efforts, I am unable to access the current event value. Here is the relevant code snippet: Parent Component:---------------------------------------------------------- ...

Using Redux to dispatch actions and dynamically update the URL address

I am working on an app that contains the following code: index.js const store = createStore(rootReducers, applyMiddleware(createLogger())); store.dispatch(fetchArticles()); ReactDOM.render( <Provider store={store}> <Router> ...

Using React form input event handler effectively

I have integrated an API to fetch country codes and store them in a select field as options. The user can select a country from the dropdown menu, and the corresponding calling code will automatically appear in an input box. My goal is to make sure that w ...

Updating state on a component that has been unmounted using context and hooks in React Native

UPDATE: I've implemented the code provided by the instructor in this post, however, despite using the state isMounted and the cleanup function in useEffect, I am still facing the same issue. The code appears to be functioning correctly, but I consiste ...

Encountering unanticipated breakpoints in compiled .Next files while using Visual Studio Code

As a newcomer to NextJS, I have encountered an issue that is disrupting my workflow. I followed the instructions in https://nextjs.org/docs/advanced-features/debugging#using-the-debugger-in-visual-studio-code to set up my launch.json file. Although I am ...

The issue of duplicate marks in MUI range sliders arises when there are two children that share identical keys

There seems to be a bug in the mui range slider where the marks get duplicated when both thumb sliders are moved all the way to the right. Initially, the slider looks like this: When both thumbs are slid to the far right, an error message appears in the ...

Tips for resolving the issue: Notification: incorrect value assigned to the `settaskstatus` property within the <span> element

After creating my own component as shown below: import React, { Component, Fragment } from 'react'; import Checkbox from '@material-ui/core/Checkbox'; import clsx from 'clsx'; import { makeStyles } from '@material-ui/cor ...

Turn off server queries while constructing the project

We currently have a docker image that sets up both the backend server and frontend NextJS application utilizing ApolloClient. However, during the build process of the NextJS application, the Apollo client attempts to query the backend server's graphql ...

Create a Referral Program page within a swapping interface similar to the functionalities found in platforms like PancakeSwap, PantherSwap, and

Currently, my goal is to create a referral program page similar to the one found at . I have explored the source code on GitHub for the PantherSwap interface, but unfortunately, I did not come across any references to that specific section. Would you be ...

Retrieve the Gatsby Component in a function

I've been trying to access a Gatsby component called Anime from outside of it, but I'm struggling to determine its instance name. Here's the code snippet: import React from 'react' import PropTypes from 'prop-types&ap ...

Having trouble with the useStyles feature in mui5?

I am having trouble customizing the styling of my icon. I've managed to change the background color successfully, but increasing the font size doesn't seem to work. I've tried using "!important" as I read, but it's not taking effect. It ...

What is the best way to access a value within a JSON object in a React render method?

Overview I am currently working on creating a blog using React and JSON data. Upon checking this.state.blogs, I am getting the output of: [object Object],[object Object],[object Object]. I have attempted to use the .map function but I am not sure how to ...

"Facing an issue where the Prisma migrate command becomes unresponsive

Trying to integrate Prisma with a local instance of Supabase using docker. A basic model has been created in the file prisma/schema.prisma: // Example Prisma schema file, // find more about it on: https://pris.ly/d/prisma-schema generator client { provi ...

How to Deactivate the Default Selection in React-Select

Having trouble with the focus in a React Select dropdown. The first item always gets focused when opening the dropdown, despite passing various props without success. I checked their GitHub for related issues around autofocus but couldn't find a solut ...