aturan23
aturan23

Reputation: 5400

react-table prevent row onClick for Checkbox row

I use react-table latest version. My table:

const MaterialTable = ({
 onClick,
 ...
}) => {
...
 const { getTableProps, headerGroups, rows, prepareRow, selectedFlatRows } = useTable(
  {
   columns: head,
   data,
   defaultColumn,
  },
  useRowSelect,
  useBlockLayout,
  useResizeColumns,
  (hooks) => {
   if (isSelectable) {
    hooks.visibleColumns.push((columns) => [
     {
      id: 'selection',
      width: 5,
      maxWidth: 5,
      // The header can use the table's getToggleAllRowsSelectedProps method
      // to render a checkbox
      Header: ({ getToggleAllRowsSelectedProps }) => (
       <IndeterminateCheckbox {...getToggleAllRowsSelectedProps()} />
      ),
      // The cell can use the individual row's getToggleRowSelectedProps method
      // to the render a checkbox
      Cell: ({ row }) => <IndeterminateCheckbox {...row.getToggleRowSelectedProps()} />,
     },
     ...columns,
    ]);
   }
  }
 );
 const renderTableBody = (row) => {
  prepareRow(row);
  return (
   <TableRow
    role="checkbox"
    onClick={() => onClick(row.original)}
   >
    {row.cells.map((cell) => (
     <TableCell
      {...cell.getCellProps()}
     >
      {cell.render('Cell')}
     </TableCell>
    ))}
   </TableRow>
  );
 };
...
};

And my IndeterminateCheckbox component:

const IndeterminateCheckbox = React.forwardRef(({ indeterminate, ...rest }, ref) => {
 const defaultRef = React.useRef();
 const resolvedRef = ref || defaultRef;
 React.useEffect(() => {
  resolvedRef.current.indeterminate = indeterminate;
 }, [resolvedRef, indeterminate]);
 return <Checkbox ref={resolvedRef} {...rest} />;
});

And question is: How to prevent onClick function when I click to checkbox?

Upvotes: 0

Views: 1667

Answers (1)

Valentin Ivanchikov
Valentin Ivanchikov

Reputation: 93

Look in the documentation about event.preventDefault()

Upvotes: 1

Related Questions