Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Autocomplete tag - Material UI

I'm using autocomplete of material ui and this is what default tag looks like

enter image description here

I want to customize tag like this.

enter image description here

How can i do that? Thank you.

            <Autocomplete
              disableCloseOnSelect={true}
              multiple
              options={this.options}
              getOptionLabel={options => options.title}
              value={this.state.value}
              onChange={(e, techs) => {
                this.newValue(techs);
              }}
              renderInput={params => (
                <TextField
                  {...params}
                  variant="outlined"
                  placeholder={Technology}
                  fullWidth
                />
              )}
            ></Autocomplete>
like image 369
randomguy Avatar asked Dec 03 '19 09:12

randomguy


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 change the height of autocomplete material UI?

How can you reduce the height of the autocomplete component of material-UI? To reduce the height, I used the size attribute and removed the label attribute. Make sure not to override the params provided by the Autocomplete component on your TextField component, which already include things like InputProps .

How do you clear the selected value in autocomplete material UI?

clearOnBlur={true} helps to clear the value of the autocomplete component when it loses focus.

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.


2 Answers

The renderTags prop from the Autocomplete API docs: https://material-ui.com/api/autocomplete/

The "tags" are Material UI Chips https://material-ui.com/components/chips/ so you can style an individual Chip component or variants to your liking and then override the default tags for Autocomplete.

Your styling for the chip would look something like

import { makeStyles } from '@material-ui/core/styles';
import Chip from '@material-ui/core/Chip';

export const useStyles = makeStyles((theme) => ({
  root: {
    borderRadius: 0,
    color: labelColor,
    boxSizing: 'border-box',
    border: '1px solid',
    borderColor: '#bddaff',
  }
}));
;

const MyChip = (props) => {
  const classes = useStyles();

  return (
      <Chip
        className={classes.root}
        {...props}
      />
  );
};

And then you override the default chips

  <Autocomplete
    getOptionLabel={(option) => option.title}
    label
    placeHolder
    multiple
    openOnFocus
    renderInput={(params) => <TextField {...params} label={label} placeholder={placeHolder} />}
    renderTags={(tagValue, getTagProps) => {
      return tagValue.map((option, index) => (
        <MyChip {...getTagProps({ index })} label={option.title} />
      ));
    }}
    {...rest}
  />
);
like image 83
Jon Staples Avatar answered Oct 31 '22 05:10

Jon Staples


You can use the tag CSS class to customize the tags as shown below.

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

const CustomAutocomplete = withStyles({
  tag: {
    backgroundColor: "#a0a",
    height: 24,
    position: "relative",
    zIndex: 0,
    "& .MuiChip-label": {
      color: "#fff"
    },
    "& .MuiChip-deleteIcon": {
      color: "red"
    },
    "&:after": {
      content: '""',
      right: 10,
      top: 6,
      height: 12,
      width: 12,
      position: "absolute",
      backgroundColor: "white",
      zIndex: -1
    }
  }
})(Autocomplete);

export default function Tags() {
  return (
    <div style={{ width: 500 }}>
      <CustomAutocomplete
        multiple
        id="tags-standard"
        options={top100Films}
        getOptionLabel={option => option.title}
        defaultValue={[top100Films[13]]}
        renderInput={params => (
          <TextField
            {...params}
            variant="standard"
            label="Multiple values"
            placeholder="Favorites"
            margin="normal"
            fullWidth
          />
        )}
      />
    </div>
  );
}


// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { title: "The Shawshank Redemption", year: 1994 },
  { title: "The Godfather", year: 1972 },
  { title: "The Godfather: Part II", year: 1974 },
// ... plus many more
];

Edit Material demo

like image 30
Ryan Cogswell Avatar answered Oct 31 '22 06:10

Ryan Cogswell