async-tree-select.tsx


Overview

async-tree-select.tsx defines a React functional component AsyncTreeSelect that provides a hierarchical tree-based selection UI with asynchronous loading capabilities. It is designed for use cases where tree nodes may be dynamically loaded on demand (e.g., lazy loading child nodes from a server). The component displays a dropdown popover containing a collapsible tree structure, allowing users to select a node. Nodes can be expanded or collapsed, and if children are not yet loaded, the component triggers asynchronous loading via a user-supplied callback.

Key features include:


Component and Types

Types

type TreeId = number | string;
export type TreeNodeType = {
  id: TreeId;
  title: ReactNode;     // Node display content
  parentId: TreeId;     // Parent node ID
  isLeaf?: boolean;     // Optional flag indicating leaf node (no children)
};
type AsyncTreeSelectProps = {
  treeData: TreeNodeType[];          // Flat array of all tree nodes
  value?: TreeId;                    // Currently selected node id (controlled)
  onChange?(value: TreeId): void;   // Callback when selection changes
  loadData?(node: TreeNodeType): Promise<any>; // Async loader for node's children
};

AsyncTreeSelect Component

export function AsyncTreeSelect({
  treeData,
  value,
  loadData,
  onChange,
}: AsyncTreeSelectProps) { ... }

Description

Renders an interactive tree-select dropdown with async loading support. Presents selected node title or a placeholder when none selected. Displays nodes in a nested list where parents can be expanded/collapsed by arrow buttons. Loads data asynchronously for nodes lacking children when expanded.

Parameters

Returns


Detailed Implementation

State Variables

Hooks and Callbacks

Effects

Render Output


Usage Example

import { AsyncTreeSelect, TreeNodeType } from './async-tree-select';

const initialData: TreeNodeType[] = [
  { id: '1', title: 'Root', parentId: '', isLeaf: false },
];

async function fetchChildren(node: TreeNodeType) {
  // Fetch children from API or other source
  const children = await api.getChildren(node.id);
  // Update tree data state externally
}

function MyComponent() {
  const [treeData, setTreeData] = useState<TreeNodeType[]>(initialData);
  const [selected, setSelected] = useState<TreeId>();

  return (
    <AsyncTreeSelect
      treeData={treeData}
      value={selected}
      onChange={setSelected}
      loadData={async (node) => {
        const children = await fetchChildren(node);
        setTreeData([...treeData, ...children]);
      }}
    />
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Diagram: Component Structure and Interaction

componentDiagram
    AsyncTreeSelect <|-- Popover
    AsyncTreeSelect <|-- PopoverTrigger
    AsyncTreeSelect <|-- PopoverContent
    AsyncTreeSelect o-- Button : expand/collapse toggles
    AsyncTreeSelect o-- "TreeNodeType[]" : treeData
    AsyncTreeSelect o-- loadData : async loading callback
    AsyncTreeSelect o-- onChange : selection callback

    Popover <|-- PopoverTrigger
    Popover <|-- PopoverContent

    note right of AsyncTreeSelect
      - Maintains state: open, expandedKeys, loadingId
      - Renders nested <ul> with <li> nodes recursively
      - Controls selection & expansion
    end note

Summary

The async-tree-select.tsx file provides a reusable and extensible React component that renders a dropdown tree selector with asynchronous loading capabilities. It supports dynamic data fetching for child nodes, recursive rendering of hierarchical data, and user-friendly selection and expansion interactions. The component relies on a flat tree data structure and expects the parent to handle data loading and state updates. It integrates seamlessly with the project’s UI library and translation framework, making it a valuable component for applications needing hierarchical selection with lazy loading.


End of documentation for async-tree-select.tsx