All files / src/material-toolbox/data-table Row.js

100% Statements 13/13
92.86% Branches 13/14
100% Functions 5/5
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                          4x               27x   27x   27x   21x   21x 21x 21x     21x 12x     27x                                                                      
// @flow strict
import React, { useEffect, useRef, useReducer } from 'react';
import cx from 'classnames';
 
import type { Node } from 'react';
 
import DataTableCell from './Cell';
import Checkbox from '../checkbox';
 
import { mtAttrsReducer } from '../reducers';
 
import type { HTMLDataTableRowElement } from '../mt-types';
 
type Props = {
  className?: string,
  header?: boolean,
  withCheckbox?: boolean,
  rowId?: string,
  children?: Node,
};
 
const DataTableRow = ({
  className,
  children,
  header = false,
  withCheckbox = false,
  rowId,
  ...rest
}: Props) => {
  const rootRef = useRef<?HTMLTableRowElement>();
 
  const [mtAttrs, mtAtrrsDistpach] = useReducer(mtAttrsReducer, {});
 
  useEffect(() => {
    // flowlint-next-line unclear-type:off
    const root: HTMLDataTableRowElement = (rootRef.current: any);
 
    Eif (root) {
      root.mtAddClassName = mClassName => root.classList.add(mClassName);
      root.mtRemoveClassName = mClassName => root.classList.remove(mClassName);
    }
 
    root.mtSetAttr = (name, value) =>
      mtAtrrsDistpach({ type: 'set', name, value });
  }, []);
 
  return (
    <tr
      {...rest}
      {...mtAttrs}
      ref={rootRef}
      data-row-id={rowId}
      className={cx(
        {
          'mdc-data-table__header-row': header,
          'mdc-data-table__row': !header,
        },
        className,
      )}
    >
      {withCheckbox && (
        <DataTableCell
          className={cx({
            'mdc-data-table__header-cell--checkbox': withCheckbox && header,
            'mdc-data-table__cell--checkbox': withCheckbox && !header,
          })}
        >
          <Checkbox
            className={cx({
              'mdc-data-table__header-row-checkbox': withCheckbox && header,
              'mdc-data-table__row-checkbox': withCheckbox && !header,
            })}
          />
        </DataTableCell>
      )}
      {children}
    </tr>
  );
};
 
export default DataTableRow;