react拖动组件代码详解
Admin 2023-04-01 群英技术资讯 960 次浏览
这篇文章主要介绍“react拖动组件代码详解”,有一些人在react拖动组件代码详解的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。使用react-sortable-hoc实现拖拽
如图:

提示:下面案例可供参考
代码如下(示例):文件名称:./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;
代码如下(示例):文件名称’./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
代码如下(示例):
import Usedrag from './usedrag';
<Usedrag renderData={renderData}/>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要给大家介绍了关于利用JS判断元素是否为数组的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家详细介绍了Vue dialog模态框的封装方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
JavaScript分页组件怎样使用?分页组件是web开发中常见的组件,因此本文就分享个JavaScript分页组件使用示例给大家做个参考,我们需要实现的需求如下,接下来我们就来看看怎样实现吧。
这篇文章给大家分享的是微信小程序中的底部弹出框效果的实现。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
React路由传参方法有哪些?我们知道React中传参方式有很多,通过路由传参的方式也是必不可少的一种。因此,这篇文章就给大家分享一下React路由传参方法及实现,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008