Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change styles of Material-UI Autocomplete list?

I want to change styles of list/dropdown (not the input) of Autocomplete component in Material-UI. I'm using material-styles for styling.

enter image description here

I would like this list to be more visible from the background so maybe increase the box-shadow.

How can I do this?

like image 979
Mimoid Avatar asked Jan 06 '21 11:01

Mimoid


People also ask

How do you style material-UI autocomplete?

The Material-UI Autocomplete component and its Popper component can be styled by targeting built-in MUI styling APIs. The Popper is also called a dropdown or listbox. The difference is that Popper is the positioning element while Listbox is the visible list element.

How do I get selected values material-UI autocomplete?

To get the value in the React Material-UI autocomplete, we can get the selected values from the onChange callback. We add the Autocomplete from the @material-ui/lab module. And we set the options prop to the top5films array to add the options for the autocomplete.

How do I add styling to material-UI?

Luckily, Material-UI provides a solution for this: makeStyles . Using makeStyles , you can add CSS in JS without making your code look messy. First, you need to import makeStyles in your app. Next, pass all the CSS you need in your app as an object to makeStyles and store it inside a variable, useStyles .

What is material-UI for?

Material-UI is simply a library that allows us to import and use different components to create a user interface in our React applications. This saves a significant amount of time since the developers do not need to write everything from scratch.

How do I style the autocomplete options?

If you want to style the Autocomplete’s text color, text decoration, font, or control a variety of other styling options, the Popper component is really what you need to target. Also, if you want to set the height of the box containing the Autocomplete options, make sure to set minHeight instead of setting height.

How do I customize material UI components?

Learn how to customize Material UI components by taking advantage of different strategies for specific use cases. Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization

How do I add style overrides to a component?

The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can be used with all Material UI components. To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop.

How do I customize a component's styles?

Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization To change the styles of one single instance of a component, you can use one of the following options:


1 Answers

One way of doing this is by adjusting the elevation of the Paper component used by Autocomplete. The default elevation is 1. The example below uses a value of 8 by specifying that in a custom Paper component (CustomPaper) which is then provided via the PaperComponent prop of Autocomplete.

import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import Paper from "@material-ui/core/Paper";

const CustomPaper = (props) => {
  return <Paper elevation={8} {...props} />;
};
export default function ComboBox() {
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      PaperComponent={CustomPaper}
      renderInput={(params) => (
        <TextField {...params} label="Combo box" variant="outlined" />
      )}
    />
  );
}

Edit Custom Paper in Autocomplete

If the elevation prop does not give you the look you want, you can customize styles of the Paper component via the classes prop of Autocomplete as in the example below which uses a very ugly border for demonstration purposes, but you can make whatever CSS change you want to make using the same approach.

import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  paper: {
    border: "5px solid black"
  }
});
export default function ComboBox() {
  const classes = useStyles();
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      classes={{ paper: classes.paper }}
      renderInput={(params) => (
        <TextField {...params} label="Combo box" variant="outlined" />
      )}
    />
  );
}

Edit Custom Paper in Autocomplete

like image 110
Ryan Cogswell Avatar answered Oct 11 '22 02:10

Ryan Cogswell