2

I have a react component which has a input field with disable attribute. On click of the component, the input gets enabled and user can type on the field. I'm able to achieve till there, but I need to focus on input field on getting enabled.

Attributes.js

basicAmenitiesList.map(function(item, index){
     return <Attribute key={index} 
                       name={item.amenity_id} 
                       type={item.title} 
                       icon={item.icon} 
                       selected={item.isSelected} 
                       value={item.value} 
                       onClick={_this.handleClick.bind(this)} 
                       onChange={_this.handleChange.bind(this)}/>
})

The Attribute file :

<div onClick={this.handleClick.bind(this)}>
...
       <input type="text" name={this.props.name}
              ref={this.props.name}
              placeholder="NO INFO FOUND"
              value={this.props.value}
              disabled={!this.props.selected}
              onChange={this.props.onChange}
       />
</div>

handleClick(e) {
    // I need to focus on enabling., that is when **this.props.selected** is true.
    return this.props.onClick(this.props.name);
}

UPDATE

I tried onFocus for input,

onFocus(){
    this.refs.pets.focus();
}

Right now I'm hard coding the refs name as pets, but is there any way to make it dynamic by sending the name with the onFocus?

1 Answer 1

1

you can use autoFocus as property for input

<input type="text" name={this.props.name}
              ref={this.props.name}
              placeholder="NO INFO FOUND"
              value={this.props.value}
              disabled={!this.props.selected}
              onChange={this.props.onChange}
              autoFocus
       />
Sign up to request clarification or add additional context in comments.

1 Comment

I have the same problem and unfortunately, autofocus property is not an option in my case (possible multiple, nested / hidden inputs)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.