我正在尝试实现:原始节点应保持在其位置并对其进行拖放,必须在放置位置创建一个新节点。原稿应保持在初始位置。
拖动重复节点应该只是改变它的位置。
import React, { Component, MouseEvent } from 'react'
import ReactFlow from 'react-flow-renderer'
export default class Platform extends Component {
constructor(props) {
super(props)
this.state = {
duplicates : [],
original: [{id: "original", data: {label: 'original', duplicate: false }, position: {x:100, y:100}, style: this.style}]
}
}
lastId = 0
style = { background: '#ccc', width: 50, height: 50, borderRadius: 50}
onNodeDragStart = (evt: MouseEvent, node: Node) => {
}
onNodeDragStop = (evt: MouseEvent, node: Node) => {
if(!node.data.duplicate){
node.id = (this.lastId++).toString(10)
node.data.duplicate = true
node.data.label = "duplicate"
this.setState(prevState => ({
duplicates: prevState.duplicates.concat(node)
}))
}
}
render() {
console.log("state: ", this.state)
const elements = this.state.original.concat(this.state.duplicates)
const graphStyles = { width: '100%', height: '800px' };
return (
<div>
<ReactFlow onNodeDragStart={this.onNodeDragStart} onNodeDragStop={this.onNodeDragStop} style={graphStyles} elements={elements}></ReactFlow>
</div>
)
}
}
拖放后在放置位置创建一个重复节点。当我看到组件的状态时,它仍然显示原来的位置为position:{x:100, y:100}
. 但它不在那个位置。