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

react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能

 更新時間:2021年08月09日 16:12:04   作者:當(dāng)當(dāng)當(dāng)  
這篇文章主要介紹了react-beautiful-dnd 實(shí)現(xiàn)組件拖拽功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一個React.js 的 漂亮,可移植性 列表拖拽庫。想了解更多react-beautiful-dnd特點(diǎn)適用人群請看官方文檔、中文翻譯文檔

npm:https://www.npmjs.com/package/react-beautiful-dnd

1.安裝

​ 在已有react項(xiàng)目中 執(zhí)行以下命令 so easy。

# yarn
yarn add react-beautiful-dnd
 
# npm
npm install react-beautiful-dnd --save

2.APi

詳情查看 官方文檔。

3.react-beautiful-dnd demo

3.1 demo1 縱向組件拖拽

效果下圖:

demo1.gif

實(shí)現(xiàn)代碼:

import React, { Component } from "react";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
 
//初始化數(shù)據(jù)
const getItems = count =>
  Array.from({ length: count }, (v, k) => k).map(k => ({
    id: `item-${k + 1}`,
    content: `this is content ${k + 1}`
  }));
 
// 重新記錄數(shù)組順序
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;
 
// 設(shè)置樣式
const getItemStyle = (isDragging, draggableStyle) => ({
  // some basic styles to make the items look a bit nicer
  userSelect: "none",
  padding: grid * 2,
  margin: `0 0 ${grid}px 0`,
 
  // 拖拽的時候背景變化
  background: isDragging ? "lightgreen" : "#ffffff",
 
  // styles we need to apply on draggables
  ...draggableStyle
});
 
const getListStyle = () => ({
  background: 'black',
  padding: grid,
  width: 250
});
 
 
 
export default class ReactBeautifulDnd extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: getItems(11)
    };
    this.onDragEnd = this.onDragEnd.bind(this);
  }
 
  onDragEnd(result) {
    if (!result.destination) {
      return;
    }
 
    const items = reorder(
      this.state.items,
      result.source.index,
      result.destination.index
    );
 
    this.setState({
      items
    });
  }
 
 
  render() {
    return (
      <DragDropContext onDragEnd={this.onDragEnd}>
        <center>
          <Droppable droppableId="droppable">
            {(provided, snapshot) => (
              <div
              //provided.droppableProps應(yīng)用的相同元素.
                {...provided.droppableProps}
                // 為了使 droppable 能夠正常工作必須 綁定到最高可能的DOM節(jié)點(diǎn)中provided.innerRef.
                ref={provided.innerRef}
                style={getListStyle(snapshot)}
              >
                {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>
        </center>
      </DragDropContext>
    );
  }
}

3.2 demo2 水平拖拽

​ 效果下圖:

demo2.gif

實(shí)現(xiàn)代碼: 其實(shí)和縱向拖拽差不多 Droppable 中 多添加了一個排列順序的屬性,direction="horizontal"

import React, { Component } from "react";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
 
 
const getItems = count => (
  Array.from({ length: count }, (v, k) => k).map(k => ({
    id: `item-${k + 1}`,
    content: `this is content ${k + 1}`
  }))
)
 
// 重新記錄數(shù)組順序
const reorder = (list, startIndex, endIndex) => {
  const result = Array.from(list);
  //刪除并記錄 刪除元素
  const [removed] = result.splice(startIndex, 1);
  //將原來的元素添加進(jìn)數(shù)組
  result.splice(endIndex, 0, removed);
  return result;
};
 
const grid = 8;
 
 
// 設(shè)置樣式
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 `,
 
  // 拖拽的時候背景變化
  background: isDragging ? "lightgreen" : "#ffffff",
 
  // styles we need to apply on draggables
  ...draggableStyle
});
 
const getListStyle = () => ({
  background: 'black',
  display: 'flex',
  padding: grid,
  overflow: 'auto',
});
 
 
class ReactBeautifulDndHorizontal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: getItems(10)
    };
    this.onDragEnd = this.onDragEnd.bind(this);
  }
 
  onDragEnd(result) {
    if (!result.destination) {
      return;
    }
 
    const items = reorder(
      this.state.items,
      result.source.index,
      result.destination.index
    );
 
    this.setState({
      items
    });
  }
 
  render() {
    return (
      <DragDropContext onDragEnd={this.onDragEnd}>
        <Droppable droppableId="droppable" direction="horizontal">
          {(provided, snapshot) => (
            <div
              {...provided.droppableProps}
              ref={provided.innerRef}
              style={getListStyle(snapshot.isDraggingOver)}
            >
              {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>
    )
  }
}
 
export default ReactBeautifulDndHorizontal

3.3 demo3實(shí)現(xiàn)一個代辦事項(xiàng)的拖拽(縱向 橫向拖拽)

demo3.gif

實(shí)現(xiàn)原理其實(shí)大同小異 。代碼整理后放在github上。地址:github

4.感受

目前簡單的使用react - beautiful-dnd下來感覺 ,上手感覺挺簡單,api也不繁瑣。性能也不錯(demo2中做過渲染170多個task。拖拽起來還是如絲般順滑)。日后遇到啥不足會mark在一下。

到此這篇關(guān)于react-beautiful-dnd 實(shí)現(xiàn)組件拖拽的文章就介紹到這了,更多相關(guān)react-beautiful-dnd 組件拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 采用React編寫小程序的Remax框架的編譯流程解析(推薦)

    采用React編寫小程序的Remax框架的編譯流程解析(推薦)

    這篇文章主要介紹了采用React編寫小程序的Remax框架的編譯流程解析(推薦),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • react數(shù)據(jù)管理機(jī)制React.Context源碼解析

    react數(shù)據(jù)管理機(jī)制React.Context源碼解析

    這篇文章主要為大家介紹了react數(shù)據(jù)管理機(jī)制React.Context源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • React性能debug場景解決記錄

    React性能debug場景解決記錄

    這篇文章主要為大家介紹了React性能debug場景解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 探究react-native 源碼的圖片緩存問題

    探究react-native 源碼的圖片緩存問題

    本篇文章主要介紹了探究react-native 源碼的圖片緩存問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • React中嵌套組件與被嵌套組件的通信過程

    React中嵌套組件與被嵌套組件的通信過程

    這篇文章主要介紹了React中嵌套組件與被嵌套組件的通信過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • React狀態(tài)提升案例介紹

    React狀態(tài)提升案例介紹

    這篇文章主要介紹了React狀態(tài)提升案例,所謂 狀態(tài)提升 就是將各個子組件的 公共state 提升到它們的父組件進(jìn)行統(tǒng)一存儲、處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-04-04
  • React中使用Workbox進(jìn)行預(yù)緩存的實(shí)現(xiàn)代碼

    React中使用Workbox進(jìn)行預(yù)緩存的實(shí)現(xiàn)代碼

    Workbox是Google Chrome團(tuán)隊(duì)推出的一套 PWA 的解決方案,這套解決方案當(dāng)中包含了核心庫和構(gòu)建工具,因此我們可以利用Workbox實(shí)現(xiàn)Service Worker的快速開發(fā),本文小編給大家介紹了React中使用Workbox進(jìn)行預(yù)緩存的實(shí)現(xiàn),需要的朋友可以參考下
    2023-11-11
  • React通過redux-persist持久化數(shù)據(jù)存儲的方法示例

    React通過redux-persist持久化數(shù)據(jù)存儲的方法示例

    這篇文章主要介紹了React通過redux-persist持久化數(shù)據(jù)存儲的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • react native 獲取地理位置的方法示例

    react native 獲取地理位置的方法示例

    這篇文章主要介紹了react native 獲取地理位置的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • 自己動手封裝一個React Native多級聯(lián)動

    自己動手封裝一個React Native多級聯(lián)動

    這篇文章主要介紹了自己動手封裝一個React Native多級聯(lián)動,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09

最新評論