# All files / table TableFooter.tsx

98.58% Statements 139/141
94.44% Branches 17/18
100% Functions 1/1
98.58% Lines 139/141

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
    1421x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    2x
    2x
    2x
    2x
    8x
    8x
    8x
    4x

    4x
    4x
    4x
    2x
    2x
    2x
    2x
    2x
    4x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
     "use client";
    import { cnb } from "cnbuilder";
    import type { HTMLAttributes } from "react";
    import { forwardRef, useCallback, useMemo, useState } from "react";
    import { useEnsuredRef } from "../useEnsuredRef.js";
    import { useIntersectionObserver } from "../useIntersectionObserver.js";
    import { bem } from "../utils/bem.js";
    import type {
      TableCellConfig,
      TableConfigContext,
    } from "./TableConfigurationProvider.js";
    import {
      TableConfigProvider,
      useTableConfig,
    } from "./TableConfigurationProvider.js";
    import { useTableContainer } from "./TableContainerProvider.js";
    import type { TableStickySectionProps } from "./types.js";

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

    /**
     * @remarks \@since 6.0.0 Added support for "sticky-active" state.
     */
    export interface TableFooterProps
      extends HTMLAttributes<HTMLTableSectionElement>,
        Pick<TableCellConfig, "lineWrap">,
        TableStickySectionProps {
      /**
       * This is a rename of the `disableHover` of the `TableConfig` since table
       * footers are not hoverable by default. This prop can be enabled to add the
       * row hover color within table footers, but it is not really recommended.
       *
       * @defaultValue `false`
       */
      hoverable?: boolean;
    }

    /**
     * **Client Component**
     * TODO: Create separate useStickyTableFooter + StickyTableFooter component
     *
     * Creates a `<tfoot>` element with some basic styles. This component will
     * disable the hover effect and line wrapping by default, but the hover effect
     * and line-wrapping can be re-enabled if desired through the `hoverable` and
     * `disableNoWrap` props.
     */
    export const TableFooter = forwardRef<
      HTMLTableSectionElement,
      TableFooterProps
    >(function TableFooter(props, propRef) {
      const {
        className,
        hoverable = false,
        lineWrap: propLineWrap,
        children,
        sticky = false,
        stickyOptions,
        isStickyActive,
        disableStickyStyles = false,
        ...remaining
      } = props;

      // update the table configuration with the custom overrides for the `<tfoot>`
      const { dense, hAlign, vAlign, lineWrap, disableHover, disableBorders } =
        useTableConfig({
          lineWrap: propLineWrap,
          disableHover: !hoverable,
        });

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

      const [tfootRef, tfootRefCallback] = useEnsuredRef(propRef);
      const { exists, containerRef } = useTableContainer();
      const [stickyActive, setStickyActive] = useState(false);
      const targetRef = useIntersectionObserver<
        HTMLTableSectionElement | HTMLElement
      >({
        ref: exists ? undefined : tfootRefCallback,
        root: containerRef,
        disabled: !sticky || disableStickyStyles,
        threshold: exists ? 0 : 1,
        getRootMargin: useCallback(() => {
          const topOffset =
            exists && tfootRef.current ? tfootRef.current.offsetHeight - 1 : 1;

          return `0px 0px -${topOffset}px 0px`;
        }, [exists, tfootRef]),
        onUpdate: useCallback(
          ([entry]) => {
            if (typeof isStickyActive === "function") {
              return isStickyActive(entry);
            }

            const { intersectionRatio, boundingClientRect, isIntersecting } = entry;
            if (exists) {
              setStickyActive(!isIntersecting);
              return;
            }

            setStickyActive(intersectionRatio < 1 && boundingClientRect.top >= 0);
          },
          [exists, isStickyActive]
        ),
        // allow the user defined sticky options to override the default behavior
        ...stickyOptions,
      });

      return (
        (<TableConfigProvider value={configuration}>
          {exists && sticky && !disableStickyStyles && (
            // rendering a `<tbody>` since it is valid to have 0-many in a table
            // https://html.spec.whatwg.org/multipage/tables.html#the-table-element
            (<tbody aria-hidden ref={targetRef} />)
          )}
          <tfoot
            {...remaining}
            ref={exists ? tfootRefCallback : targetRef}
            className={cnb(
              styles({
                sticky,
                "sticky-active": stickyActive,
              }),
              className
            )}
          >
            {children}
          </tfoot>
        </TableConfigProvider>)
      );
    });