@actualwave/walker-children-adapter 中文文档教程
Object Adapter for TreeWalker
为子节点使用 children
属性的对象树适配器。 源结构的每个节点都必须具有 name:String
和 children: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.