I'm trying to style a table using react table based on the value of the cell, to start with I'm trying to change the background colour of each cell but what I've got based on the react table api documentation and adding getProps() to the column array doesn't seem to work.
To start with, I wanted to just update the style of the cell based on the value, however adding a custom className based on the cell value would also work.
This is the array which creates the columns:
const columns = [
{
Header: 'Things to Do',
accessor: 'item',
getProps: (rowInfo) => {
return {
style: {
backgroundColor: rowInfo && rowInfo.row.item === 'Do a thing' ? 'red' : null,
},
}
}
},
{
Header: '2020-04-01',
accessor: 'date.2020-04-01',
getProps: (rowInfo) => {
return {
style: {
backgroundColor: rowInfo && rowInfo.row['date.2020-04-01'] === 'Y' ? 'red' : null,
},
}
}
},
This is one piece of sample data (you can see that the headers will be nested when accessing them to generate the cells, e.g. date.2020-04-01.
const data = [
{
item: 'Do a thing',
date: {
'2020-04-01': 'Y',
'2020-04-02': 'Y',
'2020-04-03': 'N',
'2020-04-04': 'Y',
}
},
]
To be clear, my table is generated using some pretty standard stuff:
const Table = ({ columns, data }) => {
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
});
return (
<table {...getTableProps()} className="table">
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render('Header')}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row)
return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
})}
</tr>
)
})}
</tbody>
</table>
);
}