react实现记录拖动排序

 更新时间:2023年07月11日 08:46:32   作者:yuyuyu37  
这篇文章主要介绍了react实现记录拖动排序的相关资料,需要的朋友可以参考下

最近项目中要做一个拖动排序功能,首先想到的是之前项目中用过的antd自带的tree和table的拖动排序,但是只能在对应的组建里使用。这里用的是自定义组件,随意拖动排序,所以记录一下实现流程

  • react-dnd antd组件的拖动排序都是用的这个库,使用比较灵活,但是要配置的东西比较多,需求复杂时使用这个库;
    class BodyRow extends React.Component {
      render() {
        const { isOver, connectDragSource, connectDropTarget, moveRow, ...restProps } = this.props;
        const style = { ...restProps.style, cursor: 'move' };
    
        let { className } = restProps;
        if (isOver) {
          if (restProps.index > dragingIndex) {
            className += ' drop-over-downward';
          }
          if (restProps.index < dragingIndex) {
            className += ' drop-over-upward';
          }
        }
        return connectDragSource(
          connectDropTarget(<tr {...restProps} className={className} style={style} />),
        );
      }
    }
    const rowSource = {
      beginDrag(props) {
        dragingIndex = props.index;
        return {
          index: props.index,
        };
      },
    };
    const rowTarget = {
      drop(props, monitor) {
        const dragIndex = monitor.getItem().index;
        const hoverIndex = props.index;
        // Don't replace items with themselves
        if (dragIndex === hoverIndex) {
          return;
        }
        // Time to actually perform the action
        props.moveRow(dragIndex, hoverIndex);
        // Note: we're mutating the monitor item here!
        // Generally it's better to avoid mutations,
        // but it's good here for the sake of performance
        // to avoid expensive index searches.
        monitor.getItem().index = hoverIndex;
      },
    };
    const DragableBodyRow = DropTarget('row', rowTarget, (connect, monitor) => ({
      connectDropTarget: connect.dropTarget(),
      isOver: monitor.isOver(),
    }))(
      DragSource('row', rowSource, connect => ({
        connectDragSource: connect.dragSource(),
      }))(BodyRow),
    );
    
    <DndProvider backend={HTML5Backend}>
      <Table
        columns={columns}
        dataSource={this.state.data}
        components={this.components}
        onRow={(record, index) => ({
          index,
          moveRow: this.moveRow,
        })}
      />
    </DndProvider>
    
  • react-beautiful-dnd 在项目中看到引用了这个库,使用起来也不算复杂,就试着用了这个库,不过只支持水平或垂直拖动,一行或者一列元素时可以使用,可惜这个需求时两行多列元素,也没办法用;
    <DragDropContext onDragEnd={this.onDragEnd}>
      <Droppable droppableId='phone-droppable'>
        {droppableProvided => (
          <div ref={droppableProvided.innerRef} {...droppableProvided.droppableProps}>
            {this.state.phoneImages.map((image, index) => (
              <Draggable key={image||'upload'} draggableId={image||'upload'} index={index}>
                {(provided, snapshot) => (
                  <div
                    ref={provided.innerRef}
                    {...provided.draggableProps}
                    {...provided.dragHandleProps}
                    style={{
                      ...provided.draggableProps.style,
                      opacity: snapshot.isDragging ? 0.8 : 1,
                      display: 'inline-block'
                    }}
                  >
                    <img src={img}/>
                  </div>
                )}
              </Draggable>
            ))}
            {droppableProvided.placeholder}
          </div>
        )}
      </Droppable>
    </DragDropContext>
    
  • react-sortable-hoc 最后在网上搜索的时候,又看到这个库,使用起来比较简单,使用SortableList包裹要拖拽元素的容器,SortableElement包裹要拖拽的子元素,设置容器拖拽方向axis={'xy'}即可使grid布局的多个元素支持水平和竖直方向拖动排序;
    const SortableItem = SortableElement(({children}) => (
      <div>{children}</div>
    ));
    const SortableList = SortableContainer(({children}) => {
      return (
        <div style={{display: 'grid', gridTemplateRows: '117px 117px', gridTemplateColumns: '120px 120px 120px 120px'}}>
          {children}
        </div>
      );
    });
    
    <SortableList onSortEnd={this.onPadSortEnd} helperClass={Styles.sortableHelper} axis={'xy'}>
      {this.state.padImages.map((image, index) => (
        <SortableItem key={`pad-item-${index}`} index={index} className={Styles.sortableItem}>
          <img src={img}/>
        </SortableItem>
      ))}
    </SortableList>
    

好久没更新博客了,最近工作比较忙,差不多每天都要加班,中间有经历搬家,没时间坐下来总结学到的东西。工作的时候因为这块花费了一些时间,想到可能别人也会遇到类似问题,所以就记录下来了

到此这篇关于react实现记录拖动排序的文章就介绍到这了,更多相关react记录拖动排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 关于React状态管理的三个规则总结

    关于React状态管理的三个规则总结

    随着 JavaScript 单页应用开发日趋复杂,JavaScript 需要管理比任何时候都要多的 state (状态),这篇文章主要给大家介绍了关于React状态管理的三个规则,需要的朋友可以参考下
    2021-07-07
  • React 中的重新渲染类组件及函数组件

    React 中的重新渲染类组件及函数组件

    这篇文章主要为大家介绍了React 中的重新渲染类组件及函数组件使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • 在 React 项目中优雅实现新用户引导HagiCode 的 driver.js 实践指南

    在 React 项目中优雅实现新用户引导HagiCode 的 driver.js

    本文介绍了在HagiCode项目中使用driver.js实现新用户引导的经验,通过轻量级、简洁API和灵活配置,配合状态管理、目标元素定位和动态导入等技术手段,实现精准的用户引导,旨在帮助新用户快速理解产品工作流,并提高用户体验,感兴趣的朋友跟随小编一起看看吧
    2026-04-04
  • React中使用antd组件的方法

    React中使用antd组件的方法

    antd 是基于 Ant Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品,这篇文章主要介绍了React中使用antd组件,需要的朋友可以参考下
    2022-09-09
  • React使用TypeScript的最佳实践和技巧

    React使用TypeScript的最佳实践和技巧

    在React项目中使用TypeScript可以显著提高代码的可维护性和可读性,并提供强大的类型检查功能,减少运行时错误,以下是一些优雅地将TypeScript集成到React项目中的最佳实践和技巧,需要的朋友可以参考下
    2024-06-06
  • react实现浏览器自动刷新的示例代码

    react实现浏览器自动刷新的示例代码

    这篇文章主要介绍了react实现浏览器自动刷新的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • React项目中hook实现展示对话框功能

    React项目中hook实现展示对话框功能

    Modal(模态框)是 web 开发中十分常见的组件,即从页面中弹出的对话框,下面这篇文章主要给大家介绍了关于React项目中hook实现展示对话框功能的相关资料,需要的朋友可以参考下
    2022-05-05
  • react-router实现跳转传值的方法示例

    react-router实现跳转传值的方法示例

    这篇文章主要给大家介绍了关于react-router实现跳转传值的相关资料,文中给出了详细的示例代码,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-05-05
  • 如何不使用eject修改create-react-app的配置

    如何不使用eject修改create-react-app的配置

    许多刚开始接触create-react-app框架的同学,不免都会有个疑问:如何在不执行eject操作的同时,修改create-react-app的配置。
    2021-04-04
  • 浅谈对于react-thunk中间件的简单理解

    浅谈对于react-thunk中间件的简单理解

    这篇文章主要介绍了浅谈对于react-thunk中间件的简单理解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05

最新评论