< p>我创建了一个简单的< a href =" http://react-dnd.github.io/react-dnd/docs-overview.html" rel =" nofollow noreferrer"> Dnd example< / a>,包含以下组件:< / p>
< p> Externalevents.js(拖动源)< / p>
< pre>< code>导入React,{Component}来自'反应&#39 ;;
从' react-dnd'导入{DragSource};
import' ./ customCal.css&#39 ;;
const ItemTypes = {
活动:'活动'
};
const eventSource = {
beginDrag(道具){
return {};
},
endDrag(props,monitor){
const item = monitor.getItem()
const dropResult = monitor.getDropResult()
if(dropResult){
alert(`你把$ {item.name}丢入$ {dropResult.name}!`)
}
},
}
function collect(connect,monitor){
返回{
connectDragSource:connect.dragSource(),
connectDragPreview:connect.dragPreview(),
isDragging:monitor.isDragging()
}
}
class externalEvents扩展Component {
render(){
const {connectDragSource,isDragging} = this.props;
const {name} = this.props;
return connectDragSource(
< span> {name}< / span>
)
}
}
export default DragSource(ItemTypes.EVENT,eventSource,collect)(externalEvents);
< /代码>< /预>
< p> customCal.js(放弃目标)< / p>
< pre>< code>导入React,{Component}来自'反应&#39 ;;
从' react-dnd'中导入{DropTarget};
import' ./ customCal.css&#39 ;;
const calTarget = {
canDrop(道具){
返回true;
},
drop(道具){
}
};
function collect(connect,monitor){
返回{
connectDropTarget:connect.dropTarget(),
isOver:monitor.isOver(),
canDrop:monitor.canDrop()
};
}
const ItemTypes = {
活动:'活动'
};
class customCal extends Component {
render(){
const {connectDropTarget,isOver,canDrop} = this.props;
return connectDropTarget(
< div className =" day"风格= {{
backgroundColor:canDrop? '#EEE' :''
}>
< span className =" dayDetails">
<跨度>星期一< /跨度>
< span> 1月1日< / span>
< / span>
< / DIV>
);
}
}
export default DropTarget(ItemTypes.EVENT,calTarget,collect)(customCal);
< /代码>< /预>
< p>以及加载这两个组件的父组件。
customCalLoader.js< / p>
< pre>< code>导入React,{Component}来自'反应&#39 ;;
import' ./ customCal.css&#39 ;;
从' ./ customCal'中导入CustomCal;
从' ./ externalEvents'中导入ExternalEvents;
从' react-dnd'导入{DragDropContext,DragDropContextProvider};
从' react-dnd-html5-backend'中导入HTML5Backend;
class customCalLoader扩展Component {
render(){
回来(
< div className =" customCal__Loader">
< div className =" event__box">
< ExternalEvents name =" Nike" />
< ExternalEvents name =" RT" />
< ExternalEvents name =" Jaboog" />
< / DIV>
< div className =" customCal__main">
< CustomCal />
< / div>
< / DIV>
)
}
}
export default DragDropContext(HTML5Backend)(customCalLoader);
< /代码>< /预>
< p>现在当前拖动工作并且当成功地悬停在放置目标上时,放置目标颜色改变,因此拖放工作正在工作,但是现在我想要的是当我拖动元素时并将其放在dropTarget上,我希望它坚持放下目标,我如何在React Dnd中执行此操作? < / p为H.
< p>我想实现< a href =" https://fullcalendar.io/docs/external-dragging-demo" rel =" nofollow noreferrer"> this< / a> (这是在jQuery中),我如何在React Dnd中实现这一点? < / p为H.