question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Autocomplete] Pass `getOptionLabel` as a parameter to `renderOption`

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

The renderOption callback of the Autocomplete component could receive the components getOptionLabel callback a a fourth parameter. This would allow customizing the rendering of the options globally in the theme, while still being able to define the property to be shown on an individual basis.

Maybe even the whole AutocompleteOwnerState could be provided, as It has already been done for renderTags: https://github.com/mui/material-ui/pull/32637

Examples 🌈

No response

Motivation 🔦

Let’s say I want to add a ripple effect to all of my Autocomplete options. In the current version, I could define the renderOption property as a defaultProp for Autocomplete in the Theme.

Theme.tsx

MuiAutocomplete: {
    defaultProps: {
      renderOption: (props: HTMLAttributes<HTMLLIElement>, option: any, state: AutocompleteRenderOptionState) => {
        return <ButtonBase>{option.label}</ButtonBase>;
      },
    },
},

However this approach would have to assume that the type of the options every Autocomplete in the app receives has to be equal. I suppose the getOptionLabel exists exactly for this purpose, but I cannot access it in the theme. When this would be the case, I could access the function in the theme and provide the getOptionLabel individually in every Autocomplete instance I use:

Theme.tsx

MuiAutocomplete: {
    defaultProps: {
      renderOption: (props: HTMLAttributes<HTMLLIElement>, option: any, state: AutocompleteRenderOptionState, getOptionLabel) => {
        return <ButtonBase>{getOptionLabel(option)}</ButtonBase>;
      },
    },
},

MyComponent.tsx

type Option = { name: string }

const MyComponent = (options: Option[]) => {
  return <Autocomplete options={options} getOptionLabel={(option) => option.name} .../>
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
FionaDLcommented, Oct 17, 2022

@ZeeshanTamboli Do you mind me asking if you are working on this? I was thinking about possibly picking it up if you aren’t.

1reaction
ZeeshanTambolicommented, Jul 14, 2022

@ZeeshanTamboli I can try, but it will take some time.

No problem at all!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get Material-UI AutoComplete renderOption to work for ...
... options' ids as the actual value to pass to the form handler so I use the built in function getOptionLabel={option => `${option.id}`}...
Read more >
Customize MUI Autocomplete with getOptionLabel ...
renderInput requires a function so that parameters can be passed to the custom component you pass as the Input component. renderOption controls ...
Read more >
A Guide on Material UI AutoComplete in React - Refine Dev
Steps we'll cover: What is Material UI? Getting Started with MUI Autocomplete; MUI Autocomplete props. RenderInput. GetOptionLabel ...
Read more >
Autocomplete API - Material UI - MUI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
Demystifying Errors in MUI Autocomplete — part — I - Medium
With respect to the input field, Autocomplete is an abstract input field… ... getOptionlabel expects a function that has a default parameter ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found