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] undefined text when enter is pressed using "freeSolo" with "getOptionLabel"

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When using the freeSolo along with getOptionLabel on an array of objects, and the user types any value and presses enter, the inputTextBox will have “undefined”.

export default function FreeSolo() {
  return (
    <Autocomplete
      freeSolo
      id="free-solo-2-demo"
      disableClearable
      options={top100Films}
      getOptionLabel={(option) => (option ? option["title"] : "")}
      renderInput={(params) => (
        <TextField
          {...params}
          label="Search input"
          InputProps={{
            ...params.InputProps,
            type: "search"
          }}
        />
      )}
    />
  );
}

const top100Films = [
  { id: 1, title: "The Shawshank Redemption", year: 1994 },
  { id: 2, title: "The Godfather", year: 1972 }
];

code sandbox here

Expected behavior 🤔

Input should not change when pressing enter, at least in freeSolo mode.

Steps to reproduce 🕹

Steps:

  1. type any value in the search input
  2. press enter
  3. search input text will be “undefined” instead of the current in input

Context 🔦

No response

Your environment 🌎

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
zishiwu123commented, Apr 6, 2022

@alikleitcr7 here is a workaround so you can keep using getOptionLabels:

getOptionLabel={(option) => (option["title"] ? option["title"] : option)}

This way if the input option is a string and not an object, option["title"] will evaluate to false and option string will be displayed instead of undefined.

1reaction
michaldudakcommented, Apr 6, 2022

Thanks for the analysis, @zishiwu123. What happens here is the type mismatch. In @alikleitcr7’s example options are objects ({ title, year }). The value typed into the input is a string. After the value is committed by pressing <kbd>Enter</kbd>, getOptionLabel runs on the value. It tries to call option["title"] on a string, which understandably fails.

Converting options to strings is likely the best option to overcome this problem. Alternatively, you could update your getOptionLabel function so it does not return undefined when it’s called with a string.

I’ll update the docs so it’s more clear what to expect when using freeSolo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material-ui <Autocomplete /> getOptionLabel - passing empty ...
Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""]. I have 2 components. The child one is:
Read more >
A Guide on Material UI AutoComplete in React - Refine Dev
We'll discover the Material UI (MUI) AutoComplete component with ... getOptionLabel is used to display the text in the dropdown menu.
Read more >
Demystifying Errors in MUI Autocomplete — part - Medium
This is actually causing an error. MUI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for “Andorra”.
Read more >
Autocomplete API - Material UI - MUI
Name Type Default options * array renderInput * func autoComplete bool false
Read more >
How to Use The Autocomplete Component in Material-UI
Free solo — Where the text input can contain any value but is suggested from a list of possible values. In this article,...
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