# All files / table Table.tsx

100% Statements 147/147
100% Branches 3/3
100% Functions 2/2
100% Lines 147/147

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
    1481x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    29x
    29x
    29x
    29x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    19x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import { forwardRef, useMemo, type TableHTMLAttributes } from "react";
    import { bem } from "../utils/bem.js";
    import {
      TableConfigProvider,
      type TableConfigContext,
      type TableConfiguration,
    } from "./TableConfigurationProvider.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-table-background-color"?: string;
        "--rmd-table-border-size"?: string | number;
        "--rmd-table-border-color"?: string;
        "--rmd-table-header-background-color"?: string;
        "--rmd-table-cell-color"?: string;
        "--rmd-table-cell-height"?: string | number;
        "--rmd-table-cell-horizontal-padding"?: string | number;
        "--rmd-table-cell-vertical-padding"?: string | number;
        "--rmd-table-header-cell-height"?: string | number;
        "--rmd-table-hover-color"?: string;
        "--rmd-table-selected-color"?: string;
        "--rmd-table-sticky-cell"?: string | number;
        "--rmd-table-sticky-header"?: string | number;
        "--rmd-table-sticky-footer"?: string | number;
        "--rmd-table-sticky-background-color"?: string;
      }
    }

    const styles = bem("rmd-table");

    /** @remarks \@since 6.0.0 */
    export interface TableClassNameOptions {
      className?: string;

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

      /** @defaultValue `false` */
      fullWidth?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function table(options: TableClassNameOptions = {}): string {
      const { dense = false, fullWidth = false, className } = options;

      return cnb(styles({ dense, "full-width": fullWidth }), className);
    }

    export interface TableProps
      extends TableHTMLAttributes<HTMLTableElement>,
        TableConfiguration {}

    /**
     * **Client Component**
     *
     * @example
     * Responsive Example
     * ```tsx
     * import {
     *   Table,
     *   TableBody,
     *   TableCell,
     *   TableContainer,
     *   TableHeader,
     *   TableRow,
     * } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <TableContainer>
     *       <Table>
     *         <TableHeader>
     *           <TableRow>
     *             <TableCell>Header 1</TableCell>
     *             <TableCell>Header 2</TableCell>
     *             <TableCell>Header 3</TableCell>
     *           </TableRow>
     *         </TableHeader>
     *         <TableBody>
     *           <TableRow>
     *             <TableCell>Row 1 Cell 1</TableCell>
     *             <TableCell>Row 1 Cell 2</TableCell>
     *             <TableCell>Row 1 Cell 3</TableCell>
     *           </TableRow>
     *           <TableRow>
     *             <TableCell>Row 2 Cell 1</TableCell>
     *             <TableCell>Row 2 Cell 2</TableCell>
     *             <TableCell>Row 2 Cell 3</TableCell>
     *           </TableRow>
     *           <TableRow>
     *             <TableCell>Row 3 Cell 1</TableCell>
     *             <TableCell>Row 3 Cell 2</TableCell>
     *             <TableCell>Row 3 Cell 3</TableCell>
     *           </TableRow>
     *         </TableBody>
     *       </Table>
     *     </TableContainer>
     *   );
     * }
     * ```
     */
    export const Table = forwardRef<HTMLTableElement, TableProps>(
      function Table(props, ref) {
        const {
          className,
          children,
          dense = false,
          hAlign = "left",
          vAlign = "middle",
          lineWrap = false,
          fullWidth = false,
          disableHover = false,
          disableBorders = false,
          ...remaining
        } = props;

        const configuration = useMemo<TableConfigContext>(
          () => ({
            dense,
            header: false,
            hAlign,
            vAlign,
            lineWrap,
            disableHover,
            disableBorders,
          }),
          [dense, hAlign, vAlign, lineWrap, disableHover, disableBorders]
        );

        return (
          <TableConfigProvider value={configuration}>
            <table
              {...remaining}
              ref={ref}
              className={table({ dense, fullWidth, className })}
            >
              {children}
            </table>
          </TableConfigProvider>
        );
      }
    );