HextaUI

Tree (UI SDK): Multiple selection

With mode="multiple", Cmd or Ctrl-click toggles an item and Shift extends a range. expandOnPress={false} leaves expanding to the chevron and the arrow keys.

Inbox
Updates
Forums
Promotions
Projects
Website
Mobile app
Archive
Trash

2 selected

"use client"

import * as React from "react"

import { Tree, type TreeItemData } from "@/components/ui/ui-sdk-tree"

const items: TreeItemData[] = [
  {
    value: "inbox",
    label: "Inbox",
    children: [
      { value: "updates", label: "Updates" },
      { value: "forums", label: "Forums" },
      { value: "promotions", label: "Promotions" },
    ],
  },
  {
    value: "projects",
    label: "Projects",
    children: [
      { value: "website", label: "Website" },
      { value: "mobile", label: "Mobile app" },
    ],
  },
  { value: "archive", label: "Archive" },
  { value: "trash", label: "Trash" },
]

function UiSdkTreeMultiple() {
  const [value, setValue] = React.useState(["updates", "website"])

  return (
    <div className="flex w-full max-w-xs flex-col gap-3">
      <Tree
        aria-label="Folders"
        items={items}
        mode="multiple"
        value={value}
        onValueChange={setValue}
        expandOnPress={false}
        defaultExpanded={["inbox", "projects"]}
      />
      <p className="text-sm text-muted-foreground">{value.length} selected</p>
    </div>
  )
}

export { UiSdkTreeMultiple }

This adds the example as components/examples/ui-sdk-tree/multiple.tsx, together with the Tree component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/ui-sdk-tree-multiple.json

To add only the component, follow the Tree installation.

Props in this example

The Tree props this example sets, from the API reference.

PropTypeDefault
itemsThe whole hierarchy. Without children, the tree renders styled rows from it.
TreeItemData[]–
mode
"single" | "multiple" | "none""single"
value
string | string[] | null–
onValueChange
(value, details) => void–
defaultExpanded
string[]–
expandOnPress
booleantrue

Every prop is in the Tree API reference.

See them all, with keyboard support and accessibility notes, on the Tree page.