javascript - React js autofocus input field -
i created input, , want autofocus it.
when page loads, fake input div displayed , when user ckicks div real input focused. works fine. want autofocus , show keyboard.
i tried set autofocus , "trigger" focus on real input, on desktops works fine when test on mobile devices keyboard not appear.
i tried "trigger" click on fake input (div), , not work.
now when page loads looks this
i don't know wrong.
here code:
import react, {component} 'react'; import proptypes 'prop-types'; import {field} 'redux-form'; import {rules} 'authcomponents/partials/validationrules'; import reactdom 'react-dom'; const renderfakeinputcontent = (that, type, meta) => { // code not relevant issue return content; }; class reduxformpininput extends component { renderfield = ({input, label, type, meta: {pristine, touched, error, warning, valid, dirty}}) => { let fakeinputblurredclass = ' blurred'; let focusrealinput = () => { this.textinput.focus(); // works fine mobile , desktop }; let blurfake = () => { this.fakeinput.classname = this.fakeinput.classname.replace(fakeinputblurredclass, ''); }; let simulatefocusonfake = () => { this.fakeinput.classname += fakeinputblurredclass; }; return ( <fieldset classname={"form-group "+ this.props.classname + ((error && dirty) ? ' pin-input-has-error' : "")}> <div ref={(fake) => { this.fakeinput = fake; }} classname="fake-pin-input no-margin" onclick={focusrealinput.bind(this)} > {renderfakeinputcontent(this, type, {pristine, touched, error, warning, valid, dirty})} </div> {dirty && error && <span classname="pin-input-error">{error}</span>} <div classname="pin-input-real-container"> <input ref={(input) => { this.textinput = input && input.focus(); }} classname="pin-input-real" {...input} onfocus={simulatefocusonfake.bind(this)} onblur={blurfake.bind(this)} maxlength={this.props.maxlength} autocomplete="off" //autofocus={true} /> </div> </fieldset> ); }; componentdidmount(){ let x = this.textinput; // tried many combination of following code x.focus(); x.blur(); x.select(); x.setselectionrange(0, x.value.length); } render() { let validaterules = this.props.validate.map((rule)=>{ if(rules.hasownproperty(rule)){ return rules[rule]; } }); return ( <field name={this.props.name} type={this.props.type} component={this.renderfield} label={this.props.label} validate={validaterules} /> ) } } reduxformpininput.proptypes = { classname: proptypes.string, name: proptypes.string, required: proptypes.bool, validate: proptypes.array, maxlength: proptypes.number, }; reduxformpininput.defaultprops = { classname: '', name: '', validate: [], required: true, maxlength: 9999, }; export default reduxformpininput;
Comments
Post a Comment