How to customize the selected option style in MUI Autocomplete component?
P粉769413355
P粉769413355 2023-08-14 10:47:21
0
1
597
<p>The question is like the title says, how can I change the style of the selected option in the MUI when using the <code>multi</code> option. I want to change the style of all highlighted options. Your help is greatly appreciated, thank you! </p>
P粉769413355
P粉769413355

reply all(1)
P粉022140576

You can use the renderOption attribute to render MUI Autocomplete, the code is as follows:

    <Autocomplete
        multiple
        id="tags-standard"
        value={value}
        onChange={(event, value) => setValue(value)}
        options={top100Films}
        getOptionLabel={(option) => option.title}
        renderInput={(params) => (
          <TextField
            {...params}
            variant="standard"
            label="Multiple values"
            placeholder="Favorites"
          />
        )}
        renderOption={(props, option, state) => {
          return (
            <li
              {...props}
              style={{
                ...props.style,
                ...(state.selected ? { backgroundColor: "yellow" } : {})
              }}
            >
              {option.title}
            </li>
          );
        }}
      />

This is a Codesandbox link.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template