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实现导出excel文件的操作步骤

    React实现导出excel文件的操作步骤

    在React项目的TypeScript文件中,因为原生的JavaScript或TypeScript并没有提供直接的Excel导出功能,常用的Excel导出方法通常涉及使用第三方库,本文介绍了React实现导出excel文件的操作步骤,需要的朋友可以参考下
    2024-12-12
  • 30分钟精通React今年最劲爆的新特性——React Hooks

    30分钟精通React今年最劲爆的新特性——React Hooks

    这篇文章主要介绍了30分钟精通React今年最劲爆的新特性——React Hooks,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • useEffect 返回函数执行过程源码解析

    useEffect 返回函数执行过程源码解析

    这篇文章主要为大家介绍了useEffect 返回函数执行过程源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • React事件处理的机制及原理

    React事件处理的机制及原理

    这篇文章主要介绍了React事件处理的机制及原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • React实现一个高度自适应的虚拟列表

    React实现一个高度自适应的虚拟列表

    这篇文章主要介绍了React如何实现一个高度自适应的虚拟列表,帮助大家更好的理解和学习使用React,感兴趣的朋友可以了解下
    2021-04-04
  • React-router中结合webpack实现按需加载实例

    React-router中结合webpack实现按需加载实例

    本篇文章主要介绍了React-router中结合webpack实现按需加载实例,非常具有实用价值,需要的朋友可以参考下
    2017-05-05
  • React 中的列表渲染要加 key的原因分析

    React 中的列表渲染要加 key的原因分析

    这篇文章主要介绍了React 中的列表渲染为什么要加 key,在 React 中我们经常需要渲染列表,比如展示好友列表,文中给大家介绍了列表渲染不提供 key 会如何,通过实例代码给大家介绍的非常详细,需要的朋友一起看看吧
    2022-07-07
  • React Electron生成桌面应用过程

    React Electron生成桌面应用过程

    这篇文章主要介绍了React+Electron快速创建并打包成桌面应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12
  • windows下create-react-app 升级至3.3.1版本踩坑记

    windows下create-react-app 升级至3.3.1版本踩坑记

    这篇文章主要介绍了windows下create-react-app 升级至3.3.1版本踩坑记,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • React useContext与useReducer函数组件使用

    React useContext与useReducer函数组件使用

    useContext和useReducer 可以用来减少层级使用, useContext,可以理解为供货商提供一个公共的共享值,然后下面的消费者去接受共享值,只有一个供货商,而有多个消费者,可以达到共享的状态改变的目的
    2023-02-02

最新评论