Skip to content

Using templates and defaultValue results in an empty option on focus #424

Open
@ediblecode

Description

@ediblecode

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working the way it should (including incorrect wording in documentation)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions