# All files / src/table TableCell.tsx

100% Statements 299/299
55.55% Branches 5/9
100% Functions 1/1
100% Lines 299/299

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
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    3001x
    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
    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
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    11x
    11x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    145x
    1x
     "use client";
    import {
      forwardRef,
      type ButtonHTMLAttributes,
      type ReactNode,
      type TdHTMLAttributes,
      type ThHTMLAttributes,
    } from "react";
    import { getIcon } from "../icon/iconConfig.js";
    import { type PropsWithRef } from "../types.js";
    import {
      TableCellContent,
      type SortOrder,
      type TableCellContentsIconRotatorProps,
    } from "./TableCellContent.js";
    import {
      useTableConfig,
      type TableCellConfig,
    } from "./TableConfigurationProvider.js";
    import { tableCell } from "./tableCellStyles.js";

    export type TableCellAttributes = Omit<
      | TdHTMLAttributes<HTMLTableCellElement>
      | ThHTMLAttributes<HTMLTableCellElement>,
      "scope"
    >;

    /**
     * @remarks \@since 6.0.0
     */
    export interface TableCellOptions extends TableCellConfig {
      /**
       * This is a bit of a "weird" prop since all it does is apply `width: 100%` to
       * this cell. This will make this specific cell fill the remaining width of
       * the table (if there is any). If no cells have this prop enabled and the
       * `fullWidth` table configuration is enabled, all cells will have an
       * equal-sized width.
       *
       * @defaultValue `false`
       */
      grow?: boolean;

      /**
       * This prop is only valid when the `header` prop is enabled or the
       * `TableCell` is a child of the `TableHeader` component. This will generally
       * be used with a value of `"row"` if you have table headers that are at the
       * start of each row instead of at the top of the table.
       *
       * @defaultValue `"col"`
       */
      scope?: "row" | "col" | "rowgroup" | "colgroup";

      /**
       *
       * @defaultValue `false`
       * @remarks \@since 6.0.0 This prop is only a boolean.
       */
      sticky?: boolean;

      /**
       * @internal
       * @defaultValue `false`
       */
      inputToggle?: boolean;
    }

    /**
     * @remarks \@since 6.0.0 Removed the `colSpan="100%"` support since `colSpan`
     * really only supports numbers.
     */
    export interface TableCellProps extends TableCellAttributes, TableCellOptions {
      /**
       * If you want to apply a sort icon for a header cell, set this prop to either
       * `"ascending"` or `"descending"`. When you change the sort order, this prop
       * should change as well which will cause the sort icon to rotate. The default
       * behavior is to have the icon facing upwards and not-rotated when
       * `"ascending"`, otherwise it will rotate downwards when `"descending"`.
       *
       * If this prop is set to `"none"`, the cell will render the clickable button
       * in the children, just without the sort icon. This is so that the sort
       * behavior can still be toggled for keyboard users and will be tab-focusable.
       *
       * @see {@link beforeChildren}
       * @see {@link afterChildren}
       */
      "aria-sort"?: SortOrder;

      /**
       * An optional sort icon to use. This will be defaulted to the configured sort
       * icon from the `@react-md/core` package. If you do not want the default
       * implementation for the sort icon behavior from `react-md`, you can set this
       * prop to `null`.
       */
      sortIcon?: ReactNode;

      /**
       * Boolean if the sort icon should appear after the children in the cell
       * instead of before.
       */
      sortIconAfter?: boolean;

      /**
       * Boolean if the sort icon should be rotated instead of the default
       * direction. When this is `undefined`, it will only be `true` when the
       * `"aria-sort"` prop is set to `"descending"`. If this is not `undefined`,
       * its boolean value will always be used.
       */
      sortIconRotated?: boolean;

      /**
       * @defaultValue `"horizontal"`
       */
      padding?: "horizontal" | "vertical" | "none";

      /**
       * This can be used to apply styling or any other props to the
       * `UnstyledButton` that surrounds the `children` when the `"aria-sort"` prop
       * has been provided.
       *
       * @remarks \@since 6.0.0
       */
      contentProps?: PropsWithRef<
        ButtonHTMLAttributes<HTMLButtonElement>,
        HTMLButtonElement
      >;

      /**
       * Any additional props to pass to the `IconRotator` when the `aria-sort` prop
       * has been provided.
       *
       * @remarks \@since 6.0.0
       */
      iconRotatorProps?: TableCellContentsIconRotatorProps;

      /**
       * Since providing an `aria-sort` prop will wrap the `children` in an
       * `UnstyledButton`, you can use this prop to render another button within the
       * table cell before the main `children`.
       *
       * @see {@link afterChildren} for an example.
       * @remarks \@since 6.0.0
       */
      beforeChildren?: ReactNode;

      /**
       * Since providing an `aria-sort` prop will wrap the `children` in an
       * `UnstyledButton`, you can use this prop to render another button within the
       * table cell before the main `children`.
       *
       * @example
       * ```tsx
       * import type { SortOrder } from "@react-md/core";
       * import { Button, Dialog, TableCell } from "@react-md/core";
       * import MoreVertIcon from "@react-md/material-icons/MoreVertIcon";
       * import type { ReactElement } from "react";
       * import { useState } from "react";
       *
       * interface Props {
       *   setSort(sort: string): void;
       *   sortKey: string;
       *   sortOrder: SortOrder;
       * }
       *
       * function Example({ sortKey, sortOrder, setSort }: Props): ReactElement {
       *   const [visible, setVisible] = useState(false);
       *
       *   return (
       *     <>
       *       <TableCell
       *         aria-sort={sortKey === "example" ? sortOrder : "none"}
       *         onClick={() => setSort("example")}
       *         afterChildren={
       *           <Button
       *             aria-label="Options"
       *             buttonType="icon"
       *             onClick={() => {
       *               setVisible(true)
       *             }}
       *           >
       *             <MoreVertIcon />
       *           </Button>
       *         }
       *       >
       *         Example content
       *       </TableCell>
       *       <Dialog
       *         aria-label="Options"
       *         visible={visible}
       *         onRequestClose={() => setVisible(false)}
       *       >
       *         Pretend Content...
       *       </Dialog>
       *     </>
       *   );
       * }
       * ```
       *
       * @remarks \@since 6.0.0
       */
      afterChildren?: ReactNode;
    }

    /**
     * **Client Component**
     *
     * Creates a `<th>` or `<td>` cell with sensible styled defaults. You can create
     * a `<th>` element by enabling the `header` prop OR having a `TableCell` as a
     * child of the `TableHeader` component.
     *
     * Note: If you have a checkbox/radio column in the `TableHeader` without any
     * labels, you will need to manually set the `header={false}` prop for that cell
     * since it is invalid to have a `<th>` without any readable content for screen
     * readers.
     */
    export const TableCell = forwardRef<HTMLTableCellElement, TableCellProps>(
      function TableCell(props, ref) {
        const {
          "aria-sort": sortOrder,
          className,
          grow = false,
          scope: propScope,
          hAlign: propHAlign,
          vAlign: propVAlign,
          header: propHeader,
          lineWrap: propDisableLineWrap,
          inputToggle,
          children,
          beforeChildren,
          afterChildren,
          sticky,
          sortIcon: propSortIcon,
          sortIconAfter = false,
          sortIconRotated,
          iconRotatorProps,
          padding = "horizontal",
          contentProps,
          ...remaining
        } = props;

        const sortIcon = getIcon("sort", propSortIcon);

        // Note: unlike the other usages of `useTableConfig`, the `propHeader`
        // is not provided. This is so that `TableCheckbox` components can still
        // be a sticky header without being rendered as a `<th>`. This also makes
        // it so the scope can be defaulted to `col` or `row` automatically.
        const {
          header: inheritedHeader,
          hAlign,
          vAlign,
          lineWrap,
        } = useTableConfig({
          hAlign: propHAlign,
          vAlign: propVAlign,
          lineWrap: propDisableLineWrap,
        });
        const header = propHeader ?? inheritedHeader;

        let scope = propScope;
        if (!scope && header) {
          scope = !inheritedHeader && propHeader ? "row" : "col";
        }

        const Component = header ? "th" : "td";
        return (
          <Component
            {...remaining}
            ref={ref}
            aria-sort={sortOrder === "none" ? undefined : sortOrder}
            className={tableCell({
              className,
              grow,
              header,
              sticky,
              inputToggle,
              hAlign,
              vAlign,
              lineWrap: !sortOrder && lineWrap,
              padding: sortIcon && sortOrder ? "none" : padding,
              isInTableHeader: inheritedHeader,
            })}
            scope={scope}
          >
            {beforeChildren}
            <TableCellContent
              {...contentProps}
              icon={sortIcon}
              iconAfter={sortIconAfter}
              iconRotatorProps={iconRotatorProps}
              sortOrder={sortOrder}
              hAlign={hAlign}
              rotated={sortIconRotated}
            >
              {children}
            </TableCellContent>
            {afterChildren}
          </Component>
        );
      }
    );