# All files TableFooter.tsx

0% Statements 0/139
0% Branches 0/1
0% Functions 0/1
0% Lines 0/139

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

     "use client";
    import {
      forwardRef,
      useCallback,
      useMemo,
      useState,
      type HTMLAttributes,
    } from "react";
    import { useEnsuredRef } from "../useEnsuredRef.js";
    import { useIntersectionObserver } from "../useIntersectionObserver.js";
    import {
      TableConfigProvider,
      useTableConfig,
      type TableCellConfig,
      type TableConfigContext,
    } from "./TableConfigurationProvider.js";
    import { useTableContainer } from "./TableContainerProvider.js";
    import { tableFooter } from "./tableFooterStyles.js";
    import { type TableStickySectionProps } from "./types.js";

    /**
     * @remarks \@since 6.0.0
     */
    export const isTableFooterStickyActive = (
      entry: IntersectionObserverEntry,
      isInTableContainer: boolean
    ): boolean => {
      const { intersectionRatio, boundingClientRect, isIntersecting } = entry;
      if (isInTableContainer) {
        return !isIntersecting;
      }

      return intersectionRatio < 1 && boundingClientRect.top >= 0;
    };
    /**
     * @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 = isTableFooterStickyActive,
        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]) => {
            setStickyActive(isStickyActive(entry, exists));
          },
          [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={tableFooter({ className, sticky, stickyActive })}
          >
            {children}
          </tfoot>
        </TableConfigProvider>
      );
    });