我已经在我的React
应用程序导航栏中添加了一个功能,使用户可以拖动项目并更改位置。(运作良好!)
现在我正在尝试递归地渲染导航树,以便可以拖动和替换内部导航(不重新设置父级关系)。
递归渲染的工作正常,但问题是拖动内部子项会导致整个父节点被拖动。
NodeWrapper.js
:
export class NodeWrapper extends Component {
public render() {
return (
<DragList onItemMoved={this.props.onItemMoved}>
{this.props.navItems
.map((component, index) => (
<Node key={`node-${index}`} {...component}/>
))}
</DragList>
);
}
}
Node.js
:
public render() {
return (
<div>
<span>{this.props.label}</span>
{this.props.childNodes && (
<NodeWrapper navItems={this.props.childNodes} />
)}
</div>
);
}
注意,
DragList
组件非常庞大,我认为它与问题无关,如果需要,我会添加。基本上,它使用事件处理程序渲染一个子级列表,并允许拖动它们。我尝试在拖动事件处理程序中添加
event.stopPropagation()
,但仍然会捕获并拖动整个父节点。我该如何在所有级别之间执行拖动?
DragList
的文档吗?HTML 元素有一个draggable
属性,可以设置为false
来禁用拖拽。 - Nice Books