1

I have 2 inputs on the form. I am using React.js and Twitter Bootstrap.

The intended behavior is:

When an input is typed in on either of the form input fields, another input field should not accept any input (or be disabled).

I am using FormControl from Bootstrap.

How should I achieve this in this React.js/Bootstrap setting?

4
  • I have 2 FormGroup <FormGroup controlId={this.props.id}> <ControlLabel>{this.props.label}</ControlLabel> <FormControl {...this.props} /> </FormGroup> Commented Jul 4, 2017 at 22:19
  • What do you mean by disabled? Commented Jul 4, 2017 at 22:21
  • use state isDisabled in first input then onKeyDown second you true the state Commented Jul 4, 2017 at 22:25
  • I have 2 inputs and button and I can write information only in one input, the second becomes disabled. Commented Jul 5, 2017 at 7:02

1 Answer 1

3

You have to use disabled property. You can do something like this:

class MyForm extends React.Component{
  constructor(){
    super();
    this.state = {
      focused: undefined,
    }
  }
  onFocus(id){
    this.setState({
      focused: id
    });
  }
  onBlur(){
    this.setState({
      focused: undefined,
    });
  }
  render(){
    const { focused } = this.state;
    return (
        <div>
          <FormGroup>
            <FormControl type="text" onFocus={() => this.onFocus(1)} onBlur={this.onBlur} disabled={focused && focused !== 1}/>
            <FormControl type="text" onFocus={() => this.onSecondFocus(2)} onBlur={this.onBlur} disabled={focused && focused !== 2}/>
          </FormGroup>
        </div>
    );
  }
}

export default MyForm;

https://codesandbox.io/s/yPBvrOvKg

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you/ And if I want to desabled input if the first input has some information? I have 2 inputs and button and I can write information only in one input
Just change onFocus to onChange and check there length of input value, if it's > 0 you disable another input, else enable. I have edited sandbox
And if i have 2 components. in first i have method handleInputChange = inputId => event => { this.setState({ [inputId]: event.target.value }); }; and in second component i have in props this.state.input1 and this.state.input2. how in this case?
Could you ask by another way? I don't understand the last question, if it's question
i have this situation codesandbox.io/s/Q2gp7Ky5 2 components(they are in index.js and Hello.js) and one method handleInputChange. How in this way?
|

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.