實(shí)現(xiàn)方法:1、利用“import{DndProvider}from 'react-dnd'”定義一個(gè)可拖拽的范圍;2、利用“import{useDrag}from 'react-dnd'”將DragSource包裹住組件,使其可以拖動(dòng)即可。
本教程操作環(huán)境:Windows10系統(tǒng)、react17.0.1版、Dell G3電腦。
react-dnd怎么實(shí)現(xiàn)拖拽
React DnD是React和Redux核心作者 Dan Abramov創(chuàng)造的一組React 高階組件,可以在保持組件分離的前提下幫助構(gòu)建復(fù)雜的拖放接口。
React DnD 的需求
-
默認(rèn)使用 HTML5 拖放API,但支持
-
不直接操作 DOM
-
DOM 和拖放的源和目標(biāo)解耦
-
融入HTML5拖放中竊取類型匹配和數(shù)據(jù)傳遞的想法
React DnD 的特點(diǎn)
專注拖拽,不提供現(xiàn)成組件
React DnD提供了一組強(qiáng)大的原語(yǔ),但它不包含任何現(xiàn)成組件,而是采用包裹使用者的組件并注入 props 的方式。 它比jQuery UI等更底層,專注于使拖放交互正確,而把視覺(jué)方面的效果例如坐標(biāo)限制交給使用者處理。這其實(shí)是一種關(guān)注點(diǎn)分離的原則,例如React DnD不打算提供可排序組件,但是使用者可以基于它快速開(kāi)發(fā)任何需要的自定義的可排序組件。
單向數(shù)據(jù)流
類似于 React 一樣采取聲明式渲染,并且像 redux 一樣采用單向數(shù)據(jù)流架構(gòu),實(shí)際上內(nèi)部使用了 Redux
隱藏了平臺(tái)底層API的問(wèn)題
HTML5拖放API充滿了陷阱和瀏覽器的不一致。 React DnD為您內(nèi)部處理它們,因此使用者可以專注于開(kāi)發(fā)應(yīng)用程序而不是解決瀏覽器問(wèn)題。
可擴(kuò)展可測(cè)試
React DnD默認(rèn)提供了HTML5拖放API封裝,但它也允許您提供自定義的“后端(backend)”。您可以根據(jù)觸摸事件,鼠標(biāo)事件或其他內(nèi)容創(chuàng)建自定義DnD后端。例如,內(nèi)置的模擬后端允許您測(cè)試Node環(huán)境中組件的拖放交互。
為未來(lái)做好了準(zhǔn)備
React DnD不會(huì)導(dǎo)出mixins,并且對(duì)任何組件同樣有效,無(wú)論它們是使用ES6類,createReactClass還是其他React框架創(chuàng)建的。而且API支持了ES7 裝飾器。
示例如下:
1.1.使用DndProvider定義一個(gè)可以拖拽的范圍
/* * @Author: muge * @Date: 2021-12-04 16:59:25 * @LastEditors: Please set LastEditors * @LastEditTime: 2021-12-08 14:24:47 */ import React, { useState } from 'react'; import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import SourceBox from './SourceBox'; import TargetBox from './TargetBox'; import TreeBox from './TreeBox'; const item: any[] = [ { id: 1, name: 'muge', }, { id: 2, name: 'muxi', }, { id: 3, name: 'mugege', }, ]; const Container = () => { // 當(dāng)前拖拽項(xiàng) const [currentList, setCurrentList] = useState<any>({}); return ( // 類似redux數(shù)據(jù)傳輸 需要在最外層包裹對(duì)象 <DndProvider backend={HTML5Backend}> <h1>拖拽源組件 列表-----樹(shù)</h1> <div style={{ display: 'flex' }}> <div> {/* 列表拖拽源 */} {item.map((itemz: any, index: number) => ( <SourceBox setCurrentList={setCurrentList} item={itemz} key={index} /> ))} </div> {/* 注意,不要樹(shù)組件整體直接設(shè)置拖拽,抽成一個(gè)組件來(lái)遍歷每一項(xiàng) =》自定義渲染*/} {/* 樹(shù)形拖拽源 */} <TreeBox /> </div> <h1>拖拽放置目標(biāo)</h1> {/* 拖拽最終放置組件 */} <TargetBox currentList={currentList} /> </DndProvider> ); }; export default Container;
2.使用 DragSource 包裹住組件,使其可以進(jìn)行拖動(dòng)
/* * @Author: muge * @Date: 2021-12-07 14:26:08 * @LastEditors: Please set LastEditors * @LastEditTime: 2021-12-08 14:18:52 */ import { useDrag } from 'react-dnd'; const ItemTypes = { BOX: 'box', }; const style = { border: '1px dashed gray', backgroundColor: 'white', padding: '0.5rem 1rem', marginRight: '1rem', marginBottom: '1rem', cursor: 'move', }; const SourceBox = ({ item, setCurrentList }: any) => { const [{ opacity }, drag] = useDrag( () => ({ type: ItemTypes.BOX, collect: (monitor) => ({ opacity: monitor.isDragging() ? 0.4 : 1, }), item: () => item, //返回當(dāng)前列表項(xiàng)數(shù)據(jù) canDrag: (monitor) => { //是否取消拖拽 console.log(monitor, 'monitor131'); return true; }, // end(currentItem, monitor) { // monitor.getDropResult(); //獲取拖拽對(duì)象所處容器的數(shù)據(jù) // monitor.didDrop(); // 當(dāng)前容器能否放置拖拽對(duì)象 拖動(dòng)停止時(shí)觸發(fā) monitor.didDrop() && setCurrentList(currentItem); //在容器點(diǎn)松開(kāi) 才賦值 }, }), [], ); return ( <div ref={drag} style={{ ...style, opacity }}> {item.id}------{item.name} </div> ); }; export default SourceBox;
3.使用 DropTarget 包裹住組件,使其對(duì)拖動(dòng),懸?;?dropped 的兼容項(xiàng)目做出反應(yīng)。
/* * @Author: muge * @Date: 2021-12-07 14:26:08 * @LastEditors: Please set LastEditors * @LastEditTime: 2021-12-08 14:33:08 */ import React from 'react'; import { useDrop } from 'react-dnd'; const ItemTypes = { BOX: 'box', }; const style: any = { border: '1px solid gray', height: '15rem', width: '15rem', padding: '2rem', textAlign: 'center', }; const TargetBox = ({ currentList }: any) => { const [{ isActive, isOver, canDrop }, drop] = useDrop(() => ({ accept: ItemTypes.BOX, collect: (monitor) => ({ isActive: monitor.canDrop() && monitor.isOver(), isOver: monitor.isOver(), canDrop: monitor.canDrop(), }), // hover: (item, monitor) => { // console.log(item, 'item'); // console.log(monitor, 'monitor'); // }, })); // console.log(isOver, 'isOver'); // console.log(canDrop, 'canDrop'); return ( <div ref={drop} style={style}> {isActive ? 'Release to drop' : 'Drag item here'} <div style={{ backgroundColor: 'pink', height: 30, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 17, fontWeight: 600, width: '100%', }} > {JSON.stringify(currentList) !== '{}' ? JSON.stringify(currentList) : '當(dāng)前item'} </div> </div> ); }; export default TargetBox;
到此列表拖拽即可完成
推薦學(xué)習(xí):《react視頻教程》