Reputation: 2071
Using the material-table library. I would like to replicate the behavior shown in this example.
https://codesandbox.io/s/table-hover-colors-zw9nt
https://www.npmjs.com/package/material-table
https://material-table.com/#/
I was thinking of using onRowClick={}
The logic would be
onRowClick =>
I can use conditional rendering based on a value held in state to change the background. Although this changes the background for all rows.
options={
rowStyle:{backgroundColor: this.state.selected ? '#fff' : this.state.c}
}
My current working example is here https://codesandbox.io/s/peaceful-haibt-2nefw
Thanks for your help
Upvotes: 3
Views: 9978
Reputation: 836
You also need to pass the selectedRowId
otherwise everything will be blue. Also, the rowStyle
options accept a callback, which you could invoke like so:
rowStyle: rowData => ({
backgroundColor: this.state.selected && rowData.tableData.id === this.state.selectedRowId
? this.state.c
: "#fff"
})
Your onRowClick
also needs some work (the select/unselect condition was not correct).
https://codesandbox.io/embed/select-one-row-160vm
Upvotes: 6
Reputation: 5763
The package's documentation provides an example of how you can accomplish this with the options
prop.
I forked your sandbox here.
Upvotes: 2