我设置了3个复选框(“未开始”,“进行中”,“已完成”),我希望在特定时间仅选中一个。
因此,如果自动从“未启动”开始进行检查,如果我选中“已完成”,我将如何取消选中“未启动”?
现在在这里输入我的代码:
在App.js中:
const newGame = {
id: uuid.v4(),
title: title,
hours: hours,
notStarted: true,
inProgress: false,
completed: false,
}
notStarted = (id) => {
this.setState({games: this.state.games.map(game => {
if(game.id === id){
game.notStarted = !game.notStarted
game.inProgress = false;
game.completed = false;
}
return game;
})})
};
markCompleted = (id) => {
this.setState({games: this.state.games.map(game => {
if(game.id === id){
game.completed = !game.completed
game.notStarted = false;
game.inProgress = false;
}
return game;
})})
};
在render()方法中:
<Backlog games={this.state.games}
markCompleted={this.markCompleted}
inProgress={this.inProgress}
notStarted={this.notStarted}
/>
这是Game.js中的复选框
<FormControlLabel
control={
<Checkbox
color="primary"
onChange={this.props.notStarted.bind(this, id)}
/>
}
label="Not Started"
/>
<FormControlLabel
control={
<Checkbox
color="primary"
onChange={this.props.markCompleted.bind(this, id)}
/>
}
label="Completed"
/>
截至目前,我可以成功更改道具的状态,但是不确定如何根据状态选择复选框。