|
| 1 | +/* eslint-disable no-console, react/no-access-state-in-setstate */ |
| 2 | +import React from 'react'; |
| 3 | +import { gData } from './utils/dataUtil'; |
| 4 | +import './draggable.less'; |
| 5 | +import '../../assets/index.less'; |
| 6 | +import Tree from 'rc-tree'; |
| 7 | + |
| 8 | +function allowDrop({ dropNode, dropPosition }) { |
| 9 | + if (!dropNode.children) { |
| 10 | + if (dropPosition === 0) return false; |
| 11 | + } |
| 12 | + return true; |
| 13 | +} |
| 14 | + |
| 15 | +const defaultData = [...gData, { title: 'non-draggable-title', key:'non-draggable-key'}] |
| 16 | + |
| 17 | +class Demo extends React.Component { |
| 18 | + state = { |
| 19 | + gData: defaultData, |
| 20 | + autoExpandParent: true, |
| 21 | + expandedKeys: [], |
| 22 | + }; |
| 23 | + |
| 24 | + onDragStart = info => { |
| 25 | + console.log('start', info); |
| 26 | + }; |
| 27 | + |
| 28 | + |
| 29 | + onDrop = info => { |
| 30 | + console.log(info); |
| 31 | + const dropKey = info.node.key; |
| 32 | + const dragKey = info.dragNode.key; |
| 33 | + const dropPos = info.node.pos.split("-"); |
| 34 | + const dropPosition = |
| 35 | + info.dropPosition - Number(dropPos[dropPos.length - 1]); |
| 36 | + |
| 37 | + const loop = (data, key, callback) => { |
| 38 | + for (let i = 0; i < data.length; i++) { |
| 39 | + if (data[i].key === key) { |
| 40 | + return callback(data[i], i, data); |
| 41 | + } |
| 42 | + if (data[i].children) { |
| 43 | + loop(data[i].children, key, callback); |
| 44 | + } |
| 45 | + } |
| 46 | + }; |
| 47 | + const data = [...this.state.gData]; |
| 48 | + |
| 49 | + // Find dragObject |
| 50 | + let dragObj; |
| 51 | + loop(data, dragKey, (item, index, arr) => { |
| 52 | + arr.splice(index, 1); |
| 53 | + dragObj = item; |
| 54 | + }); |
| 55 | + |
| 56 | + if (!info.dropToGap) { |
| 57 | + // Drop on the content |
| 58 | + loop(data, dropKey, (item) => { |
| 59 | + item.children = item.children || []; |
| 60 | + item.children.unshift(dragObj); |
| 61 | + }); |
| 62 | + } else if ( |
| 63 | + ((info.node).props.children || []).length > 0 && // Has children |
| 64 | + (info.node).props.expanded && // Is expanded |
| 65 | + dropPosition === 1 // On the bottom gap |
| 66 | + ) { |
| 67 | + loop(data, dropKey, (item) => { |
| 68 | + item.children = item.children || []; |
| 69 | + item.children.unshift(dragObj); |
| 70 | + }); |
| 71 | + } else { |
| 72 | + let ar = []; |
| 73 | + let i; |
| 74 | + loop(data, dropKey, (_item, index, arr) => { |
| 75 | + ar = arr; |
| 76 | + i = index; |
| 77 | + }); |
| 78 | + if (dropPosition === -1) { |
| 79 | + ar.splice(i, 0, dragObj); |
| 80 | + } else { |
| 81 | + ar.splice(i + 1, 0, dragObj); |
| 82 | + } |
| 83 | + } |
| 84 | + this.setState({ |
| 85 | + gData: data, |
| 86 | + }); |
| 87 | + }; |
| 88 | + |
| 89 | + onDragEnter = (info) => { |
| 90 | + console.log('dragEnter', info); |
| 91 | + // expandedKeys 需要受控时设置 |
| 92 | + // setExpandedKeys(info.expandedKeys) |
| 93 | + }; |
| 94 | + |
| 95 | + render() { |
| 96 | + return ( |
| 97 | + <div className="draggable-demo"> |
| 98 | + <h2>non-draggable-node-allow-drop</h2> |
| 99 | + <p>When a node is not draggable,but it can be drop</p> |
| 100 | + <div className="draggable-container"> |
| 101 | + <Tree |
| 102 | + className="draggable-tree" |
| 103 | + defaultExpandedKeys={this.state.expandedKeys} |
| 104 | + draggable={{ |
| 105 | + nodeDraggable: function (node) { |
| 106 | + return node.key !== "non-draggable-key"; |
| 107 | + } |
| 108 | + }} |
| 109 | + blockNode |
| 110 | + onDragEnter={this.onDragEnter} |
| 111 | + onDrop={this.onDrop} |
| 112 | + treeData={this.state.gData} |
| 113 | + /> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +export default Demo; |
0 commit comments