Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Autocomplete] Access default getOptionLabel in renderOption #38048

Closed
2 tasks done
emlai opened this issue Jul 19, 2023 · 7 comments · Fixed by #38100
Closed
2 tasks done

[Autocomplete] Access default getOptionLabel in renderOption #38048

emlai opened this issue Jul 19, 2023 · 7 comments · Fixed by #38100
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material

Comments

@emlai
Copy link
Contributor

emlai commented Jul 19, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

We are overriding the default renderOption but we cannot access getOptionLabel in the custom renderOption function, to get the correct label.

I propose exposing getOptionLabel via the ownerState parameter of renderOption, i.e. adding it to this object:

const ownerState = {
...props,
disablePortal,
expanded,
focused,
fullWidth,
hasClearIcon,
hasPopupIcon,
inputFocused: focusedTag === -1,
popupOpen,
size,
};

Examples 🌈

This is our component override:

 defaultProps: {
    // ...
    renderOption: (
      props,
      option: React.ReactNode | { label: React.ReactNode },
      { selected },
      ownerState
    ) => {
      return (
        <li {...props}>
          {selected && <CheckIcon />}
          {
            // Work around not having access to the getOptionLabel prop here.
            typeof option === "object" && option != null && "label" in option
              ? option.label
              : option

            // Ideally the above code would be just this: (so it would work with other label props as well)
            ownerState.getOptionLabel(option)
          }
        </li>
      );
    },
  },

Motivation 🔦

If this feature is not added, we would need to drop support for getOptionLabel in our lib in order to use renderOption.

@emlai emlai added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jul 19, 2023
@emlai
Copy link
Contributor Author

emlai commented Jul 19, 2023

Actually, getOptionLabel is passed in ownerState if a user passes a custom getOptionLabel. But the default getOptionLabel is not passed. So this seems more like a bug.

@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Jul 20, 2023
@ZeeshanTamboli
Copy link
Member

You are correct; there is a bug. We forgot to pass the default getOptionLabel prop to ownerState in #36971. All props with default values should be explicitly passed to ownerState.

The addition of ownerState as a fourth parameter to renderOption was to enable global customization of different options (#33423) in the pull request #36971, which caused this issue to surface up. You can see the demo here - https://mui.com/material-ui/react-autocomplete/#globally-customized-options. This change was released in v5.14.0. I'm glad to see that you are using it.

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jul 20, 2023
@ZeeshanTamboli ZeeshanTamboli changed the title [Autocomplete] Access getOptionLabel in renderOption [Autocomplete] Access default getOptionLabel in renderOption Jul 20, 2023
@ZeeshanTamboli ZeeshanTamboli removed their assignment Jul 20, 2023
@emlai
Copy link
Contributor Author

emlai commented Jul 21, 2023

Also noticed that the internal value is not passed to ownerState. (I need it for custom styles set only when an option is selected.)

@ZeeshanTamboli
Copy link
Member

@emlai Would you like to create a PR?

@emlai
Copy link
Contributor Author

emlai commented Jul 21, 2023

No, we already have a workaround in place

@DSK9012
Copy link
Contributor

DSK9012 commented Jul 22, 2023

@ZeeshanTamboli
I've fixed the issue and raised PR. Please review.

@DSK9012
Copy link
Contributor

DSK9012 commented Jul 22, 2023

Also noticed that the internal value is not passed to ownerState. (I need it for custom styles set only when an option is selected.)

@emlai, Which value your referring? Coz I see both TextField inputValue and selected options value is accessible in renderOption.

  • inputValue you can access from option state - 3rd param.
  • value you can access from ownerState - 4th param.

Moreover, if you want to style based on selected. This is also available from option state param.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants