fmoo / react-typeahead

Pure react-based typeahead and typeahead-tokenizer
ISC License
677 stars 232 forks source link

Tab acts unexpectedly #255

Open stavlocker opened 5 years ago

stavlocker commented 5 years ago

Hi. There are problems with how tab behaves:

This is terrible UX and makes it really hard to use.

P.S: If this project is abandoned, please just say so in the README.md to save people a lot of time.

stavlocker commented 5 years ago

Here's my temporary solution, a.k.a the hackiest hack I ever hacked together:

class TypeaheadHack extends React.Component {
    componentDidMount = () => {
        ReactDOM.findDOMNode(this).addEventListener("keydown", e => {
            if(((e.key && e.key == "Tab") || e.keyCode == 9) && !e.shiftKey) {
                var current = e.target;
                var all = document.querySelectorAll('input, button, area, object, select, textarea');//no anchors!

                var currentIndex;
                for(var i = 0; i < all.length; i++){
                    if(current.isEqualNode(all[i])){
                        currentIndex = i;
                        break;
                    }
                }

                setTimeout(() => {
                    all[currentIndex + 1].focus();
                }, 50);
            }
        }, true);
    }

    render () {
        return (
            <Typeahead {...this.props}/>
        );
    }
}

What I'm doing here is hackily wrapping the Typeahead in another component which I can access it's ReactDOM.findDOMNode (which is hacky and shouldn't really be used), so I can forcefully register my keyDown event (which typeahead doesn't let me to do) and simulate a tab click (again, hacky).

Please just merge the PR that fixes this!