简体   繁体   English

Material-UI Disabled属性不起作用

[英]Material-UI Disabled attribute not working

I'm trying to disable the edit button once i click on complete but it is not working. 单击完成后,我试图禁用编辑按钮,但它不起作用。 I have passed in the state in disabled attribute but it seems not doing anything, don't know maybe because of setState's asynchronous nature. 我已经在状态中传递了disable属性,但似乎什么也没做,也许不知道是因为setState的异步性质。 I passed callback while calling setState method and it seems logging data randomly, Can someone suggest what should be done ? 我在调用setState方法时通过了回调,并且似乎是随机记录数据,有人可以建议该怎么做吗?

class App extends Component {
          state = {
             buttons: {
               id: "test"
             }
           };

          handleCheckBox = id => {
              let buttons = Object.assign({}, this.state.buttons);
              buttons.id = !this.state.buttons[id]
              this.setState({buttons}, ()=>console.log(this.state.buttons));
            }
          render() {
            return (
                <div>
                  {todos.map(todo => (
                  <List key={todo.id}>
                    <ListItem
                      role={undefined}
                      dense
                      button
                    >
                      <Checkbox
                        onClick={()=>this.handleCheckBox(todo.id)}
                        checked={todo.complete}
                        tabIndex={-1}
                        disableRipple
                      />
                      <ListItemText primary={todo.text} />
                      <ListItemSecondaryAction>
                        <Button mini color="secondary" variant="fab" disabled={this.state.buttons[todo.id]}>
                         <Icon>edit_icon</Icon>
                        </Button>
                        ListItemSecondaryAction>
                    </ListItem>
                   </List>
                  ))}
              </div>
            );
          }
        }

Instead of using id to change the state use index of Array to update the state 而不是使用id来更改状态,请使用Array的index来更新状态

Create an array in Component state which tracks the disabled attribute of each buttons 创建一个处于“组件” state的数组,该数组跟踪每个按钮的disabled属性

 state = { buttons: Array(todos.length).fill(false), }; 

In componentDidMount initialise the array according to todos componentDidMount根据待办事项初始化数组

 componentDidMount(){ const buttons=this.state.buttons.slice(); for(var i=0;i<buttons.length;i++) buttons[i]=todos[i].complete; this.setState({buttons:buttons}) } 

Now use the value in buttons state for disabled attribute of button based on the index of the component being rendered. 现在,根据要呈现的组件的索引,将button状态的值用于button的Disabled属性。

 <Button mini color="secondary" variant="fab" disabled={buttons[todos.indexOf(todo)]}> 

Whenever CheckBox is clicked pass the index to the handleChange function and update the value corresponding to the index value 每当单击CheckBox将索引传递给handleChange函数并更新与索引值相对应的值

 <Checkbox onClick={() =>this.handleCheckBox(todos.indexOf(todo))} checked={buttons[todos.indexOf(todo)]}{...other} /> 

 handleCheckBox = index => { const buttons=this.state.buttons.slice(); buttons[index] = !buttons[index]; this.setState({ buttons:buttons }) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM