@actualwave/walker-children-adapter 中文文档教程

发布于 6年前 浏览 12 项目主页 更新于 3年前

Object Adapter for TreeWalker

为子节点使用 children 属性的对象树适配器。 源结构的每个节点都必须具有 name:Stringchildren:Node[] 属性。

interface Node {
  name: string;
  children: Node[];
}

源示例:

const sourceTree = {
  name: 'root',
  children: [
    { name: 'branch', children: [] },
    {
      name: 'branch',
      children: [
        { name: 'leaf', children: [] },
      ],
    },
    { name: 'branch', children: [] },
  ]
};

How to use

库导出适配器工厂函数 createAdapter() 接受两个可选参数:

  • getNodeName:Function -- Getter function to retrieve node name, default getter returns node.name property.
  • getNodeChildren:Function -- Getter function to retrieve list of node children, default getter returns node.children property. You can pass your own getters:
import { create, setDefaultAdapter } from '@actualwave/tree-walker';
import createAdapter from '@actualwave/walker-children-adapter';

const adapter = createAdapter(
  (node) => node.label, // name getter
  (node) => node.list, // children getter
);

setDefaultAdapter(adapter);

创建此适配器是为了与 Tree Walker 库, 查看有关如何指定适配器的文档。 此适配器用于 Tree Walker Demo

Object Adapter for TreeWalker

Adapter for Object tree that uses children property for child nodes. Each node of source structure must have name:String and children:Node[] properties.

interface Node {
  name: string;
  children: Node[];
}

Source example:

const sourceTree = {
  name: 'root',
  children: [
    { name: 'branch', children: [] },
    {
      name: 'branch',
      children: [
        { name: 'leaf', children: [] },
      ],
    },
    { name: 'branch', children: [] },
  ]
};

How to use

Library exports adapter factory function createAdapter() that accepts two optional arguments:

  • getNodeName:Function -- Getter function to retrieve node name, default getter returns node.name property.
  • getNodeChildren:Function -- Getter function to retrieve list of node children, default getter returns node.children property. You can pass your own getters:
import { create, setDefaultAdapter } from '@actualwave/tree-walker';
import createAdapter from '@actualwave/walker-children-adapter';

const adapter = createAdapter(
  (node) => node.label, // name getter
  (node) => node.list, // children getter
);

setDefaultAdapter(adapter);

This adapter is created to be used with Tree Walker library, check its documentation on how to specify adapter. This adapter was used in Tree Walker Demo.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文