[英]How to disable only one edit button
What i want is, basically, when i click on checkbox, it should disable only that particular todo's edit button, however, when i click on checkbox, it disables the entire edit buttons of the todo's list. 基本上,我想要的是,当我单击复选框时,它应该仅禁用那个特定的待办事项的编辑按钮,但是,当我单击复选框时,它会禁用待办事项列表的整个编辑按钮。 Can someone please suggest me ?
有人可以建议我吗?
class App extends Component {
state = {
disable: false
}
handleCheckBox = () => {
this.setState({disable: !this.state.disable})
}
render() {
const {data: {loading, todos}} = this.props;
if(loading) {
return null;
}
return (
<div style={{display: "flex"}}>
<div style={{margin: "auto", width: 400 }}>
<Paper elevation={1}>
<Form submit={this.createTodo}/>
<List>
{todos.map(todo => (
<ListItem
key={todo.id}
role={undefined}
dense
button
onClick={() => this.updateTodo(todo)}
>
<Checkbox
onClick={this.handleCheckBox}
checked={todo.complete}
tabIndex={-1}
disableRipple
/>
<ListItemText primary={todo.text} />
<ListItemSecondaryAction>
<Button mini color="secondary" variant="fab" disabled={this.state.disable}>
<Icon>edit_icon</Icon>
</Button>
<IconButton onClick={() => this.removeTodo(todo)}>
<CloseIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
</List>
))}
</List>
</Paper>
</div>
</div>
);
}
}
You can create a map/object containing disable state with ids as keys and true/false as values. 您可以创建一个包含禁用状态的地图/对象,其中ID为键,而true / false为值。
state = { buttons = { } };
handleCheckBox = id => {
const state = { buttons: {...this.state.buttons, id: !this.state.buttons[id] }};
this.setState(state);
}
<Button mini color="secondary" variant="fab" disabled={!this.state.buttons[todo.id]}>
You can solve it by changing the state to track each one of the todos button state 您可以通过更改状态以跟踪每个待办事项按钮状态来解决它
state = { disabled: {} }
And then when you click on a checkbox, you pass the todo id
然后,当您单击复选框时,您传递待办事项
id
<Checkbox onClick={() => this.handleCheckBox(todo.id)} />
and change the checkbox handler as below: 并如下更改复选框处理程序:
handleCheckBox = (id) => {
let disabled = this.state.disabled;
disabled[id] = !disabled[id];
this.setState({ disabled });
}
Then you will be able to disable/enable the right button by 然后,您将可以通过以下方式禁用/启用右键
<Button mini color="secondary" variant="fab" disabled={this.state.disabled[tod.id]}>
This should work 这应该工作
handleCheckBox = (todo) => {
todo.disabled = !todo.disabled;
}
<Checkbox
onClick={() => {this.handleCheckBox(todo)} }
checked={todo.complete}
tabIndex={-1}
disableRipple
/>
<Button mini color="secondary" variant="fab" disabled={todo.disabled}>
<Icon>edit_icon</Icon>
</Button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.