CxJS

TreeNode

import { TreeNode } from 'cx/widgets'; Copied

The TreeNode widget renders the expand/collapse arrow, indentation, and icon for hierarchical data in a TreeGrid. Use it inside a column’s children property.

<div controller={PageController}>
  <Grid
    records={m.data}
    mod="tree"
    style="height: 300px"
    scrollable
    dataAdapter={{ type: TreeAdapter }}
    selection={{ type: KeySelection, bind: m.selection, keyField: "id" }}
    columns={[
      {
        header: "Name",
        field: "name",
        children: (
          <TreeNode
            expanded={m.$record.$expanded}
            leaf={m.$record.$leaf}
            level={m.$record.$level}
            text={m.$record.name}
            icon={expr(m.$record, (r) =>
              r.$leaf
                ? getFileIcon(r.name)
                : r.$expanded
                  ? "folder-open"
                  : "folder",
            )}
          />
        ),
      },
    ]}
  />
</div>
Name
src
components
index.tsx
App.tsx
package.json
README.md

Record Properties

The TreeAdapter manages these special properties on each record:

  • $expanded - Whether the node is expanded
  • $leaf - Whether the node is a leaf (no children)
  • $level - The depth level for indentation
  • $loading - Whether children are being loaded

Icons

TreeNode displays icons based on the node state:

  • Folder icon - For collapsed non-leaf nodes (default: folder)
  • Open folder icon - For expanded non-leaf nodes (default: folder-open)
  • Item icon - For leaf nodes (default: file)
  • Loading icon - While children are loading (default: loading)

Override defaults using folderIcon, openFolderIcon, itemIcon, or loadingIcon properties. Set hideIcon to true to hide icons entirely.

Configuration

PropertyTypeDescription
expandedBooleanPropWhether the node is expanded.
leafBooleanPropWhether the node is a leaf node (cannot have children).
levelNumberPropDepth level inside the tree, used for indentation.
loadingBooleanPropWhether the node is currently loading its children.
textStringPropText to display alongside the icon.
iconStringPropCustom icon. If not set, default folder/file icons are used.
folderIconstringIcon for folders. Default is folder.
openFolderIconstringIcon for open folders. Default is folder-open.
itemIconstringIcon for leaf nodes. Default is file.
loadingIconstringIcon shown while loading. Default is loading.
hideIconbooleanSet to true to hide icons.
hideArrowbooleanSet to true to hide the expand/collapse arrow.