|
@@ -0,0 +1,135 @@
|
|
|
+## DageTreeActions 树形控件操作栏
|
|
|
+
|
|
|
+### 基本用法
|
|
|
+
|
|
|
+```tsx
|
|
|
+import React, { useState, Key } from "react";
|
|
|
+import { message } from "antd";
|
|
|
+import { DataNode } from "antd/es/tree";
|
|
|
+import { DageTreeActions } from "@dage/pc-components";
|
|
|
+
|
|
|
+let key = 0;
|
|
|
+
|
|
|
+export default () => {
|
|
|
+ const [data, setData] = useState([
|
|
|
+ {
|
|
|
+ title: "parent 1",
|
|
|
+ key: "0",
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ title: "leaf 1",
|
|
|
+ key: "0-0",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ title: "leaf 2",
|
|
|
+ key: "0-1",
|
|
|
+ children: [
|
|
|
+ {
|
|
|
+ title: "leaf 2-1",
|
|
|
+ key: "0-1-0",
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ ]);
|
|
|
+
|
|
|
+ const handleAddNode = async (item: DataNode, path: number[]) => {
|
|
|
+ return new Promise((res) => {
|
|
|
+ setTimeout(() => {
|
|
|
+ const temp = [...data];
|
|
|
+ let _item: DataNode = temp;
|
|
|
+
|
|
|
+ while (path.length) {
|
|
|
+ _item = _item[path.shift()];
|
|
|
+ if (path.length) {
|
|
|
+ _item = _item.children;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!_item.children) {
|
|
|
+ _item.children = [
|
|
|
+ {
|
|
|
+ title: "add leaf " + key,
|
|
|
+ key: "add-" + key++,
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ _item.children.push({
|
|
|
+ title: "add leaf " + key,
|
|
|
+ key: "add-" + key++,
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ setData(temp);
|
|
|
+ res(true);
|
|
|
+ }, 1000);
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleEditNode = (item: DataNode, path: number[]) => {
|
|
|
+ message.info("编辑节点路径:" + JSON.stringify(path));
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleDeleteNode = (item: DataNode, path: number[]) => {
|
|
|
+ return new Promise((res) => {
|
|
|
+ setTimeout(() => {
|
|
|
+ const temp = [...data];
|
|
|
+ let _item: DataNode = temp;
|
|
|
+
|
|
|
+ while (path.length) {
|
|
|
+ if (path.length === 1) {
|
|
|
+ _item.splice(path[0], 1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ _item = _item[path.shift()];
|
|
|
+ if (path.length) {
|
|
|
+ _item = _item.children;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ setData(temp);
|
|
|
+ res(true);
|
|
|
+ }, 1000);
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const handleSelect = (selectedKeys: Key[]) => {
|
|
|
+ message.success("选中节点 key:" + JSON.stringify(selectedKeys));
|
|
|
+ };
|
|
|
+
|
|
|
+ return (
|
|
|
+ <DageTreeActions
|
|
|
+ maxLevel={4}
|
|
|
+ treeData={data}
|
|
|
+ onAdd={handleAddNode}
|
|
|
+ onEdit={handleEditNode}
|
|
|
+ onDelete={handleDeleteNode}
|
|
|
+ onSelect={handleSelect}
|
|
|
+ />
|
|
|
+ );
|
|
|
+};
|
|
|
+```
|
|
|
+
|
|
|
+## API
|
|
|
+
|
|
|
+<API hideTitle src='@dage/pc-components/components/DageTreeActions/index.d.ts'></API>
|
|
|
+
|
|
|
+## Methods
|
|
|
+
|
|
|
+### findNodeLevel 获取节点层级
|
|
|
+
|
|
|
+| Name | Description | Type | Default |
|
|
|
+| --------- | ----------- | ------------ | ------------ |
|
|
|
+| nodes | -- | `DataNode[]` | `(required)` |
|
|
|
+| targetKey | 目标 key | `React.Key` | `(required)` |
|
|
|
+| curLevel | 当前层级 | `number` | `--` |
|
|
|
+
|
|
|
+### findKeyPath 获取节点路径
|
|
|
+
|
|
|
+| Name | Description | Type | Default |
|
|
|
+| --------- | ----------- | ------------ | ------------ |
|
|
|
+| nodes | -- | `DataNode[]` | `(required)` |
|
|
|
+| targetKey | 目标 key | `React.Key` | `(required)` |
|
|
|
+| path | 路径 | `number[]` | `[]` |
|