async-tree-select.tsx


Overview

async-tree-select.tsx is a React functional component that provides an asynchronously loaded tree selection UI element using the Ant Design (antd) TreeSelect component. It enables users to select folders from a hierarchical file structure, where the folder data is fetched dynamically on demand (lazy-loading). This component integrates with a file management system by fetching folder data via a custom hook and renders it in a tree-select dropdown with smooth user experience and internationalization support.


Detailed Explanation

Component: AsyncTreeSelect

Purpose

AsyncTreeSelect renders a dropdown tree selector that loads folder nodes asynchronously from a backend or data source. It is designed specifically for selecting folders from a file management hierarchy.

Props

Prop Name

Type

Description

value

[string \

undefined](/projects/311/74002)

onChange

[(value: string) => void \

undefined](/projects/311/71659)

Internal State

State Name

Type

Description

treeData

Omit[]

The flattened array of tree nodes representing folders, formatted for Ant Design's treeDataSimpleMode.

Hooks and Dependencies


Functions and Methods

onLoadData

const onLoadData: TreeSelectProps['loadData'] = useCallback(
  async ({ id }) => {
    const ret = await fetchList(id);
    if (ret.code === 0) {
      setTreeData((tree) => {
        return tree.concat(
          ret.data.files
            .filter((x: IFile) => x.type === 'folder')
            .map((x: IFile) => ({
              id: x.id,
              pId: x.parent_id,
              value: x.id,
              title: x.name,
              isLeaf:
                typeof x.has_child_folder === 'boolean'
                  ? !x.has_child_folder
                  : false,
            })),
        );
      });
    }
  },
  [fetchList],
);

handleChange

const handleChange = (newValue: string) => {
  onChange?.(newValue);
};

React Lifecycle


Rendered Output

Renders an antd TreeSelect with these key props:


Implementation Details and Algorithms


Integration and Interaction


Usage Example

import AsyncTreeSelect from './async-tree-select';

const ParentComponent = () => {
  const [selectedFolder, setSelectedFolder] = useState<string | undefined>();

  return (
    <AsyncTreeSelect
      value={selectedFolder}
      onChange={(val) => setSelectedFolder(val)}
    />
  );
};

Mermaid Component Diagram

componentDiagram
    component AsyncTreeSelect {
      +value?: string
      +onChange?: (value: string) => void
      -treeData: Array<TreeNode>
      -onLoadData({id}): Promise<void>
      -handleChange(newValue: string): void
    }
    component useFetchPureFileList {
      +fetchList(id: string): Promise<FileListResponse>
    }
    component TreeSelect {
      +treeDataSimpleMode: boolean
      +value: string
      +onChange: (value: string) => void
      +loadData: (node) => Promise<void>
      +treeData: Array<TreeNode>
    }
    component useTranslation {
      +t(key: string): string
    }


    AsyncTreeSelect --> useFetchPureFileList : fetchList()
    AsyncTreeSelect --> useTranslation : t()
    AsyncTreeSelect --> TreeSelect : renders

Summary

async-tree-select.tsx encapsulates an asynchronously loaded folder tree selector that efficiently fetches folder data on demand, integrates smoothly with a file management backend, and provides a user-friendly, localized tree selection UI using Ant Design components. Its architecture and lazy loading strategy make it suitable for large and dynamic folder structures.