react-beautiful-dnd拖拽排序功能的实现过程

 更新时间:2024年07月08日 09:20:59   作者:qing_小诺  
这篇文章主要介绍了react-beautiful-dnd拖拽排序功能的实现过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

如果 react 项目中需要用到拖拽功能,可以使用 react-beautiful-dnd 插件。

1、react-beautiful-dnd插件

github官网链接:https://github.com/atlassian/react-beautiful-dnd/blob/master/docs/about/examples.md

打开后显示下图:

2、查看所有范例

点上图中的“All the examples!”,可以查看所有范例(链接:https://react-beautiful-dnd.netlify.app/

如下图:

3、代码示例

点击上图中的“Simple horizontal list”

可以看到代码示例:

index.js代码如下:稍加改造就能直接用到项目中啦~~~

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
 
// fake data generator
const getItems = count =>
  Array.from({ length: count }, (v, k) => k).map(k => ({
    id: `item-${k}`,
    content: `item ${k}`,
  }));
 
// a little function to help us with reordering the result
const reorder = (list, startIndex, endIndex) => {
  const result = Array.from(list);
  const [removed] = result.splice(startIndex, 1);
  result.splice(endIndex, 0, removed);
 
  return result;
};
 
const grid = 8;
 
const getItemStyle = (isDragging, draggableStyle) => ({
  // some basic styles to make the items look a bit nicer
  userSelect: 'none',
  padding: grid * 2,
  margin: `0 ${grid}px 0 0`,
 
  // change background colour if dragging
  background: isDragging ? 'lightgreen' : 'grey',
 
  // styles we need to apply on draggables
  ...draggableStyle,
});
 
const getListStyle = isDraggingOver => ({
  background: isDraggingOver ? 'lightblue' : 'lightgrey',
  display: 'flex',
  padding: grid,
  overflow: 'auto',
});
 
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: getItems(6),
    };
    this.onDragEnd = this.onDragEnd.bind(this);
  }
 
  onDragEnd(result) {
    // dropped outside the list
    if (!result.destination) {
      return;
    }
 
    const items = reorder(
      this.state.items,
      result.source.index,
      result.destination.index
    );
 
    this.setState({
      items,
    });
  }
 
  // Normally you would want to split things out into separate components.
  // But in this example everything is just done in one place for simplicity
  render() {
    return (
      <DragDropContext onDragEnd={this.onDragEnd}>
        <Droppable droppableId="droppable" direction="horizontal">
          {(provided, snapshot) => (
            <div
              ref={provided.innerRef}
              style={getListStyle(snapshot.isDraggingOver)}
              {...provided.droppableProps}
            >
              {this.state.items.map((item, index) => (
                <Draggable key={item.id} draggableId={item.id} index={index}>
                  {(provided, snapshot) => (
                    <div
                      ref={provided.innerRef}
                      {...provided.draggableProps}
                      {...provided.dragHandleProps}
                      style={getItemStyle(
                        snapshot.isDragging,
                        provided.draggableProps.style
                      )}
                    >
                      {item.content}
                    </div>
                  )}
                </Draggable>
              ))}
              {provided.placeholder}
            </div>
          )}
        </Droppable>
      </DragDropContext>
    );
  }
}
 
// Put the thing into the DOM!
ReactDOM.render(<App />, document.getElementById('root'));

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 代码解析React中setState同步和异步问题

    代码解析React中setState同步和异步问题

    前端框架从MVC过渡到MVVM。从DOM操作到数据驱动,一直在不断的进步着,本文给大家介绍React中setState同步和异步问题,感兴趣的朋友一起看看吧
    2021-06-06
  • React服务端渲染和同构的实现

    React服务端渲染和同构的实现

    本文主要介绍了React服务端渲染和同构的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • 解决React报错`value` prop on `input` should not be null

    解决React报错`value` prop on `input` should&

    这篇文章主要为大家介绍了React报错`value` prop on `input` should not be null解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • 从零开始搭建webpack+react开发环境的详细步骤

    从零开始搭建webpack+react开发环境的详细步骤

    这篇文章主要介绍了从零开始搭建webpack+react开发环境的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • React class和function的区别小结

    React class和function的区别小结

    Class组件和Function组件是React中创建组件的两种主要方式,本文主要介绍了React class和function的区别小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • React内存泄漏的常见原因及避免策略

    React内存泄漏的常见原因及避免策略

    内存泄漏是指程序中分配的内存未能正确释放,导致内存占用不断增加,最终可能影响应用性能甚至崩溃,在React中,内存泄漏常发生于组件卸载后,本文将详细介绍内存泄漏在React中的常见原因及避免策略,需要的朋友可以参考下
    2025-03-03
  • 浅谈react受控组件与非受控组件(小结)

    浅谈react受控组件与非受控组件(小结)

    本篇文章主要介绍了浅谈react受控组件与非受控组件(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • react基于Ant Desgin Upload实现导入导出

    react基于Ant Desgin Upload实现导入导出

    本文主要介绍了react基于Ant Desgin Upload实现导入导出,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01
  • React实现Step组件的示例代码

    React实现Step组件的示例代码

    这篇文章主要为大家详细介绍了React实现Step组件(步骤条组件)的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • react-json-editor-ajrm解析错误与解决方案

    react-json-editor-ajrm解析错误与解决方案

    由于历史原因,项目中 JSON 编辑器使用的是 react-json-editor-ajrm,近期遇到一个严重的展示错误,传入编辑器的数据与展示的不一致,这是产品和用户不可接受的,本文给大家介绍了react-json-editor-ajrm解析错误与解决方案,需要的朋友可以参考下
    2024-06-06

最新评论