# All files / tree TreeProvider.tsx

100% Statements 198/198
100% Branches 5/5
100% Functions 2/2
100% Lines 198/198

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
    1991x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2380x
    2380x
    2380x
    2380x
    8x
    8x
    2372x
    2372x
    2372x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
     "use client";
    import {
      createContext,
      useContext,
      useMemo,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { type CustomLinkComponent } from "../link/Link.jsx";
    import { type NonNullRef } from "../types.js";
    import {
      type DefaultTreeItemNode,
      type TreeData,
      type TreeItemNode,
    } from "./types.js";
    import { type TreeExpansion } from "./useTreeExpansion.js";
    import { type TreeSelection } from "./useTreeSelection.js";

    /**
     * When this is set to `"auto"`, clicking on a tree item with a mouse or with
     * the Enter/Space keys will select and expand/collapse the tree item. The user
     * can still use the ArrowLeft and ArrowRight keys to expand/collapse nested
     * tree items.
     *
     * When this is set to `"manual"`, clicking on a tree item with a mouse or with
     * the Enter/Space keys will only select that tree item. The user must use the
     * ArrowLeft and ArrowRight keys to expand/collapse nested tree items.
     *
     * The main use-case for the `"manual"` setting is creating a navigation tree
     * that requires the user to click on an icon or a button to expand the child
     * items.
     *
     * @remarks \@since 6.0.0
     */
    export type TreeExpansionMode = "auto" | "manual";

    /**
     * @internal
     * @remarks \@since 6.0.0
     */
    export interface TreeItemMetadataLookup {
      /**
       * This is used for the keyboard movement behavior for trees that allow the
       * `ArrowRight` key for tree items that have children.
       */
      expandable: Record<string, boolean>;

      /**
       * This is a quick lookup if a tree item is disabled since it's possible the
       * custom `renderer` sets the `disabled` state and not the `TreeData`.
       */
      disabledItems: Record<string, boolean>;

      /**
       * A lookup of element `id` (DOM id) for each tree item to the `itemId`. This
       * is a quick way to figure out which item is being interacted with from a
       * keyboard event without needed to add `data-*` attributes.
       */
      elementToItem: Record<string, string>;

      /**
       * A lookup of tree item `itemId` to the element `id` (DOM id). This is used
       * to be able to find a parent tree item when the `ArrowLeft` key is pressed
       * to focus that parent item.
       */
      itemToElement: Record<string, string>;
    }

    /**
     * The tree context is mostly a convenience API for implementing a custom tree
     * item that requires the {@link TreeExpansion}, {@link TreeSelection} and
     * {@link TreeData} to work.
     *
     * The other properties on the context are most likely for internal use only.
     *
     * @remarks \@since 6.0.0
     */
    export interface TreeContext<T extends TreeItemNode = DefaultTreeItemNode>
      extends TreeExpansion,
        TreeSelection {
      data: TreeData<T>;
      rootId: string | null;
      disableTransition: boolean;
      temporaryChildItems: boolean;

      linkComponent: CustomLinkComponent;

      /** @internal */
      expanderLeft: boolean;
      /** @internal */
      expanderIcon: ReactNode;
      /** @internal */
      expansionMode: TreeExpansionMode;
      /** @internal */
      metadataLookup: NonNullRef<TreeItemMetadataLookup>;
    }

    // Allow the hook to correct typecast this instead
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const context = createContext<TreeContext<any> | undefined>(undefined);
    context.displayName = "Tree";
    const { Provider } = context;

    /**
     * This can be used for a custom tree item renderer.
     *
     * @see The `Tree` component for an example.
     * @remarks \@since 6.0.0
     */
    export function useTreeContext<
      T extends TreeItemNode = DefaultTreeItemNode,
    >(): TreeContext<T> {
      const value = useContext(context);
      if (!value) {
        throw new Error("Cannot find a parent Tree component");
      }

      return value;
    }

    /**
     * @internal
     * @remarks \@since 6.0.0
     */
    export interface TreeProviderProps<T extends TreeItemNode = DefaultTreeItemNode>
      extends TreeContext<T> {
      children: ReactNode;
    }

    /**
     * **Client Component**
     *
     * @internal
     * @remarks \@since 6.0.0
     */
    export function TreeProvider<T extends TreeItemNode = DefaultTreeItemNode>(
      props: TreeProviderProps<T>
    ): ReactElement {
      const {
        children,
        data,
        rootId,
        multiSelect,
        expanderIcon,
        expanderLeft,
        expansionMode,
        metadataLookup,
        disableTransition,
        temporaryChildItems,
        linkComponent,
        expandedIds,
        selectedIds,
        expandMultipleTreeItems,
        toggleTreeItemExpansion,
        selectMultipleTreeItems,
        toggleTreeItemSelection,
      } = props;
      const value = useMemo<TreeContext<T>>(
        () => ({
          data,
          rootId,
          multiSelect,
          expanderIcon,
          expanderLeft,
          expansionMode,
          metadataLookup,
          disableTransition,
          temporaryChildItems,
          linkComponent,
          expandedIds,
          selectedIds,
          expandMultipleTreeItems,
          toggleTreeItemExpansion,
          selectMultipleTreeItems,
          toggleTreeItemSelection,
        }),
        [
          data,
          disableTransition,
          expandMultipleTreeItems,
          expandedIds,
          expanderIcon,
          expanderLeft,
          expansionMode,
          linkComponent,
          metadataLookup,
          multiSelect,
          rootId,
          selectMultipleTreeItems,
          selectedIds,
          temporaryChildItems,
          toggleTreeItemExpansion,
          toggleTreeItemSelection,
        ]
      );

      return <Provider value={value}>{children}</Provider>;
    }