【发布时间】:2019-10-15 20:11:38
【问题描述】:
我设置了 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"
/>
到目前为止,我可以成功更改道具的状态,但是我不确定如何根据状态使框选中/取消选中?
【问题讨论】:
标签: javascript reactjs checkbox material-ui state