欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年02月24日 09:34:54   作者:小菜鳥飛飛飛~  
本文主要介紹了react拖拽組件react-sortable-hoc的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用react-sortable-hoc實現(xiàn)拖拽

如圖:

在這里插入圖片描述

提示:下面案例可供參考

1.文件1

代碼如下(示例):文件名稱:./dragcomponents

import * as React from 'react'
import {
    sortableContainer,
    sortableElement,
    sortableHandle,
} from "react-sortable-hoc"; // 拖拽的關(guān)鍵組件

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);
        }
    }

    // 拖拽時返回新數(shù)組
    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);
    }
    // 數(shù)據(jù)更新
    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} />
        );
    });

    // 拖拽后回調(diào)
    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>控件標(biāo)簽顯示名稱<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}/>

到此這篇關(guān)于react拖拽組件react-sortable-hoc的使用的文章就介紹到這了,更多相關(guān)react拖拽組件react-sortable-hoc內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • react后臺系統(tǒng)最佳實踐示例詳解

    react后臺系統(tǒng)最佳實踐示例詳解

    這篇文章主要為大家介紹了react后臺系統(tǒng)最佳實踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • React技巧之中斷map循環(huán)的方法詳解

    React技巧之中斷map循環(huán)的方法詳解

    這篇文章主要和大家來分享一下React的技巧之如何中斷map循環(huán),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2023-06-06
  • 基于Webpack5 Module Federation的業(yè)務(wù)解耦實踐示例

    基于Webpack5 Module Federation的業(yè)務(wù)解耦實踐示例

    這篇文章主要為大家介紹了基于Webpack5 Module Federation的業(yè)務(wù)解耦實踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • react native仿微信PopupWindow效果的實例代碼

    react native仿微信PopupWindow效果的實例代碼

    本篇文章主要介紹了react native仿微信PopupWindow效果的實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • React?useEffect異步操作常見問題小結(jié)

    React?useEffect異步操作常見問題小結(jié)

    本文主要介紹了React?useEffect異步操作常見問題小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 通過實例學(xué)習(xí)React中事件節(jié)流防抖

    通過實例學(xué)習(xí)React中事件節(jié)流防抖

    這篇文章主要介紹了通過實例學(xué)習(xí)React中事件節(jié)流防抖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下
    2019-06-06
  • React中實現(xiàn)組件通信的幾種方式小結(jié)

    React中實現(xiàn)組件通信的幾種方式小結(jié)

    在構(gòu)建復(fù)雜的React應(yīng)用時,組件之間的通信是至關(guān)重要的,從簡單的父子組件通信到跨組件狀態(tài)同步,不同組件之間的通信方式多種多樣,下面我們認(rèn)識react組件通信的幾種方式,需要的朋友可以參考下
    2024-04-04
  • 一文詳解React組件API

    一文詳解React組件API

    這篇文章主要介紹了React的組件API,及組件API的用法詳解,文中有詳細(xì)的代碼示例,對學(xué)習(xí)或工作有一定的參考價值,感興趣的同學(xué)可以閱讀本文
    2023-04-04
  • React-intl 實現(xiàn)多語言的示例代碼

    React-intl 實現(xiàn)多語言的示例代碼

    本篇文章主要介紹了React-intl 實現(xiàn)多語言的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 基于React Context實現(xiàn)一個簡單的狀態(tài)管理的示例代碼

    基于React Context實現(xiàn)一個簡單的狀態(tài)管理的示例代碼

    本文主要介紹了基于React Context實現(xiàn)一個簡單的狀態(tài)管理的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評論