Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove placeholder text from MUI Autocomplete, TextField when clicked?

I’m using Material-UI (MUI) Autocomplete with a TextField, and I want to achieve a specific behavior. Currently, when I click on the search bar, the placeholder text moves to the top of the TextField. However, I want to completely remove the placeholder text when the search bar is clicked.

Here’s my current code:

import * as React from 'react';
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';

const top100Cars = [
  { label: 'Toyota' },
  { label: 'Ford' },
  { label: 'Chevrolet' },
  { label: 'Tesla' },
  { label: 'Honda' },
  { label: 'BMW' },
  { label: 'Audi' },
  { label: 'Mercedes-Benz' },
  { label: 'Ferrari' },
  { label: 'Porsche' },
  { label: 'Lamborghini' },
  { label: 'Nissan' },
  { label: 'Volkswagen' },
  { label: 'Mazda' },
  { label: 'DeLorean' },
  { label: 'Dodge' },
];



export default function searchBar() {
  // const [selectedBrand, setSelectedBrand] = React.useState(null);
  return (
    <Autocomplete
      disablePortal
      id="combo-box-demo"
      options={top100Cars}
      // value={selectedBrand}
      // onChange={(event, newValue) => {
      //   setSelectedBrand(newValue);
      // }}
      sx={{
        width: 254, backgroundColor: 'white', borderRadius: 50,
      }}
      renderInput={(params) => (
        <TextField
          {...params}
          label="Марка"
          size='small'
        />
      )}
    />
  );
}

This is how it looks right now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is how it looks right now!

I want to remove the placeholder text that appears in the upper left corner when clicked on the text field. I hope this makes sense to you!

I’ve tried setting InputLabelProps and InputProps properties with various configurations, but the placeholder text still remains. How can I modify my code to achieve the desired behavior of removing the placeholder text when the search bar is clicked?

>Solution :

You could try to place an empty lable in the textField, like this :

<TextField
    id="standard-full-width"
    label=""
    placeholder="Placeholder"
/>

As suggested here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading