• react使用react-sortable-hoc实现拖拽


    react-sortable-hoc拖拽

    安装

     npm install react-sortable-hoc --save

    代码如下(示例):

    1. import React, { useImperativeHandle, forwardRef, memo, useState } from 'react';
    2. import { DrawerForm } from '@ant-design/pro-form';
    3. import { message, Select ,Table} from 'antd';
    4. import { MenuOutlined } from '@ant-design/icons';
    5. import { useUpdateEffect } from 'react-use';
    6. // 核心代码
    7. import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
    8. type ModalProps = {
    9.         ref?: React.MutableRefObject<SortTrackDrawerRefObject | undefined>;
    10.         onUpdated: () => void;
    11. };
    12. export type SortTrackDrawerRefObject = {
    13.         openModal: () => void;
    14. };
    15. // 定义拖拽的图标
    16. const DragHandle = SortableHandle(() => <MenuOutlined style={{ cursor: 'move', color: '#999' }} />);
    17. const columns = [
    18.         {
    19.                 title: '排序',
    20.                 align: 'center',
    21.                 dataIndex: 'sort',
    22.                 width: 30,
    23.                 className: 'drag-visible',
    24.                 render: () => <DragHandle />,
    25.         },
    26.         {
    27.                 title: '音频名称',
    28.                 dataIndex: 'name',
    29.                 className: 'drag-visible',
    30.         },
    31. ];
    32. const SortTrackDrawer: React.ForwardRefRenderFunction<SortTrackDrawerRefObject, ModalProps> = memo(
    33. forwardRef((sProps, ref) => {
    34.         const [visible, setVisible] = useState<boolean>(false);
    35.         // 拖拽体
    36.         const SortableItem = SortableElement((props: any) => <tr {...props} />);
    37.        // 拖拽容器
    38.         const SortContainer = SortableContainer((props: any) => <tbody {...props} />);
    39.        
    40.       // 拖拽时原列表替换
    41.         const arrayMoveMutable=(array: any[], fromIndex: number, toIndex: number) =>{
    42.                 const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
    43.                 if (startIndex >= 0 && startIndex < array.length) {
    44.                         const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
    45.                         const [item] = array.splice(fromIndex, 1);
    46.                         array.splice(endIndex, 0, item);
    47.                 }
    48.         }
    49.         // 拖拽时返回新数组
    50.         const arrayMoveImmutable=(array: any[], fromIndex: number, toIndex: number) =>{
    51.                 array = [...array];
    52.                 arrayMoveMutable(array, fromIndex, toIndex);
    53.                 return array;
    54.         }
    55.       
    56.         // 拖拽后回调
    57.         const onSortEnd = ({ oldIndex, newIndex }:
    58.                 { oldIndex: number; newIndex: number }) => {
    59.                         if (oldIndex !== newIndex) {
    60.                                 const newData = arrayMoveImmutable([...tableData], oldIndex,                                 newIndex).filter((el) => !!el);
    61.                                 setTableData([...newData]);
    62.                         }
    63.         };
    64.       
    65.        //  获取表格数据
    66.         const getTableData = async () => {
    67.                 try {
    68.                         let res = [{
    69.                                 name:"测试13",
    70.                                 orderNum:1
    71.                                 uid:1,
    72.                           }]
    73.                         setTableData(res);
    74.                 } finally {
    75.                 }
    76.         };
    77.         
    78.        // 确定按钮返回的数据
    79.         const handleFinish = async () => {
    80.                 const uids = tableData.map((item) => item.uid);
    81.                 console.log(uids)
    82.                 message.success('排序已更新');
    83.                 return true;
    84.         };
    85.        
    86.        // 拖拽容器方法
    87.         const DraggableContainer = (props: any) => (
    88.                 <SortContainer
    89.                         useDragHandle
    90.                          disableAutoscroll
    91.                         helperClass="row-dragging"
    92.                         onSortEnd={onSortEnd}
    93.                         {...props}
    94.                 />
    95.         );
    96.         // 拖拽体方法
    97.         const DraggableBodyRow = (props: any) => {
    98.                 const { className, style, ...restProps } = props;
    99.                 const index = tableData.findIndex((x) => x.uid === restProps['data-row-key']);
    100.                 return <SortableItem index={index} {...restProps} />;
    101.         };
    102.         // 进入组件加载数据
    103.         useUpdateEffect(() => {
    104.                 getTableData();
    105.         }, [orderType]);
    106. return (
    107.         <DrawerForm
    108.                 width={isMobile ? '100%' : 520}
    109.                 onVisibleChange={setVisible}
    110.                 title={
    111.                         <div className="sort-track-drawer-header">
    112.                                 <div>拖拽排序({tableData.length})</div>
    113.                         </div>
    114.                 }
    115.                 visible={visible}
    116.                 onFinish={handleFinish}
    117.                 drawerProps={{ closable: false }}
    118.                 submitter={{
    119.                         searchConfig: {
    120.                                 submitText: '保存排序',
    121.                                 resetText: '取消',
    122.                         },
    123.                 }}
    124.         >
    125.                 <Table
    126.                 pagination={false}
    127.                 dataSource={tableData}
    128.                 columns={columns as any}
    129.                 rowKey="uid"
    130.                 size="small"
    131.                 showHeader={false}
    132.                 loading={loading}
    133.                 components={{
    134.                         body: {
    135.                                 wrapper: DraggableContainer,
    136.                                 row: DraggableBodyRow,
    137.                         },
    138.                        }}
    139.                 />
    140.         </DrawerForm>
    141.         );
    142.         }),
    143. );
    144. export default SortTrackDrawer;

     

    效果如图: 

  • 相关阅读:
    C语言——通讯录管理系统
    小功能丨代码修改物体在 Hierarchy 上顺序
    【Python基础】从数值范围创建数组
    MySQL数据库中text类型的数据使用Mybatis自动生成之后,返回值为空
    Linux指定网卡名称
    CAS 自旋锁(一)
    【数据结构】list.h 常用函数实现详解
    设计模式——桥接模式
    【Java基础】面向对象基础
    APP瀑布流分页问题——领取,使用等操作即消失问题
  • 原文地址:https://blog.csdn.net/qq_40063198/article/details/134207583