0

I have input text-field and Checkbox, when we click on Checkbox the text-field should get disabled. But I couldn't do that, couldn't able to figure out where i'm going wrong.

I have tried in this way:

<FormControlLabel
                                        control={
                                            <Checkbox
                                                // checked={this.state.games=== -1 ? true : false}
                                                onChange={this.handleTextField}
                                                name="games" color="primary"
                                            />}
                                        label="all"
                                    />
                                }

 <TextField required type="number" name='games' variant="outlined"
                                        disabled={this.state.games === -1 ? true : false}
                                        placeholder="value"
                                        onChange={this.handleTextField}
                                    />

OnChange method:

handleTextField = (e) => {

 this.setState({games: e.target.checked === true ? -1 : 0 || e.target.value})
}

Still textfield is not getting disabled. Can anyone help me with this query?

2 Answers 2

1
this.state = {
  games: 0,
}

handleTextField = (e) => {
  this.setState({ textValue: e.target.value })
}

toggleCheckbox = () => this.setState({ games: this.state.games === 0 ? -1 : 0 });

<FormControlLabel
  control={
    <Checkbox
      checked={this.state.games === -1}
      onChange={this.toggleCheckbox}
      name="games"
      color="primary"
    />}
  label="all"
/>

<TextField required type="number" name='games' variant="outlined"
  disabled={this.state.games === -1}
  placeholder="value"
  onChange={this.handleTextField}
/>

I propose different idea, if you don't mind. Use one flag,isCheckboxChecked. Apply it on input checkbox and textfield.

Update:

I update the answer to rely based on games value

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

6 Comments

Hi, When we click on checkbox i have to get variable games value as -1. That is why i have given onChange method for both checkbox and textfield
When click on checkbox (it could be check or uncheck) or when the chechbox is checked the games state should be -1?
When we click on checkbox, it should be checked and value should be -1 and textfield will get disable
The thing is we'll be using one variable for both checkbox and text-field
@Sanjana if it is answer your question, please accept it as accepted answer
|
0

You need to use .setState to update the state value here:

handleTextField = (e) => {
 this.setState({
   games: e.target.checked === true ? -1 : 0 || e.target.value
});}

6 Comments

Yes, i've done that but still textfield is not getting disabled
@Sanjana: Can you try console.log(event.target.) too see target context?
data-indeterminate="false" getting the input tag in console
Is it input type checkbox?
The same way i declared above in my query.
|

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.