網站首頁 編程語言 正文
使用react-sortable-hoc實現拖拽
如圖:
提示:下面案例可供參考
1.文件1
代碼如下(示例):文件名稱:./dragcomponents
import * as React from 'react' import { sortableContainer, sortableElement, sortableHandle, } from "react-sortable-hoc"; // 拖拽的關鍵組件 const Sortable: React.FC<any> = (props) => { const { dataSource = [], ComSortItem, sortEnd } = props; // 拖拽時原列表替換 function arrayMoveMutable(array, fromIndex, toIndex) { const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex; if (startIndex >= 0 && startIndex < array.length) { const endIndex = toIndex < 0 ? array.length + toIndex : toIndex; const [item] = array.splice(fromIndex, 1); array.splice(endIndex, 0, item); } } // 拖拽時返回新數組 function arrayMoveImmutable(array, fromIndex, toIndex) { array = [...array]; arrayMoveMutable(array, fromIndex, toIndex); return array; } // 拖拽容器 const SortableContainer = sortableContainer(({ children }) => { return <div>{children}</div>; }); // 拖拽ico const DragHandle = sortableHandle((value1, sortIndex1) => ( <div id='ListItem' className='ListItem' > <div className="ChildCom"> <ComSortItem data={value1} index={sortIndex1} updateData={updateData} /> </div> </div> )); function handleDelete(index) { const List = [...dataSource]; List.splice(index, 1) sortEnd(List); } // 數據更新 function updateData(val, index) { const List = [...dataSource]; List[index] = val; sortEnd(List); } // 拖拽體 const SortableItem = sortableElement(({ value, sortIndex }) => { return ( // <div id='ListItem' className='ListItem' > // <DragHandle value1={value} sortIndex1={sortIndex} /> // </div> <DragHandle valuedata={value} sortIndexdata={sortIndex} /> ); }); // 拖拽后回調 const onSortEnd = ({ oldIndex, newIndex }) => { const List = arrayMoveImmutable(dataSource, oldIndex, newIndex); sortEnd(List); }; return ( <> <SortableContainer onSortEnd={onSortEnd} useDragHandle helperClass="row-dragging-item"> {dataSource.length > 0 && dataSource.map((value, index) => ( <SortableItem key={`sortable-item-${index}`} index={index} value={value} sortIndex={index} /> ))} </SortableContainer> </> ); } export default Sortable;
2.文件2
代碼如下(示例):文件名稱’./usedrag’
import * as React from 'react' import { Checkbox } from 'antd' import Sortable from './dragcomponents' import './index.scss' const _ = require('lodash') import store from './store' import { SAVE_RENDER_ALL_DATA } from './actionType' const Usedrag: React.FC<any> = (props) => { const { state, dispatch } = React.useContext(store); // 自定義拖拽體 const {upDateRenderData} = props const showdata ={...props.renderData} function AddForm(showdata) { return ( < div className='ItemBox'> <div className='name'><span className="icon iconfont iconyidongshu"></span>{showdata.data.valuedata.fieldName}</div> <div className='Opt'> <span>控件標簽顯示名稱<span>{showdata.data.valuedata.labelName}</span></span> <span>所占列寬<span>{showdata.data.valuedata.span}</span></span> {/* <Checkbox onChange={changeChecked} checked={checked} ></Checkbox> */} </div> </div> ) } const updateSource = (val) => { const arrdata: any = _.cloneDeep(props.renderData) const arr: any = _.cloneDeep(val) if(JSON.stringify(arrdata) !== JSON.stringify(arr)){ for (let i = 0; i <= arr.length - 1; i++) { arr[i].edit = 1; } } // upDateRenderData(arr) dispatch({ type: SAVE_RENDER_ALL_DATA, value: arr }) } return ( <div className='RightBox' > <div className='item-con' style={{ overflow: 'auto' }}> <Sortable className='sortable' dataSource={...props.renderData} ComSortItem={(p) => <AddForm {...p} />} sortEnd={(val) => { updateSource(val) }} /> </div> </div> ); }; export default Usedrag
3.使用
代碼如下(示例):
import Usedrag from './usedrag'; <Usedrag renderData={renderData}/>
原文鏈接:https://blog.csdn.net/weixin_43138550/article/details/128947986
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-02-19 .NET?與樹莓派WS28XX?燈帶的顏色漸變動畫效果的實現_實用技巧
- 2022-02-07 SSH連服務器提示“Permission denied(publickey,gssapi-keyex
- 2023-04-01 一文帶你深入了解Python中的二次移動平均法_python
- 2023-01-26 Python上下文管理器深入講解_python
- 2022-05-25 python教程之利用pyautogui圖形自動化擊敗重復性辦公任務_python
- 2022-11-10 pytorch人工智能之torch.gather算子用法示例_python
- 2022-10-20 Android開發App啟動流程與消息機制詳解_Android
- 2022-04-17 彈性布局 怎么讓某一列自適應元素內容的寬度
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支