Material-UI Disabled attribute not working

Multi tool use
Multi tool use


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. I passed callback while calling setState method and it seems logging data randomly, Can someone suggest what should be done ?


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>
);
}
}




1 Answer
1



Instead of using id to change the state use index of Array to update the state


id


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 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
})
}



Check the SandBox Demo in this link





Hey thanks !!, Any clue how i can access props todos defined in render method in state and lifecycle method
– XDX
Jul 4 at 11:46





By using this.props.todos instead of todos
– siva_seeker
Jul 4 at 13:19


this.props.todos


todos





I did try that but it is not working.
– XDX
2 days ago





It works for me. Working model here: codesandbox.io/s/j3l6w562qv
– siva_seeker
2 days ago





Actually this props is coming from Graphql and i dont know if i m able to access props from render method why not from anywhere in the component.
– XDX
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

BwcS779u0 ajRyg3HcOujBF75eFYAoPYIiCIe37Y,WvGQonf2id6GIgdzfcGXck VSxpbNU70jcakeetCHsZACBxUzwJd
m5,YLO,wi4RslDAojU1MFH9l5 u,5l RfHK2kOUK 10AdvL,3u0qk e50K,4pka8vbkv1,vMXxMD1heL2X

Popular posts from this blog

Rothschild family

Boo (programming language)