# All files / src/utils RenderRecursively.tsx

100% Statements 202/202
80% Branches 4/5
100% Functions 1/1
100% Lines 202/202

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
    2031x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    271x
    271x
    271x
    271x
    271x
    271x
    271x
    574x
    574x
    574x
    199x
    199x
    199x
    199x
    199x
    199x
    199x
    199x
    199x
    199x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    574x
    271x
    271x
    271x
     // TODO: Figure out how to strictly enforce the data
    import { type ComponentType, type ReactElement, type ReactNode } from "react";

    /**
     * @remarks \@since 6.0.0
     */
    export type RecursiveItem<T = Record<string, unknown>> = T & {
      items?: readonly RecursiveItem<T>[];
    };

    /**
     * @remarks \@since 6.0.0
     */
    export interface RenderRecursiveItemsProps<
      Item = Record<string, unknown>,
      Data = unknown,
    > {
      data?: Data;

      /**
       * The current item to render.
       */
      item: RecursiveItem<Item>;

      /**
       * The list of parent items which can be used to determine the depth or "share
       * props" if the items contained props.
       */
      parents: readonly RecursiveItem<Item>[];

      /**
       * This will be provided if the {@link item} had child items and will be the
       * rendered content.
       */
      children?: ReactNode;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface RenderRecursivelyProps<
      Item = Record<string, unknown>,
      Data = unknown,
    > {
      data?: Data;
      items: readonly RecursiveItem<Item>[];

      /**
       * The renderer for each item.
       */
      render: ComponentType<RenderRecursiveItemsProps<Item, Data>>;

      /**
       * This should not be used for external users. This is used to build the
       * {@link RenderRecursiveItemsProps.parents} list.
       *
       * @internal
       * @defaultValue `[]`
       */
      parents?: readonly RecursiveItem<Item>[];

      /**
       * Gets a React `key` for a specific item. This should be provided if the
       * items can be moved around to improve performance.
       *
       * @example
       * ```ts
       * getItemKey={(item) => item.id}
       * ```
       *
       * @defaultValue `() => ${parents.length}-${index}`.
       */
      getItemKey?(item: RecursiveItem<Item>): string;
    }

    /**
     * Helper component for recursively rendering specific data structures (mostly
     * trees). The main use-case is for rendering site navigation and the `Tree`
     * component.
     *
     * @example
     * ```tsx
     * import {
     *   buildTree,
     *   List,
     *   ListItem,
     *   ListItemLink,
     *   RenderRecursively,
     *   TreeData,
     *   type DefaultTreeItemNode,
     *   type RenderRecursiveItemsProps,
     * } from "@react-md/core";
     * import { type ReactElement } from "react";
     *
     * const navItems = {
     *   "/": {
     *     href: "/",
     *     itemId: "/",
     *     parentId: null,
     *     children: "Home",
     *   },
     *   "/route-1": {
     *     itemId: "/route-1",
     *     parentId: null,
     *     children: "Collapsible",
     *   },
     *   "/nested-route-1": {
     *     itemId: "/nested-route-1",
     *     parentId: "/route-1",
     *     children: "Child 1",
     *   },
     *   "/nested-route-2": {
     *     itemId: "/nested-route-2",
     *     parentId: "/route-2",
     *     children: "Child 2",
     *   },
     *   "/divider-1": {
     *     itemId: "/divider-1",
     *     parentId: null
     *   },
     *   "/route-2": {
     *     itemId: "/route-2"
     *     parentId: null,
     *     children: "Route 2",
     *   },
     * } satisfies TreeData;
     *
     * function NestedNavigation(props: RenderRecursiveItemsProps<DefaultTreeItemNode>): ReactElement {
     *   const { item, parents, children } = props;
     *   const { toggle, toggled: collapsed } = useToggle(false);
     *   const { elementProps } = useCollapseTransition({
     *     transitionIn: !collapsed,
     *   });
     *
     *   return (
     *     <li>
     *       <Button onClick={toggle}>{item.children}</Button>
     *       <List {...elementProps}>{children}</List>
     *     </li>
     *   ):
     * }
     *
     *
     * function Render(props: RenderRecursiveItemsProps<DefaultTreeItemNode>): ReactElement {
     *   const { item, parents } = props;
     *   if (item.itemId.includes("divider")) {
     *     return <Divider />;
     *   }
     *
     *   if (item.items) {
     *     return <NestedNavigation {...props} />
     *   }
     *
     *   const prefix = parents.map((parent) => parent.itemId).join("/");
     *   return (
     *     <Link href={`${prefix}${item.itemId}`}>
     *       {item.children}
     *     </Link>
     *   );
     * }
     * ```
     *
     *
     * @remarks \@since 6.0.0
     */
    export function RenderRecursively<Item, Data>(
      props: RenderRecursivelyProps<Item, Data>
    ): ReactElement {
      const { data, items, render: Render, getItemKey, parents = [] } = props;

      return (
        <>
          {items.map((item, index) => {
            let children: ReactNode;
            const depth = parents.length;
            if (item.items?.length) {
              children = (
                <RenderRecursively
                  data={data}
                  items={item.items}
                  render={Render}
                  getItemKey={getItemKey}
                  parents={[...parents, item]}
                />
              );
            }

            return (
              <Render
                key={getItemKey ? getItemKey(item) : `${depth}-${index}`}
                // typecast since it should be undefined in renderer as well?
                data={data}
                item={item}
                parents={parents}
              >
                {children}
              </Render>
            );
          })}
        </>
      );
    }