[AutoComplete] undefined text when enter is pressed using "freeSolo" with "getOptionLabel"
See original GitHub issueDuplicates
- 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 }
];
Expected behavior 🤔
Input should not change when pressing enter, at least in freeSolo mode.
Steps to reproduce 🕹
Steps:
- type any value in the search input
- press enter
- search input text will be “undefined” instead of the current in input
Context 🔦
No response
Your environment 🌎
No response
Issue Analytics
- State:
- Created a year ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@alikleitcr7 here is a workaround so you can keep using
getOptionLabels
:This way if the input
option
is a string and not an object,option["title"]
will evaluate to false andoption
string will be displayed instead ofundefined
.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 calloption["title"]
on a string, which understandably fails.Converting
options
to strings is likely the best option to overcome this problem. Alternatively, you could update yourgetOptionLabel
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
.