# All files / src/tree useTree.ts

100% Statements 68/68
100% Branches 1/1
100% Functions 1/1
100% Lines 68/68

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
    691x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
    38x
     "use client";
    import type { TreeItemDefaultIds } from "./types.js";
    import type { TreeExpansion } from "./useTreeExpansion.js";
    import { useTreeExpansion } from "./useTreeExpansion.js";
    import type { TreeSelection } from "./useTreeSelection.js";
    import { useTreeSelection } from "./useTreeSelection.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface TreeHookOptions {
      /**
       * @defaultValue `false`
       */
      multiSelect?: boolean;

      /**
       * @defaultValue `[]`
       */
      defaultExpandedIds?: TreeItemDefaultIds;

      /**
       * @defaultValue `[]`
       */
      defaultSelectedIds?: TreeItemDefaultIds;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface TreeImplementation extends TreeSelection, TreeExpansion {}

    /**
     * This is a convenience wrapper for the {@link useTreeExpansion} and
     * {@link useTreeSelection} hooks since they will almost always be used together.
     *
     * @example
     * Simple Example
     * ```tsx
     * import type { TreeData } from "@react-md/core";
     * import { Tree, useTree } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * const data: TreeData = {
     *   // pretend data
     * };
     *
     * function Example(): ReactElement {
     *   const tree = useTree();
     *
     *   return <Tree {...tree} data={data} aria-label="Tree" />;
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useTree(options: TreeHookOptions = {}): TreeImplementation {
      const {
        multiSelect = false,
        defaultExpandedIds,
        defaultSelectedIds,
      } = options;

      return {
        ...useTreeExpansion(defaultExpandedIds),
        ...useTreeSelection(defaultSelectedIds, multiSelect),
      };
    }