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 - Changing Input programatically

See original GitHub issue
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

I am changing the input of the Autocomplete programatically in the following way:

  • I get a reference of the TextField component inside the Autocomplete
  • I create a new Event for changing it’s value with the following code:
  const input = inputRef.querySelector('input') // get the referece of the input
    var valueSetter = Object.getOwnPropertyDescriptor(input,'value').set  
    const prototype = Object.getPrototypeOf(input)
    const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype,'value').set
    if (valueSetter && valueSetter !== prototypeValueSetter) {
        prototypeValueSetter.call(input, lastComment.updatetype.toString());
    } else {
        valueSetter.call(input, lastComment.updatetype);
    }
    input.dispatchEvent(new Event('input',{bubbles:true})) // dispatch the event to trigger the onChange method

  • After that code executes, the TextField will talk to an API to render possible entries for the Autocomplete. And this works fine, I see the TextField changing and the Autocomplete giving me the possible list of items from the API based on the TextField value
  • However, when I select the option from the Autocomplete, the onInputChange method doesn’t fire, and the selected option is never recorded

Expected Behavior 🤔

After selecting the option rendered on the Autocomplete, the onInputChange method has to trigger so the selected option can be saved to state

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v4.9.4
React 16.12.0

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
ottogutierrezcommented, Apr 19, 2020

@erfansamieyan, I get what you’re saying but the issue is that onChange on the Textfield does not get triggered. You can see this example in this Codesandbox. This is how I finally solved my issue

1reaction
oliviertassinaricommented, Apr 14, 2020

The onChange callback will fire if called properly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autocomplete - Changing Input programatically · Issue # ...
I get a reference of the TextField component inside the Autocomplete; I create a new Event for changing it's value with the following...
Read more >
Programmatically set value in material-ui Autocomplete ...
Question: how can I programmaticaly set an input value by clicking on the button without choosing from the dropdown list? For example, I...
Read more >
How to Disable the Browser Autocomplete and Autofill on ...
Learn how to prevent browsers auto filling the input fields of HTML forms. Use autocomplete="off" to specify that autocomplete is disabled. See examples....
Read more >
Technique H98:Using HTML 5.2 autocomplete attributes
The technique works by adding the appropriate autocomplete token to each form field on the form to make the identified inputs Programmatically Determinable....
Read more >
Place Autocomplete | Maps JavaScript API
The Autocomplete widget creates a text input field on your web page, supplies predictions of places in a UI pick ...
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