最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

1.安裝

​ 在已有react項目中 執(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

實現(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;
 
// 設置樣式
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應用的相同元素.
                {...provided.droppableProps}
                // 為了使 droppable 能夠正常工作必須 綁定到最高可能的DOM節(jié)點中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

實現(xiàn)代碼: 其實和縱向拖拽差不多 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);
  //將原來的元素添加進數(shù)組
  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 `,
 
  // 拖拽的時候背景變化
  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實現(xiàn)一個代辦事項的拖拽(縱向 橫向拖拽)

demo3.gif

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

4.感受

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

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

相關(guān)文章

  • React 實現(xiàn)拖拽功能的示例代碼

    React 實現(xiàn)拖拽功能的示例代碼

    這篇文章主要介紹了React 實現(xiàn)拖拽功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • react+react-beautiful-dnd實現(xiàn)代辦事項思路詳解

    react+react-beautiful-dnd實現(xiàn)代辦事項思路詳解

    這篇文章主要介紹了react+react-beautiful-dnd實現(xiàn)代辦事項,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • React如何接收excel文件下載導出功能封裝

    React如何接收excel文件下載導出功能封裝

    這篇文章主要介紹了React如何接收excel文件下載導出功能封裝,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • react項目優(yōu)化配置的操作詳解

    react項目優(yōu)化配置的操作詳解

    這篇文章主要介紹了react項目優(yōu)化配置,主要包括優(yōu)化配置CDN,優(yōu)化路由懶加載,結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 官方推薦react-navigation的具體使用詳解

    官方推薦react-navigation的具體使用詳解

    本篇文章主要介紹了官方推薦react-navigation的具體使用詳解,react-navigation是致力于解決導航卡頓,數(shù)據(jù)傳遞,Tabbar和navigator布局,支持redux。非常具有實用價值,需要的朋友可以參考下
    2018-05-05
  • 使用react和redux構(gòu)建一個簡單的計數(shù)器

    使用react和redux構(gòu)建一個簡單的計數(shù)器

    這篇文章主要為大家詳細介紹了如何使用react和redux構(gòu)建一個簡單的計數(shù)器,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • React文件名和目錄規(guī)范最佳實踐記錄(總結(jié)篇)

    React文件名和目錄規(guī)范最佳實踐記錄(總結(jié)篇)

    React在使用時非常靈活,如果沒有一個規(guī)范約束項目,在開發(fā)過程中會非?;靵y,本文將介紹幾個優(yōu)秀的規(guī)范,介紹文件名和目錄前,需要先簡述一下幾種通用的類型,用來區(qū)分文件的功能,感興趣的朋友一起看看吧
    2022-05-05
  • 在react中使用windicss的問題

    在react中使用windicss的問題

    這篇文章主要介紹了在react中使用windicss的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • styled-components?性能詳解

    styled-components?性能詳解

    這篇文章主要為大家介紹了styled-components?的性能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 一文教會你用redux實現(xiàn)computed計算屬性

    一文教會你用redux實現(xiàn)computed計算屬性

    在computed中,可以定義一些屬性,即計算屬性,下面這篇文章主要給大家介紹了關(guān)于如何利用redux實現(xiàn)computed計算屬性的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05

最新評論

襄汾县| 泰来县| 阿坝县| 铁岭市| 边坝县| 雅江县| 秭归县| 珲春市| 综艺| 吉安县| 拜泉县| 商洛市| 舞钢市| 鱼台县| 吐鲁番市| 灵山县| 那曲县| 克什克腾旗| 灵寿县| 南皮县| 崇义县| 五大连池市| 平邑县| 衡阳市| 井研县| 东兰县| 三都| 理塘县| 马山县| 卢龙县| 岑溪市| 青浦区| 屏东县| 封丘县| 洪洞县| 武宣县| 大洼县| 南溪县| 卢湾区| 自贡市| 锡林郭勒盟|