简体   繁体   English

如何仅禁用一个编辑按钮

[英]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.

相关问题 如果只有一个格则禁用按钮 - Disable button if only one div 如何在列表视图的特定行中禁用编辑按钮 - How to disable edit button in specific row in Listview 如果没有选中一个框,如何禁用ALLOW按钮? 仅在选中所有复选框后才应启用ALLOW按钮吗? - How to disable ALLOW button if even one box in unchecked? ALLOW button should be enabled only if all the boxes are checked? 如何在一列中显示编辑删除按钮? - How to show edit delete button in one column? Sencha.ExtJs [4.2]:如何在列项目的处理程序中仅禁用一个操作按钮? - Sencha.ExtJs[4.2]: How to disable only one action button within the handler of the column item? 滑块只有一个图像时如何禁用下一个/上一个按钮? - How to disable next/prev button when slider have only one image? 如何 select 只有一个下拉菜单并禁用与单选按钮相关的所有其他下拉菜单 - How to select only one dropdown and disable all other with respect to radio button 如何一键禁用峰会按钮,如果有验证错误应该不禁用 - How to disable summit button after only one click , and it should be not disabled if there are validation errors 如何禁用超过3天的记录的编辑按钮 - How to disable an edit button for the records older than 3 days 如何从Sharepoint功能区隐藏/禁用“编辑/查看”按钮? - How to Hide/disable Edit/View button from sharepoint Ribbon?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM