I have a front-end in react-mui with a list of 8 words and each word has its state updated based on its input and it may also be disabled. Example:
<TextField required id="standard-basic" disabled={this.state.word1Disabled} label="Word1" value={this.state.word1} onChange={(event) => this.setState({word1:event.target.value.trim()})} />
<TextField required id="standard-basic" disabled={this.state.word2Disabled} label="Word2" value={this.state.word2} onChange={(event) => this.setState({word2:event.target.value.trim()})} />
The user can decide which of the 8 words to disable using a dropdown menu which implements the following function:
const handleWord = event => {
switch(event.target.value) {
case "1":
this.setState({word1Disabled:true,word2Disabled:false,word3Disabled:false,word4Disabled:false,word5Disabled:false,word6Disabled:false,word7Disabled:false,
word8Disabled:false})
break;
case "2":
this.setState({word1Disabled:false,word2Disabled:true,word3Disabled:false,word4Disabled:false,word5Disabled:false,word6Disabled:false,word7Disabled:false,
word8Disabled:false})
break;
case "3":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:true,word4Disabled:false,word5Disabled:false,word6Disabled:false,word7Disabled:false,
word8Disabled:false})
break;
case "4":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:false,word4Disabled:true,word5Disabled:false,word6Disabled:false,word7Disabled:false,
word8Disabled:false})
break;
case "5":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:false,word4Disabled:false,word5Disabled:true,word6Disabled:false,word7Disabled:false,
word8Disabled:false})
break;
case "6":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:false,word4Disabled:false,word5Disabled:false,word6Disabled:true,word7Disabled:false,
word8Disabled:false})
break;
case "7":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:false,word4Disabled:false,word5Disabled:false,word6Disabled:false,word7Disabled:true,
word8Disabled:false})
break;
case "8":
this.setState({word1Disabled:false,word2Disabled:false,word3Disabled:false,word4Disabled:false,word5Disabled:false,word6Disabled:false,word7Disabled:false,
word8Disabled:true})
break;
}
}
While the above solution solves my problem, I assume it's not good programming practice because it's over repetitive. Could someone confirm if I am on the right track?
Now I want to give the user the ability to disable two words at the same time using the dropdown menu, say word 1 and 2. How can I modify the handleWord function to achieve such? I tried to think about it but the way I think it is becoming even more repetitive, handling EVERY possible case for each of the two words disabled (8x8 = 64 cases lol).
The desired outcome is that the user will be able to pick two words they want to disable and those textfields receive the "disabled={true}" property. If the user changes their mind and want to change the word, then the old disabled textfield becomes enabled again.
this.setState({word1Disabled: event.target.value === "1", word2Disabled: event.target.value === "2"...etc...etc). Personally, I would not use conditional logic the that to act on the value because like you have noticed, you will have to keep adding a tiny bit more code ->disabledWords.includes(myValue)so then the handler wouldn't need to be modified if more words are added