react拖拽组件react-sortable-hoc的使用

 更新时间:2023年02月24日 09:34:54   作者:小菜鸟飞飞飞~  
本文主要介绍了react拖拽组件react-sortable-hoc的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

使用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}/>

到此这篇关于react拖拽组件react-sortable-hoc的使用的文章就介绍到这了,更多相关react拖拽组件react-sortable-hoc内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React 高阶组件入门介绍

    React 高阶组件入门介绍

    本篇文章主要介绍了React高阶组件入门介绍,这篇文章中我们详细的介绍了什么是高阶组件,如何使用高阶组件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • React + Threejs + Swiper 实现全景图效果的完整代码

    React + Threejs + Swiper 实现全景图效果的完整代码

    全景图效果非常漂亮给人带来极好的用户体验效果,那么基于前端开发如何实现这种效果呢,下面小编给大家带来了React + Threejs + Swiper 实现全景图效果,感兴趣的朋友一起看看吧
    2021-06-06
  • react-router4按需加载(踩坑填坑)

    react-router4按需加载(踩坑填坑)

    这篇文章主要介绍了react-router4按需加载(踩坑填坑),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 详解如何在React中有效地监听键盘事件

    详解如何在React中有效地监听键盘事件

    React是一种流行的JavaScript库,用于构建用户界面,它提供了一种简单而灵活的方式来创建交互式的Web应用程序,在React中,我们经常需要监听用户的键盘事件,以便根据用户的输入做出相应的反应,本文将向您介绍如何在React中有效地监听键盘事件,并展示一些常见的应用场景
    2023-11-11
  • 路由react-router-dom的基本使用教程

    路由react-router-dom的基本使用教程

    在React中,路由是一套映射规则,是URL路径与组件的对应关系。使用React路由,就是配置路径和组件的对应关系,这篇文章主要介绍了路由react-router-dom的使用,需要的朋友可以参考下
    2023-02-02
  • 解决React报错Encountered two children with the same key

    解决React报错Encountered two children with the same key

    这篇文章主要为大家介绍了React报错Encountered two children with the same key解决方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • React 之最小堆min heap图文详解

    React 之最小堆min heap图文详解

    这篇文章主要为大家介绍了React 之最小堆min heap图文详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • React函数式组件Hook中的useState函数的详细解析

    React函数式组件Hook中的useState函数的详细解析

    Hook 就是 JavaScript 函数,这个函数可以帮助你钩入(hook into) React State以及生命周期等特性,这篇文章主要介绍了React Hook useState函数的详细解析的相关资料,需要的朋友可以参考下
    2022-10-10
  • React组件设计模式之组合组件应用实例分析

    React组件设计模式之组合组件应用实例分析

    这篇文章主要介绍了React组件设计模式之组合组件,结合实例形式分析了React组件设计模式中组合组件相关概念、原理、应用场景与操作注意事项,需要的朋友可以参考下
    2020-04-04
  • React中的Context应用场景分析

    React中的Context应用场景分析

    这篇文章主要介绍了React中的Context应用场景分析,Context 提供了一种在组件之间共享数据的方式,而不必显式地通过组件树的逐层传递 props,通过实例代码给大家介绍使用步骤,感兴趣的朋友跟随小编一起看看吧
    2021-06-06

最新评论