webscopeio / react-textarea-autocomplete

📝 React component implements configurable GitHub's like textarea autocomplete.
MIT License
451 stars 80 forks source link

Changing value from state does not trigger _changeHandler #109

Closed diogeneshamilton closed 5 years ago

diogeneshamilton commented 5 years ago

I am using the component and when I change the value (by changing my component's state), the _changeHandler does not trigger. It does trigger getDerivedStateFromProps, but not _changeHandler.

This is problematic since I'd like to offer a button to trigger autocomplete (like Twitter, press @ button and with minChar=0 it shows suggestions).

Right now I have a workaround by defining a CustomEvent and then calling _changeHandler(e) on this.rta, however I would imagine this should just work out of the box. Thoughts?

diogeneshamilton commented 5 years ago

Here's the workaround by the way (which basically copies code from the component's internal code):

    let newValue = this.state.enseTitle + "#"
    this.setState({ enseTitle: newValue })
    var e = new CustomEvent('change', { bubbles: true })
    this.textarea.value = newValue;
    this.textarea.dispatchEvent(e);
    this.rta._changeHandler(e);
jukben commented 5 years ago

I see! Nice use case, I will take a look on it. It makes sense to support this. 🙌

jukben commented 5 years ago

Hey @diogeneshamilton this has been deployed as 3.1.5! It would be awesome if you can test it, I did some chore, so I hope everything works as it should. 🙏

You can check the playground, I've added also test for that (https://github.com/webscopeio/react-textarea-autocomplete/pull/110) 💪

diogeneshamilton commented 5 years ago

Amazing thank you! I will test.