webscopeio / react-textarea-autocomplete

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

Option to tweak token regexp #56

Closed andypearson closed 6 years ago

andypearson commented 6 years ago

I'd like to be able to use tokens that look like this: :This/That:.

Unfortunately, the default regexp uses \w for the "wordy" part of the token, which means at the moment my autocompletes only receive :This.

I'd like to be able to override the latter part of the token regexp. Is there a sketchy way for me to do this? I've tried overriding this.tokenRegExp in a parent components' componentDidMount but that doesn't seem to work. (For reasons that a more seasoned React developer would have to explain to me).

If there's not a sketchy way for me to get this working, what could I do in a PR for this sort of option to make it upstream? Would it be a per trigger override or some sort of global option?

Thanks for your time, and the effort you've already put into making this an ace component.

jukben commented 6 years ago

Thank you very much for creating this issue and your kind words! You are the best. 🙏

First of all, let's clear this -

I'd like to be able to override the latter part of the token regexp. Is there a sketchy way for me to do this? I've tried overriding this.tokenRegExp in a parent components' componentDidMount but that doesn't seem to work. (For reasons that a more seasoned React developer would have to explain to me).

That should work, you've actually discovered a pretty well-hidden bug. Because of this LOC we have recreated regex every time when some prop change (the check was always false). So from this time, you would be able to override the tokenRegExp without any hassle. That brings me to the second part...

...there is no reason for it. I hope we have wrongly assumed that non-word character should be the end of the token – it should be the first whitespace character. I've changed this behavior so you should be able to use it in your case without any problem. (as soon you don't involve space/tab in the token itself). What do you think about it?

P.S: Deployed as 2.0.2! 🚀

jukben commented 6 years ago

Closed in https://github.com/webscopeio/react-textarea-autocomplete/commit/160771924711df1d0bbb6747ce491fb043f2ed40

jukben commented 6 years ago

If you still think there is a reason for tweaking regexp per trigger, I'm interested in some specifics examples. Currently, I think there is no reason for it. Anyway, feel free to continue in this thread.

andypearson commented 6 years ago

@jukben thanks for much for sorting this out! I tried the update this on my project this morning and it works perfectly. :D

My regexp per trigger suggestion was really a grab at the most customisable possibility for my requirement, but I don't have anything that'd make use of the functionality at the moment so it's not worth thinking about.

Thanks again for your time and effort!

jukben commented 6 years ago

I'm glad that I can help. If you want to make me happy even more, spread the word about this library. I'm targeting 100 ⭐️ lol. 🤣