diff --git a/demo/demolist/Demo7.js b/demo/demolist/Demo7.js new file mode 100644 index 0000000..6491486 --- /dev/null +++ b/demo/demolist/Demo7.js @@ -0,0 +1,121 @@ +/** + * + * @title Tree增加节点 + * @description + * + */ + + +import React, { + Component +} from 'react'; +import Tree from '../../src'; +import Button from 'bee-button'; + +const TreeNode = Tree.TreeNode; + +class Demo7 extends Component { + constructor(props) { + super(props); + debugger; + console.log('keys--' + this.props.keys); + this.state = { + treeData: [], + defaultExpandedKeys: ['0-0', '0-1', '0-2'] + }; + this.onSelect = this.onSelect.bind(this); + this.addNode = this.addNode.bind(this); + this.clickFun = this.clickFun.bind(this); + } + componentDidMount() { + setTimeout(() => { + this.setState({ + treeData: [{ + name: 'pNode 01', + key: '0-0', + children: [{ + name: 'leaf 0-0-0', + key: '0-0-0' + }, { + name: 'leaf 0-0-1', + key: '0-0-1' + }] + }, { + name: 'pNode 02', + key: '0-1', + children: [{ + name: 'leaf 0-1-0', + key: '0-1-0' + }, { + name: 'leaf 0-1-1', + key: '0-1-1' + }] + }, { + name: 'pNode 03', + key: '0-2', + isLeaf: true + }, ], + }); + }, 100); + } + /** + * 增加节点 + * @param string prKey [父节点key] + * @param object nodeItem [子节点信息] + */ + addNode(prKey, nodeItem) { + const data = this.state.treeData; + data.forEach((item) => { + if (prKey === item.key) { + if (!item.children) { + item.children = []; + } + // 如果key不存在就动态生成一个 + if (!nodeItem.key) { + nodeItem.key = prKey + item.children.length + 1; + } + item.children.push(nodeItem); + } + }); + this.setState({ + data + }); + } + onSelect(info) { + console.log('selected', info); + } + /** + * 点击button事件 + */ + clickFun() { + let prKey, nodeItem; + prKey = '0-2'; + nodeItem = { + name: 'leaf 0-2-0' + } + this.addNode(prKey, nodeItem); + } + + render() { + const loop = data => data.map((item) => { + if (item.children) { + return {loop(item.children)}; + } + return ; + }); + const treeNodes = loop(this.state.treeData); + console.log('defaultKeys--' + this.state.defaultExpandedKeys); + return ( +
+ + {treeNodes} + + +
+ ); + } +}; + +export default Demo7 \ No newline at end of file diff --git a/demo/index.js b/demo/index.js index 0f859cc..d6a7b7e 100644 --- a/demo/index.js +++ b/demo/index.js @@ -42,7 +42,7 @@ const CARET = ; const CARETUP = ; -var Demo1 = require("./demolist/Demo1");var Demo6 = require("./demolist/Demo6");var DemoArray = [{"example":,"title":" Tree基本使用事例","code":"/**\r\n*\r\n* @title Tree基本使用事例\r\n* @description 事例涵盖 checkbox如何选择,disable状态和部分选择状态。\r\n*\r\n*/\r\n\r\n\r\nimport React, { Component } from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nconst defaultProps = {\r\n\tkeys: ['0-0-0', '0-0-1']\r\n}\r\nconsole.log(Tree);\r\nclass Demo1 extends Component {\r\n\tconstructor(props) {\r\n\t\tsuper(props);\r\n\t const keys = this.props.keys;\r\n\t this.state = {\r\n\t defaultExpandedKeys: keys,\r\n\t defaultSelectedKeys: keys,\r\n\t defaultCheckedKeys: keys,\r\n\t };\r\n\t}\r\n\tonSelect(info) {\r\n\t console.log('selected', info);\r\n\t}\r\n\tonCheck(info) {\r\n\t console.log('onCheck', info);\r\n\t}\r\n\trender() {\r\n\t return (\r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t sss} key=\"0-0-1-0\" />\r\n\t \r\n\t \r\n\t \r\n\t );\r\n\t}\r\n}\r\n\r\nDemo1.defaultProps = defaultProps;\r\n\r\n\r\n","desc":" 事例涵盖 checkbox如何选择,disable状态和部分选择状态。"},{"example":,"title":" Tree基本使用事例自定义图标","code":"/**\r\n *\r\n * @title Tree基本使用事例自定义图标\r\n * @description 添加openIcon、closeIcon属性\r\n *\r\n */\r\n\r\n\r\nimport React, {\r\n\tComponent\r\n} from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nconst defaultProps = {\r\n\tkeys: ['0-0-0', '0-0-1']\r\n}\r\nconsole.log(Tree);\r\nclass Demo1 extends Component {\r\n\tconstructor(props) {\r\n\t\tsuper(props);\r\n\t\tconst keys = this.props.keys;\r\n\t\tthis.state = {\r\n\t\t\tdefaultExpandedKeys: keys,\r\n\t\t\tdefaultSelectedKeys: keys,\r\n\t\t\tdefaultCheckedKeys: keys,\r\n\t\t};\r\n\t}\r\n\tonSelect(info) {\r\n\t\tconsole.log('selected', info);\r\n\t}\r\n\tonCheck(info) {\r\n\t\tconsole.log('onCheck', info);\r\n\t}\r\n\trender() {\r\n\t\treturn (\r\n\r\n\t\t\t\r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t sss} key=\"0-0-1-0\" />\r\n\t \r\n\t \r\n\t \r\n\t\t);\r\n\t}\r\n}\r\n\r\nDemo1.defaultProps = defaultProps;\r\n\r\n\r\n","desc":" 添加openIcon、closeIcon属性"}] +var Demo1 = require("./demolist/Demo1");var Demo2 = require("./demolist/Demo2");var Demo3 = require("./demolist/Demo3");var Demo4 = require("./demolist/Demo4");var Demo5 = require("./demolist/Demo5");var Demo6 = require("./demolist/Demo6");var Demo7 = require("./demolist/Demo7");var DemoArray = [{"example":,"title":" Tree基本使用事例","code":"/**\r\n*\r\n* @title Tree基本使用事例\r\n* @description 事例涵盖 checkbox如何选择,disable状态和部分选择状态。\r\n*\r\n*/\r\n\r\n\r\nimport React, { Component } from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nconst defaultProps = {\r\n\tkeys: ['0-0-0', '0-0-1']\r\n}\r\nconsole.log(Tree);\r\nclass Demo1 extends Component {\r\n\tconstructor(props) {\r\n\t\tsuper(props);\r\n\t const keys = this.props.keys;\r\n\t this.state = {\r\n\t defaultExpandedKeys: keys,\r\n\t defaultSelectedKeys: keys,\r\n\t defaultCheckedKeys: keys,\r\n\t };\r\n\t}\r\n\tonSelect(info) {\r\n\t console.log('selected', info);\r\n\t}\r\n\tonCheck(info) {\r\n\t console.log('onCheck', info);\r\n\t}\r\n\trender() {\r\n\t return (\r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t sss} key=\"0-0-1-0\" />\r\n\t \r\n\t \r\n\t \r\n\t );\r\n\t}\r\n}\r\n\r\nDemo1.defaultProps = defaultProps;\r\n\r\n\r\n","desc":" 事例涵盖 checkbox如何选择,disable状态和部分选择状态。"},{"example":,"title":" Tree数据可控事例","code":"/**\r\n *\r\n * @title Tree数据可控事例\r\n * @description\r\n *\r\n */\r\n/*\r\nconst x = 3;\r\nconst y = 2;\r\nconst z = 1;\r\nconst gData = [];\r\n\r\nconst generateData = (_level, _preKey, _tns) => {\r\n const preKey = _preKey || '0';\r\n const tns = _tns || gData;\r\n\r\n const children = [];\r\n for (let i = 0; i < x; i++) {\r\n const key = `${preKey}-${i}`;\r\n tns.push({ title: key, key });\r\n if (i < y) {\r\n children.push(key);\r\n }\r\n }\r\n if (_level < 0) {\r\n return tns;\r\n }\r\n const level = _level - 1;\r\n children.forEach((key, index) => {\r\n tns[index].children = [];\r\n return generateData(level, key, tns[index].children);\r\n });\r\n};\r\ngenerateData(z);\r\n*/\r\n\r\n\r\nimport React, {\r\n Component\r\n} from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst x = 3;\r\nconst y = 2;\r\nconst z = 1;\r\nconst gData = [];\r\n\r\nconst generateData = (_level, _preKey, _tns) => {\r\n const preKey = _preKey || '0';\r\n const tns = _tns || gData;\r\n\r\n const children = [];\r\n for (let i = 0; i < x; i++) {\r\n const key = `${preKey}-${i}`;\r\n tns.push({\r\n title: key,\r\n key\r\n });\r\n if (i < y) {\r\n children.push(key);\r\n }\r\n }\r\n if (_level < 0) {\r\n return tns;\r\n }\r\n const level = _level - 1;\r\n children.forEach((key, index) => {\r\n tns[index].children = [];\r\n console.log('index--' + index + \"gData--\" + gData);\r\n return generateData(level, key, tns[index].children);\r\n });\r\n};\r\ngenerateData(z);\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\n\r\nclass Demo2 extends Component {\r\n constructor(props) {\r\n debugger;\r\n super(props);\r\n this.state = {\r\n expandedKeys: ['0-0-0', '0-0-1'],\r\n autoExpandParent: true,\r\n checkedKeys: ['0-0-0'],\r\n selectedKeys: [],\r\n };\r\n this.onExpand = this.onExpand.bind(this);\r\n this.onCheck = this.onCheck.bind(this);\r\n this.onSelect = this.onSelect.bind(this);\r\n }\r\n onExpand(expandedKeys) {\r\n debugger;\r\n console.log('onExpand', arguments);\r\n // if not set autoExpandParent to false, if children expanded, parent can not collapse.\r\n // or, you can remove all expanded children keys.\r\n this.setState({\r\n expandedKeys,\r\n autoExpandParent: false,\r\n });\r\n }\r\n onCheck(checkedKeys) {\r\n this.setState({\r\n checkedKeys,\r\n selectedKeys: ['0-3', '0-4'],\r\n });\r\n }\r\n onSelect(selectedKeys, info) {\r\n console.log('onSelect', info);\r\n this.setState({\r\n selectedKeys\r\n });\r\n }\r\n render() {\r\n debugger;\r\n const loop = data => data.map((item) => {\r\n if (item.children) {\r\n return (\r\n \r\n {loop(item.children)}\r\n \r\n );\r\n }\r\n return ;\r\n });\r\n return (\r\n \r\n {loop(gData)}\r\n \r\n );\r\n }\r\n};\r\n\r\n\r\n","desc":""},{"example":,"title":" Tree 拖拽使用事例","code":"/**\r\n*\r\n* @title Tree 拖拽使用事例\r\n* @description 拖动结点插入到另一个结点后面或者其他的父节点里面。\r\n*\r\n*/\r\n\r\n\r\n\r\nimport React, { Component } from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst x = 3;\r\nconst y = 2;\r\nconst z = 1;\r\nconst gData = [];\r\n\r\nconst generateData = (_level, _preKey, _tns) => {\r\n const preKey = _preKey || '0';\r\n const tns = _tns || gData;\r\n\r\n const children = [];\r\n for (let i = 0; i < x; i++) {\r\n const key = `${preKey}-${i}`;\r\n tns.push({ title: key, key });\r\n if (i < y) {\r\n children.push(key);\r\n }\r\n }\r\n if (_level < 0) {\r\n return tns;\r\n }\r\n const level = _level - 1;\r\n children.forEach((key, index) => {\r\n tns[index].children = [];\r\n return generateData(level, key, tns[index].children);\r\n });\r\n};\r\ngenerateData(z);\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nclass Demo3 extends Component{\r\n constructor(props) {\r\n super(props);\r\n this.state = {\r\n gData,\r\n expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],\r\n };\r\n this.onDragEnter = this.onDragEnter.bind(this);\r\n this.onDrop = this.onDrop.bind(this);\r\n }\r\n onDragEnter(info) {\r\n console.log(info);\r\n // expandedKeys 需要受控时设置\r\n // this.setState({\r\n // expandedKeys: info.expandedKeys,\r\n // });\r\n }\r\n onDrop(info) {\r\n console.log(info);\r\n const dropKey = info.node.props.eventKey;\r\n const dragKey = info.dragNode.props.eventKey;\r\n // const dragNodesKeys = info.dragNodesKeys;\r\n const loop = (data, key, callback) => {\r\n data.forEach((item, index, arr) => {\r\n if (item.key === key) {\r\n return callback(item, index, arr);\r\n }\r\n if (item.children) {\r\n return loop(item.children, key, callback);\r\n }\r\n });\r\n };\r\n const data = [...this.state.gData];\r\n let dragObj;\r\n loop(data, dragKey, (item, index, arr) => {\r\n arr.splice(index, 1);\r\n dragObj = item;\r\n });\r\n if (info.dropToGap) {\r\n let ar;\r\n let i;\r\n loop(data, dropKey, (item, index, arr) => {\r\n ar = arr;\r\n i = index;\r\n });\r\n ar.splice(i, 0, dragObj);\r\n } else {\r\n loop(data, dropKey, (item) => {\r\n item.children = item.children || [];\r\n // where to insert 示例添加到尾部,可以是随意位置\r\n item.children.push(dragObj);\r\n });\r\n }\r\n this.setState({\r\n gData: data,\r\n });\r\n }\r\n render() {\r\n const loop = data => data.map((item) => {\r\n if (item.children && item.children.length) {\r\n return {loop(item.children)};\r\n }\r\n return ;\r\n });\r\n return (\r\n \r\n {loop(this.state.gData)}\r\n \r\n );\r\n }\r\n};\r\n\r\n","desc":" 拖动结点插入到另一个结点后面或者其他的父节点里面。"},{"example":,"title":" Tree可搜索事例","code":"/**\r\n*\r\n* @title Tree可搜索事例\r\n* @description\r\n*\r\n*/\r\n\r\n\r\nimport React, { Component } from 'react';\r\nimport FormControl from 'bee-form-control';\r\nimport Tree from 'bee-tree';\r\n\r\nconst x = 3;\r\nconst y = 2;\r\nconst z = 1;\r\nconst gData = [];\r\n\r\nconst generateData = (_level, _preKey, _tns) => {\r\n const preKey = _preKey || '0';\r\n const tns = _tns || gData;\r\n\r\n const children = [];\r\n for (let i = 0; i < x; i++) {\r\n const key = `${preKey}-${i}`;\r\n tns.push({ title: key, key });\r\n if (i < y) {\r\n children.push(key);\r\n }\r\n }\r\n if (_level < 0) {\r\n return tns;\r\n }\r\n const level = _level - 1;\r\n children.forEach((key, index) => {\r\n tns[index].children = [];\r\n return generateData(level, key, tns[index].children);\r\n });\r\n};\r\ngenerateData(z);\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nconst dataList = [];\r\nconst generateList = (data) => {\r\n for (let i = 0; i < data.length; i++) {\r\n const node = data[i];\r\n const key = node.key;\r\n dataList.push({ key, title: key });\r\n if (node.children) {\r\n generateList(node.children, node.key);\r\n }\r\n }\r\n};\r\ngenerateList(gData);\r\n\r\nconst getParentKey = (key, tree) => {\r\n let parentKey;\r\n for (let i = 0; i < tree.length; i++) {\r\n const node = tree[i];\r\n if (node.children) {\r\n if (node.children.some(item => item.key === key)) {\r\n parentKey = node.key;\r\n } else if (getParentKey(key, node.children)) {\r\n parentKey = getParentKey(key, node.children);\r\n }\r\n }\r\n }\r\n return parentKey;\r\n};\r\n\r\n\r\nclass Demo4 extends Component {\r\n constructor(props) {\r\n super(props);\r\n this.state = {\r\n expandedKeys: [],\r\n searchValue: '',\r\n autoExpandParent: true,\r\n }\r\n }\r\n onExpand = (expandedKeys) => {\r\n this.setState({\r\n expandedKeys,\r\n autoExpandParent: false,\r\n });\r\n }\r\n onChange = (e) => {\r\n const value = e.target.value;\r\n const expandedKeys = [];\r\n dataList.forEach((item) => {\r\n if (item.key.indexOf(value) > -1) {\r\n expandedKeys.push(getParentKey(item.key, gData));\r\n }\r\n });\r\n const uniqueExpandedKeys = [];\r\n expandedKeys.forEach((item) => {\r\n if (item && uniqueExpandedKeys.indexOf(item) === -1) {\r\n uniqueExpandedKeys.push(item);\r\n }\r\n });\r\n this.setState({\r\n expandedKeys: uniqueExpandedKeys,\r\n searchValue: value,\r\n autoExpandParent: true,\r\n });\r\n }\r\n render() {\r\n const { searchValue, expandedKeys, autoExpandParent } = this.state;\r\n const loop = data => data.map((item) => {\r\n const index = item.key.search(searchValue);\r\n const beforeStr = item.key.substr(0, index);\r\n const afterStr = item.key.substr(index + searchValue.length);\r\n const title = index > -1 ? (\r\n \r\n {beforeStr}\r\n {searchValue}\r\n {afterStr}\r\n \r\n ) : {item.key};\r\n if (item.children) {\r\n return (\r\n \r\n {loop(item.children)}\r\n \r\n );\r\n }\r\n return ;\r\n });\r\n return (\r\n
\r\n \r\n \r\n {loop(gData)}\r\n \r\n
\r\n );\r\n }\r\n}\r\n\r\n","desc":""},{"example":,"title":" Tree异步数据加载","code":"/**\r\n *\r\n * @title Tree异步数据加载\r\n * @description 当点击展开,异步获取子节点数据\r\n *\r\n */\r\n\r\n\r\nimport React, {\r\n Component\r\n} from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst x = 3;\r\nconst y = 2;\r\nconst z = 1;\r\nconst gData = [];\r\n\r\nconst generateData = (_level, _preKey, _tns) => {\r\n debugger;\r\n const preKey = _preKey || '0';\r\n const tns = _tns || gData;\r\n\r\n const children = [];\r\n for (let i = 0; i < x; i++) {\r\n const key = `${preKey}-${i}`;\r\n tns.push({\r\n title: key,\r\n key\r\n });\r\n if (i < y) {\r\n children.push(key);\r\n }\r\n }\r\n debugger;\r\n if (_level < 0) {\r\n return tns;\r\n }\r\n const level = _level - 1;\r\n children.forEach((key, index) => {\r\n tns[index].children = [];\r\n return generateData(level, key, tns[index].children);\r\n });\r\n};\r\ngenerateData(z);\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nfunction generateTreeNodes(treeNode) {\r\n debugger;\r\n const arr = [];\r\n const key = treeNode.props.eventKey;\r\n for (let i = 0; i < 3; i++) {\r\n arr.push({\r\n name: `leaf ${key}-${i}`,\r\n key: `${key}-${i}`\r\n });\r\n }\r\n return arr;\r\n}\r\n\r\nfunction setLeaf(treeData, curKey, level) {\r\n debugger;\r\n const loopLeaf = (data, lev) => {\r\n const l = lev - 1;\r\n data.forEach((item) => {\r\n if ((item.key.length > curKey.length) ? item.key.indexOf(curKey) !== 0 :\r\n curKey.indexOf(item.key) !== 0) {\r\n return;\r\n }\r\n if (item.children) {\r\n loopLeaf(item.children, l);\r\n } else if (l < 1) {\r\n item.isLeaf = true;\r\n }\r\n });\r\n };\r\n loopLeaf(treeData, level + 1);\r\n}\r\n\r\nfunction getNewTreeData(treeData, curKey, child, level) {\r\n debugger;\r\n const loop = (data) => {\r\n if (level < 1 || curKey.length - 3 > level * 2) return;\r\n data.forEach((item) => {\r\n if (curKey.indexOf(item.key) === 0) {\r\n if (item.children) {\r\n loop(item.children);\r\n } else {\r\n item.children = child;\r\n }\r\n }\r\n });\r\n };\r\n loop(treeData);\r\n setLeaf(treeData, curKey, level);\r\n}\r\n\r\nclass Demo5 extends Component {\r\n constructor(props) {\r\n super(props);\r\n this.state = {\r\n treeData: [],\r\n };\r\n this.onSelect = this.onSelect.bind(this);\r\n this.onLoadData = this.onLoadData.bind(this);\r\n }\r\n componentDidMount() {\r\n setTimeout(() => {\r\n this.setState({\r\n treeData: [{\r\n name: 'pNode 01',\r\n key: '0-0'\r\n }, {\r\n name: 'pNode 02',\r\n key: '0-1'\r\n }, {\r\n name: 'pNode 03',\r\n key: '0-2',\r\n isLeaf: true\r\n }, ],\r\n });\r\n }, 100);\r\n }\r\n onSelect(info) {\r\n console.log('selected', info);\r\n }\r\n onLoadData(treeNode) {\r\n debugger;\r\n return new Promise((resolve) => {\r\n setTimeout(() => {\r\n const treeData = [...this.state.treeData];\r\n getNewTreeData(treeData, treeNode.props.eventKey, generateTreeNodes(treeNode), 2);\r\n this.setState({\r\n treeData\r\n });\r\n resolve();\r\n }, 1000);\r\n });\r\n }\r\n render() {\r\n const loop = data => data.map((item) => {\r\n if (item.children) {\r\n return {loop(item.children)};\r\n }\r\n return ;\r\n });\r\n const treeNodes = loop(this.state.treeData);\r\n return (\r\n \r\n {treeNodes}\r\n \r\n );\r\n }\r\n};\r\n\r\n","desc":" 当点击展开,异步获取子节点数据"},{"example":,"title":" Tree基本使用事例自定义图标","code":"/**\r\n *\r\n * @title Tree基本使用事例自定义图标\r\n * @description 添加openIcon、closeIcon属性\r\n *\r\n */\r\n\r\n\r\nimport React, {\r\n\tComponent\r\n} from 'react';\r\nimport Tree from 'bee-tree';\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nconst defaultProps = {\r\n\tkeys: ['0-0-0', '0-0-1']\r\n}\r\nconsole.log(Tree);\r\nclass Demo1 extends Component {\r\n\tconstructor(props) {\r\n\t\tsuper(props);\r\n\t\tconst keys = this.props.keys;\r\n\t\tthis.state = {\r\n\t\t\tdefaultExpandedKeys: keys,\r\n\t\t\tdefaultSelectedKeys: keys,\r\n\t\t\tdefaultCheckedKeys: keys,\r\n\t\t};\r\n\t}\r\n\tonSelect(info) {\r\n\t\tconsole.log('selected', info);\r\n\t}\r\n\tonCheck(info) {\r\n\t\tconsole.log('onCheck', info);\r\n\t}\r\n\trender() {\r\n\t\treturn (\r\n\r\n\t\t\t\r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t \r\n\t sss} key=\"0-0-1-0\" />\r\n\t \r\n\t \r\n\t \r\n\t\t);\r\n\t}\r\n}\r\n\r\nDemo1.defaultProps = defaultProps;\r\n\r\n\r\n","desc":" 添加openIcon、closeIcon属性"},{"example":,"title":" Tree增加节点","code":"/**\r\n *\r\n * @title Tree增加节点\r\n * @description \r\n *\r\n */\r\n\r\n\r\nimport React, {\r\n Component\r\n} from 'react';\r\nimport Tree from 'bee-tree';\r\nimport Button from 'bee-button';\r\n\r\nconst TreeNode = Tree.TreeNode;\r\n\r\nclass Demo7 extends Component {\r\n constructor(props) {\r\n super(props);\r\n debugger;\r\n console.log('keys--' + this.props.keys);\r\n this.state = {\r\n treeData: [],\r\n defaultExpandedKeys: ['0-0', '0-1', '0-2']\r\n };\r\n this.onSelect = this.onSelect.bind(this);\r\n this.addNode = this.addNode.bind(this);\r\n this.clickFun = this.clickFun.bind(this);\r\n }\r\n componentDidMount() {\r\n setTimeout(() => {\r\n this.setState({\r\n treeData: [{\r\n name: 'pNode 01',\r\n key: '0-0',\r\n children: [{\r\n name: 'leaf 0-0-0',\r\n key: '0-0-0'\r\n }, {\r\n name: 'leaf 0-0-1',\r\n key: '0-0-1'\r\n }]\r\n }, {\r\n name: 'pNode 02',\r\n key: '0-1',\r\n children: [{\r\n name: 'leaf 0-1-0',\r\n key: '0-1-0'\r\n }, {\r\n name: 'leaf 0-1-1',\r\n key: '0-1-1'\r\n }]\r\n }, {\r\n name: 'pNode 03',\r\n key: '0-2',\r\n isLeaf: true\r\n }, ],\r\n });\r\n }, 100);\r\n }\r\n /**\r\n * 增加节点\r\n * @param string prKey [父节点key]\r\n * @param object nodeItem [子节点信息]\r\n */\r\n addNode(prKey, nodeItem) {\r\n const data = this.state.treeData;\r\n data.forEach((item) => {\r\n if (prKey === item.key) {\r\n if (!item.children) {\r\n item.children = [];\r\n }\r\n // 如果key不存在就动态生成一个\r\n if (!nodeItem.key) {\r\n nodeItem.key = prKey + item.children.length + 1;\r\n }\r\n item.children.push(nodeItem);\r\n }\r\n });\r\n this.setState({\r\n data\r\n });\r\n }\r\n onSelect(info) {\r\n console.log('selected', info);\r\n }\r\n /**\r\n * 点击button事件\r\n */\r\n clickFun() {\r\n let prKey, nodeItem;\r\n prKey = '0-2';\r\n nodeItem = {\r\n name: 'leaf 0-2-0'\r\n }\r\n this.addNode(prKey, nodeItem);\r\n }\r\n\r\n render() {\r\n const loop = data => data.map((item) => {\r\n if (item.children) {\r\n return {loop(item.children)};\r\n }\r\n return ;\r\n });\r\n const treeNodes = loop(this.state.treeData);\r\n console.log('defaultKeys--' + this.state.defaultExpandedKeys);\r\n return (\r\n
\r\n \r\n {treeNodes}\r\n \r\n \r\n
\r\n );\r\n }\r\n};\r\n\r\n","desc":" "}] class Demo extends Component { diff --git a/src/Tree.js b/src/Tree.js index ce75bff..0a2cd98 100644 --- a/src/Tree.js +++ b/src/Tree.js @@ -268,6 +268,7 @@ class Tree extends React.Component { } onSelect(treeNode) { + debugger; const props = this.props; const selectedKeys = [...this.state.selectedKeys]; const eventKey = treeNode.props.eventKey; @@ -437,6 +438,7 @@ class Tree extends React.Component { } getDragNodes(treeNode) { + debugger; const dragNodesKeys = []; const tPArr = treeNode.props.pos.split('-'); loopAllChildren(this.props.children, (item, index, pos, newKey) => {