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.

onChange is triggered when typing in AsyncTypeahead single input

See original GitHub issue

Hopefully this bug report makes sense! It might also just be me misunderstanding the API, but I managed to set up the multiple input version of the component just fine and so I’m not sure what’s going wrong when I try to use the single input version…

Version

5.0.0-alpha.1

Steps to reproduce

Use an AsyncTypeahead single input component:

<AsyncTypeahead
  multiple={false}
  onChange={selected => {
    console.log('// onChange')
  }}
  onInputChange={text => {
    console.log('// onInputChange')
  }}
  options={autocompleteOptions}
  onSearch={queryString => {
    console.log('// onSearch')
  }}
  selected={selectedItem}
/>

Expected Behavior

When I type in the input, I would expect onInputChange and onSearch to trigger, but not onChange (which I would expect to only trigger once a selection is actually made).

Actual Behavior

onChange is triggered immediately when typing, which updates selectedItem and prevents the controlled component from actually changing.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
SachaGcommented, Apr 3, 2020

Right, I was just hardcoding it as a quick example. But your reply helped me figure out the issue! In my actual code the flow looks like this:

  1. the component receives selected from its parent
  2. the user types in the component, which should trigger a query to the server
  3. when the user picks an option, this then calls a function to update that original selected in the parent’s state

Basically I was missing a step to update the parent component’s state to clear the value. I’ve added this to the onChange and it seems to work:

        onChange={selected => {
          if (selected.length === 0) {
            clearValue();
          } else {
            updateValue(selected[0].value);
          }
        }}

Thanks for helping me work this out! Great component btw, I’m planning to include it as one of the default Vulcan.js components 😃

0reactions
ericgiocommented, Apr 3, 2020

Awesome! Glad you were able to solve the issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

onChange handler in async typeahead is not called when ...
They are triggered when I type something on UI and select one option from typeahead dropdown. But from tests, both onChange and selected...
Read more >
React Bootstrap Typeahead Example
The typeahead allows single-selection by default. Setting the multiple prop turns the component into a tokenizer, allowing multiple selections. Example.
Read more >
How to Use the Enter Key Event Handler on a React-Bootstrap ...
These events are triggered when users touch, release, or hold keys, and they are used to to process specific user input.
Read more >
How To Create Searchable, Async Dropdowns with React ...
If you interact with this menu, you will trigger an onChange event and log messages to the console. By opening your browser's developer ......
Read more >
Examples - Final Form Docs
Uses the built-in React inputs: input , select , and textarea to build a form ... it's so easy to write one recursively...
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