Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the height of Autocomplete component of Material UI?

Tags:

material-ui

Can anyone help me to reduce the height of Material UI Autocomplete component ? I am trying to use set the height property to 10 or 20 px though the classes property. But it does nothing. Also tried to reduce the height of the Textfield which wrapped by Autocomplete component, but when I tried to reduce the height of the Textfield component through InputProps, then the Items that were to be suggested in Autocomplete area don't display.

like image 360
Sabbir Ahmed Sristy Avatar asked Jan 26 '20 11:01

Sabbir Ahmed Sristy


People also ask

What is autocomplete in react?

The React AutoComplete is a textbox component that provides a list of suggestions to select from as the user types. It has several out-of-the-box features such as data binding, filtering, grouping, UI customization, accessibility, and more.

What is the default state to autocomplete?

By default, it'll have the initial value set into the TextField of the Autocomplete component but when the user makes any modifications, it calls up the Autocomplete options depending on the dataSource prop.

How do I use autocomplete in material UI?

There are two versions of the autocomplete that can be used: Combo box — Where the value of the text input must be chosen from a predefined set of values. Free solo — Where the text input can contain any value but is suggested from a list of possible values.


1 Answers

I am also customizing the Autocomplete component.

To reduce the height, I used the size attribute and removed the label attribute.

renderInput={(params) => <TextField {...params} variant="standard" size="small" />}

Make sure not to override the params provided by the Autocomplete component on your TextField component, which already include things like InputProps.

You'll probably want to use Autocomplete's CSS API to achieve all the customizations you're looking for. For example,

const useStyles = makeStyles(theme => ({
  inputRoot: {
    color: theme.palette.primary.contrastText,
  },
  popupIndicator: {
    color: theme.palette.primary.contrastText,
  },
  root: {
    padding: `0 ${theme.spacing(1)}px`,
  },
}))

const AutocompleteWrapper = ({
  value,
  onChange,
  options,
}) => {
  const classes = useStyles()

  return (
  <Autocomplete
    classes={classes}
    options={options}
    value={value}
    onChange={onChange}
    renderInput={(params) => <TextField {...params} variant="standard" size="small" />}
  />
  )
}
like image 167
cyrf Avatar answered Sep 30 '22 19:09

cyrf