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.

Using templates and defaultValue results in an empty option on focus

See original GitHub issue

Using the ‘templates’ option in tandem with ‘defaultValue’ results in an empty (or undefined) suggestion when you’ve give focus to the input.

I’ve created a codepen to show the scenario and detailed repro steps are below.

In this example, the suggestion is ‘undefined’:

image

because I’m returning undefined from the suggestion function, but if you return empty string then you still get an ‘empty’ suggestion:

image.

This is somewhat related to #240 (I think), in that my issue is only manifesting itself, because of the behavior from #240.

Repro steps

  1. Set the defaultValue property
  2. Set the templates.inputValue to a function
  3. Set the templates.suggestion to a function
  4. Click on the input to give it focus

Expected result

Nothing happens

Actual result

A sugestion of undefined appears

Suggested fix

I think this is because inside autocomplete.js the initial options state doesn’t use the isQueryAnOption function, which in turn uses templateInputValue. So line 65 could be:

-options: props.defaultValue ? [props.defaultValue] : [],
+options: this.isQueryAnOption(props.defaultValue, [props.defaultValue]) ? [props.defaultValue] : [],

OR we change the options.map call within the render, to only render options where the result of this.templateSuggestion(option) is not falsey.

I think the second option is better, but would appreciate any comments!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Nate-Wesselcommented, Jul 25, 2020

I’m having a similar problem. In my case, it seems that the issue is caused by the defaultValue expecting a string, while my template functions expect an arbitrary object. Passing an object with the expected structure as the defaultValue results in Uncaught (in promise) TypeError: O.toLowerCase is not a function. Passing a string results in undefined errors when I try to access properties that don’t exist.

0reactions
jennikatecommented, Nov 21, 2022

When I do this the autocomplete suggestions list automatically appears on page load (albeit only showing the first option) but I would like it to not appear at all.

We’ve been having the same issue with the defaultValues. We’re now setting the value of the input directly with our own javascript and then updating the classes on the ul element to ensure the hidden class is used.

It’s a very hacky workaround but it does at least work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material UI Autocomplete default value of empty string
When passing the default value of empty string to the Autocomplete component, it throws a console warning that the value empty string is...
Read more >
Default Values and Placeholders
Go to your form Build page. · Select the field where you would like to insert a default value. In the Field options...
Read more >
<select>: The HTML Select element - HTML - MDN Web Docs
Note: According to the HTML specification, the default value for size ... The "focused" select option is highlighted with a dotted outline, ...
Read more >
Using the Python defaultdict Type for Handling Missing Keys
Here, you use .get() to generate a default value for missing_key , but this time, your dictionary stays empty. This is because .get()...
Read more >
Default values | Contentful Help Center
Default values help you and your editors be more efficient in editing content by letting you focus on editing the primary content fields...
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