Y轴上的拖拽排序

1、安装依赖

npm install react-movable antd

2、使用Demo

<DragDropSort 
	items={[{id: 1, name: 'hh', age: 11}, {id: 2, name: 'aa', age: 26}]} 
	onChange={(value) => {
		console.log('拖拽后的数据:', value)
	}}
	render={(row) => {
		return <div>测试数据id:{row.id}</div>
	}}
	style={{ width: 300 }}
/>

3、拖拽排序组件代码

import React, { useState } from 'react';
import { List, arrayMove } from 'react-movable';
import { Card } from 'antd';

// Demo
const initialImages = [{ id: '10' }, { id: '20' }, { id: '30' }, { id: '40' }, { id: '50' }, { id: '60' }];

type IItemsType = {
  id: string | number;
  [key: string]: any;
};
interface IProps {
  /** 父盒子样式,如宽度高度 */
  style?: React.CSSProperties;
  items: IItemsType[];
  onChange: (newItems: IItemsType[]) => void;
  render?: (row: IItemsType) => React.ReactNode | string | number;
  /** Y轴滚动条 */
  scroll?: { y?: number };
}

const DragDropSort = (props: IProps) => {
  const { items = initialImages, onChange, render, style = {}, scroll = {} } = props;
  const [dragDropCard, setDragDropCard] = useState(items);

  const scrollStyle: React.CSSProperties = {
    ...style,
    display: 'flex',
    flexDirection: 'column',
    height: scroll?.y || 'auto',
    ...(scroll?.y ? { overflowY: 'auto' } : {}),
  };

  return (
    <List
      values={dragDropCard}
      onChange={({ oldIndex, newIndex }) => {
        const data = arrayMove(dragDropCard, oldIndex, newIndex);
        setDragDropCard(data);
        onChange && onChange(data);
      }}
      renderList={({ children, props }) => (
        <div {...props} style={{ ...scrollStyle, ...(props as any).style }}>
          {children}
        </div>
      )}
      renderItem={({ value, props }) => (
        <div {...props}>
          <Card hoverable>{render ? render(value) : <div>我是测试卡片内容{value.id}</div>}</Card>
        </div>
      )}
    />
  );
};

export default DragDropSort;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值