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] Non-freesolo autocomplete acts like freesolo: passes string to getOptionLabel instead of option #36045

Closed
2 tasks done
HansBrende opened this issue Feb 4, 2023 · 8 comments · Fixed by #36088
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module! regression A bug, but worse

Comments

@HansBrende
Copy link

HansBrende commented Feb 4, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Steps:

  1. Make a normal auto-complete (not free solo)
  2. Implement getOptionLabel as (opt) => opt.property.subProperty (for some "property" field)
  3. Play around with the autocomplete for a while (selecting an option, clearing the option, and clicking to expand the options)... eventually you will get a
TypeError: Cannot read properties of undefined (reading 'subProperty')

CodeSandbox: https://codesandbox.io/s/angry-tree-2jcp3o?file=/src/App.tsx

I'm assuming that's because I'm being passed a string instead of an object of the expected type. However I was trying to debug this in prod with an upset user so I did not have time to check for sure.

This bug ONLY occurs on v5.11.7. When I pinned @mui/material to v5.11.6, the behavior went away, and user reported no further issues. I'm assuming this is the result of your recent changes to freesoloing autocompletes in the release from 3 days ago.

Current behavior 😯

No response

Expected behavior 🤔

No response

Context 🔦

No response

Your environment 🌎

No response

@HansBrende HansBrende added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 4, 2023
@ZeeshanTamboli
Copy link
Member

It doesn't look like this bug report has enough info for one of us to reproduce it.

Please provide a CodeSandbox (https://mui.com/r/issue-template), a link to a repository on GitHub, or provide a proper minimal code example that reproduces the problem.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information component: autocomplete This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 4, 2023
@HansBrende
Copy link
Author

HansBrende commented Feb 4, 2023

@ZeeshanTamboli I literally did provide enough info. That is no joke. But if you want me to spell it out, here's a minimal example (as you will see, no more information than what was already provided). This tells me you did not bother to try testing what I said would break (assuming I am incorrect that it would break with such a simple example?).

export default function App() {
  return (
    <Autocomplete
      options={[{ property: {} }, { property: {} }]}
      getOptionLabel={(opt) => opt.property.subProperty} // <-- this is the only line that matters; literally exactly the same as what I already said.
      renderInput={(params) => <TextField {...params} />}
    />
  );
}

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Feb 4, 2023
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Feb 6, 2023

@HansBrende Only explaining it in full sentences is not enough information for us. You could be explaining something else, while the reproduction code could yield different results. Anyway, here's a CodeSandbox reproduction: https://codesandbox.io/s/angry-tree-2jcp3o?file=/src/App.tsx. Thanks for your code example.

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work regression A bug, but worse labels Feb 6, 2023
@ZeeshanTamboli ZeeshanTamboli changed the title Non-freesolo Autocomplete acts like freesolo: passes string to getOptionLabel instead of option [Autocomplete] Non-freesolo autocomplete acts like freesolo: passes string to getOptionLabel instead of option Feb 6, 2023
@HansBrende
Copy link
Author

@ZeeshanTamboli I provided, not only "full sentences", but the exact code for getOptionLabel in my original issue comment, which is the only thing required to reproduce. All other values can be set to their defaults/whatever.

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Feb 7, 2023

@HansBrende What is your use case for having multiple (in your case, two-level) nested options? Do you somehow use it in the getOptionLabel callback? If not, that means it's passing unnecessary nested options and you can map it to one level. Your code in your application most likely has room for improvement since the last release. See the CodeSandbox: https://codesandbox.io/s/crimson-shape-9rgvow?file=/src/App.tsx

@HansBrende
Copy link
Author

HansBrende commented Feb 7, 2023

@ZeeshanTamboli

const options = [{isEmpty: true}, {isEmpty: false, data: {}}];
const getOptionLabel = opt => opt.isEmpty ? 'NO DATA' : opt.data.subProperty;

No, my code does not have room for improvement, and I'm certainly not going to rewrite my code to accommodate your regression; I'm going to pin mui/material to the previous patch version until this is fixed.

@rangoo94
Copy link
Contributor

rangoo94 commented Feb 7, 2023

Looking at the code, the useAutoComplete hook is falling back to '', when there is no value selected (Autocomplete.js:L474).

This shouldn't be the case. The getOptionLabel is defined as (option: T) => string (ignoring free-solo types), where T is any and is provided by an MUI's user.

Because of that, the getOptionLabel shouldn't be called when there is no value - otherwise, the definition would need to be i.e. (option: T | string) => string, which could make the integration much harder.

@rangoo94
Copy link
Contributor

rangoo94 commented Feb 7, 2023

I created #36088 PR - it will maintain the getOptionLabel contract and not call it with lacking values, while it will keep the same behavior internally (falling back to '' for comparison).

Thanks to that, it will neither break the contract nor the internals.

rangoo94 added a commit to rangoo94/material-ui that referenced this issue Feb 13, 2023
CarsonF added a commit to SeedCompany/cord-field that referenced this issue Feb 17, 2023
Includes needed fix for mui/material-ui#36045
CarsonF added a commit to SeedCompany/cord-field that referenced this issue Feb 17, 2023
Includes needed fix for mui/material-ui#36045
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! regression A bug, but worse
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants