Search code examples
react-tablereact-table-v7

Make the entire row to be clicked instead of cell react-table v7


I have a table that uses react-table v7. One of the features I included is making a cell clickable and redirect to a specific link. I want to make the whole row clickable instead of the cell, but should redirect to the same link as it did when clicking on the cell. How can I achieve that?

Below is my implementation of the click in the columns. Thanks.

.......    
const columns = useMemo(

      () => [
        {
          Header: 'Task Name',
          accessor: 'name',
          Cell:e =><Link to={`${e.value}/runs`}>{e.value}</Link>,**//clickable cell**
          disableSortBy: true,
          disableFilters : true,
          Filter: SelectColumnFilter,
          filter: 'equals',
        },
        {
          Header: 'Square',
          accessor: d => `${d.module.name} ${d.function}`,
          disableSortBy: true,
          disableFilters : true,
        },
        {
          Header: 'Schedule',
          accessor: 'schedules',
          disableSortBy: true,
          disableFilters : true,
        },
        {
          Header: 'Runs',
          accessor: 'rundata.count',
          disableSortBy: true,
          disableFilters : true,
          Filter: SelectColumnFilter,
          filter: 'equals',
        },
        {
          Header: 'Last Run At',
          accessor: 'rundata.results[0].end_date',
          disableSortBy: true,
          disableFilters : true,
          Filter: SelectColumnFilter,
          filter: 'equals',
        },
    ]

    return(
    <>
         <TableContainer columns={columns} data={runs} />
    </>
  );

Solution

  • In your TableContainer definition, you will be outputting rows (tr or TableRow just after the prepareRow call)

    Add a onclick to that row.

    eg.

    <tr onClick={((e) => doSomething(e)} />