欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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?返回函數執(zhí)行過程源碼解析

    useEffect?返回函數執(zhí)行過程源碼解析

    這篇文章主要為大家介紹了useEffect?返回函數執(zhí)行過程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    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快速創(chuàng)建并打包成桌面應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    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,可以理解為供貨商提供一個公共的共享值,然后下面的消費者去接受共享值,只有一個供貨商,而有多個消費者,可以達到共享的狀態(tài)改變的目的
    2023-02-02

最新評論