# All files / table TableCheckbox.tsx

100% Statements 208/208
100% Branches 5/5
100% Functions 3/3
100% Lines 208/208

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
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    2091x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    1x
    1x
    1x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
     import { forwardRef, type TdHTMLAttributes } from "react";
    import { Checkbox } from "../form/Checkbox.js";
    import { type CheckboxProps } from "../form/InputToggle.js";
    import { type PropsWithRef } from "../types.js";
    import { TableCell } from "./TableCell.js";

    const noop = (): void => {
      // do nothing
    };

    /**
     * @remarks \@since 6.0.0
     */
    export type TableCheckboxTdHTMLAttributes = Omit<
      TdHTMLAttributes<HTMLTableCellElement>,
      "aria-sort" | "scope" | "onChange"
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export type TableCheckboxSupportedCheckboxProps = Pick<
      CheckboxProps,
      | "name"
      | "value"
      | "icon"
      | "iconStyle"
      | "iconClassName"
      | "iconProps"
      | "checkedIcon"
      | "indeterminateIcon"
      | "checked"
      | "onChange"
      | "defaultChecked"
      | "indeterminate"
      | "aria-controls"
    >;

    /**
     * @remarks
     * \@since 6.0.0 The `id` prop is no longer required since the checkbox's id
     * will automatically be generated as `"checkbox" + useId()`. Use the {@link checkboxProps}
     * to set an `id` manually for the checkbox.
     * \@since 6.0.0 The `cellId` prop was removed. Use the `id` prop instead.
     * \@since 6,0.0 The default `aria-label` was changed from
     * `"Toggle Row Selection"` to `"Select Row"`.
     */
    export interface TableCheckboxProps
      extends TableCheckboxTdHTMLAttributes,
        TableCheckboxSupportedCheckboxProps {
      /**
       * @defaultValue `!props["aria-labelledby"] ? "Select Row" : undefined`
       */
      "aria-label"?: string;

      /** @defaultValue `false` */
      sticky?: boolean;

      /**
       * This allows you to override any props for the checkbox that are not
       * configurable as top-level props.
       *
       * @example
       * Simple Example
       * ```tsx
       * checkboxProps={{
       *   id: "some-custom-id",
       *   ref: checkboxRef,
       * }}
       * ```
       *
       * @see {@link TableCheckboxSupportedCheckboxProps}
       * @remarks \@since 6.0.0
       */
      checkboxProps?: PropsWithRef<CheckboxProps, HTMLInputElement>;
    }

    /**
     * **Server Component**
     * This might actually be a client component.
     *
     * @example
     * Simple Example
     * ```tsx
     * import {
     *   Table,
     *   TableBody,
     *   TableCell,
     *   TableCheckbox,
     *   TableContainer,
     *   TableHeader,
     *   TableRow,
     *   useCheckboxGroup
     * } from "@react-md/core";
     * import { type ReactElement } from "react";
     *
     * const rows = [
     *   { name: "Frozen Yogurt", type: "Ice Cream" },
     *   { name: "Ice cream sandwich", type: "Ice Cream" },
     *   { name: "Eclair", type: "Pastry" },
     *   // ...other content
     * ] as const;
     *
     * function Example(): ReactElement {
     *   const { getCheckboxProps, getIndeterminateProps } = useCheckboxGroup({
     *     values: rows.map(({ name }) => name),
     *     name: "selected",
     *   });
     *   return (
     *     <TableContainer>
     *       <Table>
     *         <TableHeader>
     *           <TableRow>
     *             <TableCheckbox {...getIndeterminateProps()} />
     *             <TableCell>Name</TableCell>
     *             <TableCell>Type</TableCell>
     *           </TableRow>
     *         </TableHeader>
     *         <TableBody>
     *           {rows.map(({ name, type }) => {
     *             const checkboxProps = getCheckboxProps(name);
     *             const { checked, onChange } = checkboxProps;
     *
     *             return (
     *               <TableRow
     *                 key={name}
     *                 onClick={onChange}
     *                 clickable
     *                 selected={checked}
     *               >
     *                 <TableCheckbox {...checkboxProps} />
     *                 <TableCell>{name}</TableCell>
     *                 <TableCell hAlign="right">{type}</TableCell>
     *               </TableRow>
     *             );
     *           })}
     *         </TableBody>
     *       </Table>
     *     </TableContainer>
     *   );
     * }
     * ```
     *
     * @remarks
     * \@since 6.0.0 The `id` prop is no longer required since the checkbox's id
     * will automatically be generated as `"checkbox" + useId()`. Use the {@link checkboxProps}
     * to set an `id` manually for the checkbox.
     * \@since 6.0.0 The `cellId` prop was removed. Use the `id` prop instead.
     * \@since 6,0.0 The default `aria-label` was changed from
     * `"Toggle Row Selection"` to `"Select Row"`.
     */
    export const TableCheckbox = forwardRef<
      HTMLTableCellElement,
      TableCheckboxProps
    >(function TableCheckbox(props, ref) {
      const {
        "aria-labelledby": ariaLabelledBy,
        "aria-label": ariaLabel = !ariaLabelledBy ? "Select Row" : undefined,
        "aria-controls": ariaControls,
        name,
        icon,
        iconProps,
        iconStyle,
        iconClassName,
        checkedIcon,
        indeterminateIcon,
        value,
        checked,
        onChange,
        defaultChecked,
        indeterminate,
        checkboxProps,
        onClick = noop,
        ...remaining
      } = props;

      return (
        <TableCell
          {...remaining}
          ref={ref}
          header={false}
          inputToggle
          onClick={(event) => {
            event.stopPropagation();
            onClick(event);
          }}
        >
          <Checkbox
            aria-label={ariaLabel}
            aria-labelledby={ariaLabelledBy}
            aria-controls={ariaControls}
            name={name}
            icon={icon}
            iconProps={iconProps}
            iconStyle={iconStyle}
            iconClassName={iconClassName}
            checkedIcon={checkedIcon}
            indeterminateIcon={indeterminateIcon}
            value={value}
            checked={checked}
            defaultChecked={defaultChecked}
            onChange={onChange}
            indeterminate={indeterminate}
            {...checkboxProps}
          />
        </TableCell>
      );
    });