reactjs - How to force Enter key to trigger "submit" function without need to focus an input -
i'm making app in react-redux , want simple feature that's starting tricky implement.
:)
i want able open webapp (i land on login screen) , hit enter (the browser has saved user , password , filled in in correct input fields) , have "enter" keystroke tigger login function without me needing click anywhere first focus something.
(on non-root component)
i've got password input (the last one) bound enter key correctly.
so pressing enter when have password field focused works :
handlesubmit() { const { authenticate } = this.props; const { tusername, tpassword } = this.state; const credentials = { username: tusername, password: tpassword, }; authenticate(credentials); } _handlekeypress = (e) => { if (e.key === 'enter') { this.handlesubmit(); } }; render() { return ( <div onkeypress={this._handlekeypress} > <card id="signin"> <cardimg top width="100%" src={rubislogo} alt="rubis" /> <inputgroup> <input placeholder="nom d'utilisateur" value={this.state.tusername} onchange={this.updateuser} /> </inputgroup> <inputgroup> <input placeholder="mot de passe" type="password" value={this.state.tpassword} onchange={this.updatepassword} onkeypress={this._handlekeypress} /> </inputgroup> <div> <button id="btn" onclick={this.handlesubmit} to="/home">login </button> </div> <modalpassword class="modal" buttonlabel="mot de passe oublié ?" /> <modalaccount class="modal" buttonlabel="créer un compte" /> </card> </div>); }
as can see i've tried putting on div didn't work. don't want solution based on that. want enter-key gobal fallback on view. user can't loose funtionality because clicked somewhere.
set focus div default using tabindex javascript event triggered
render() { return ( <div onkeypress={this._handlekeypress} tabindex="1" > <card id="signin"> ..)}
wiki
Comments
Post a Comment