一、入門指南
awe-dnd是一個基於React的強大的拖放框架。首先,你需要安裝React和awe-dnd:
npm install react awe-dnd
當你安裝完成後,你需要使用DragSource和DropTarget創建一個拖放組件。例如:
import { DragSource, DropTarget } from 'awe-dnd';
const draggable = {
beginDrag(props) {
return {
id: props.id
}
}
}
const droppable = {
drop(props, monitor) {
// do something
}
}
export default DropTarget('item', droppable, (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
isHovering: monitor.isOver()
}))(
DragSource('item', draggable, (connect, monitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging()
}))(ItemComponent)
);
請注意,beginDrag()函數必須返回一個對象,表示正在被拖動的數據。
二、定義DragSources和DropTargets
DragSource和DropTarget是React組件,允許您定義可以拖動或停放的組件。不同的元素可以被拖放,因此您可以定義多個DragSource和DropTarget。
三、DragSource屬性
DragSource在拖動開始時會發出通知,以便用戶知道何時開始移動元素。以下是DragSource常用的屬性:
beginDrag: 必填,拖放開始時執行的函數。endDrag: 拖放結束時發生的函數。canDrag: 函數,確定元素是否可以拖動。isDragging: 布爾值,表示元素是否正在拖動。
四、DropTarget屬性
與DragSource類似,DropTarget也是一個React組件。以下是DropTarget常用的屬性:
drop: 必填,拖放操作完成時的函數。hover: 當元素懸停在拖放目標底部時發生的函數。canDrop: 函數,確定元素是否可以放置到該組件中。isOver: 布爾值,表示元素是否正在拖放目標上方。
五、DragPreviewImage
DragPreviewImage允許您定義圖像的Node,在拖動時作為預覽顯示。
import { DragPreviewImage } from 'awe-dnd';const Item = ({ id, connectDragSource, connectDragPreview, isDragging }) => {
const ref = useRef(null);
useEffect(() => {
connectDragPreview(ref.current);
}, []);
return (
{/* Your regular component */}
原創文章,作者:LLTAV,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/351565.html
微信掃一掃
支付寶掃一掃