react-mentions icon indicating copy to clipboard operation
react-mentions copied to clipboard

Also trigger onKeyDown when suggestions are open

Open jfschwarz opened this issue 7 years ago • 6 comments

I'm wondering why we only call the onKeyDown prop if no suggestions are showing. It seems more correct to always call it.

jfschwarz avatar Aug 31 '16 07:08 jfschwarz

This would be a breaking change. My use of the control depends on the current behavior of /not/ only receiving the keys that the input/suggestions are not using.

activescott avatar Aug 16 '18 05:08 activescott

However, that would only be breaking, because you're relying on incorrect behavior which you shouldn't. Otherwise basically each bugfix must be considered a breaking change.

But tell me @activescott why do you depend on that?

frontendphil avatar Aug 17 '18 11:08 frontendphil

I'm integrating into an existing application that has some legacy keyboard handling and I needed to be able to know tab was not handled by react-mentions and when it wasn't so that I could route it back to this existing keyboard handling code. Obviously, if I were building an app from scratch, there are better ways to handle this, but it is a large existing code base and it isn't something that I can change.

Also, I started relying on this behavior because of the comment in the code as well as the adjacent code there seemed to quite clearly indicate this behavior was deliberate:

handleKeyDown = ev => {
    // do not intercept key events if the suggestions overlay is not shown
    const suggestionsCount = countSuggestions(this.state.suggestions)

    const suggestionsComp = this.suggestionsRef
    if (suggestionsCount === 0 || !suggestionsComp) {
        this.props.onKeyDown(ev)

        return
    }

If you plan to change this behavior that's fine, just please provide the containing control a similarly easy way to determine when react-mentions handles the key and when it doesn't so that we can route the unhanded ones accordingly and ignore the ones that react-mentions handles.

activescott avatar Aug 19 '18 07:08 activescott

It should call onKeyDown on every key down event

deepaksisodiya avatar Jun 29 '21 06:06 deepaksisodiya

try

const handleKeypress = e => {
        if (e.target.value.length >= 3) {
            setSuggestions(users
                .filter(user => user.firmName.toUpperCase()
                    .includes(e.target.value.toUpperCase())
                ).slice(0, 5))
        }
    };

    const handleKeyDown = e => {
        if (e.target.value.length < 3) {
            setSuggestions([])
        }
        else {
            setSuggestions(users
                .filter(user => user.firmName.toUpperCase()
                    .includes(e.target.value.toUpperCase())
                ).slice(0, 5))
        }
    };

Ransomware0 avatar Jan 22 '22 08:01 Ransomware0

Also interested in this; any idea when this'll be addressed?

russjman avatar Jun 16 '23 15:06 russjman