内容简介:Reorder:在拖拉的時候,Droppable, Draggable 元件除了
react-beautiful-dnd 有 3 個主要元件:
-
DragDropContext - 建立一個可 DnD 的範圍。
- onDragStart
- onDragUpdate
- onDragEnd
- Droppable - 建立可以被拖曳放入的區塊。
- Draggalbe - 可被拖拉元件
- 安裝
$ npm i react-beautiful-dnd
-
於希望 DnD 的位置使用 DragDropContext 包起來,且
onDragEnd必須設定:
import { DragDropContext } from 'react-beautiful-dnd';
<DragDropContext
onDragEnd={() => {}}
>
{/* Your target */}
</DragDropContext>
-
使用 Droppable 配置可被拖入的區塊
-
Droppable 必須設定
droppableId -
Droppable 使用 render props pattern
意味著內部須使用一個 function。這個 function 使用
provided將所需的參數代置 DOM -
provided是一個物件provided.droppableProps ref provided.placeholder
-
Droppable 必須設定
impoort { Droppable } from 'react-beautiful-dnd';
<Droppable droppableId="id">
{
provided => (
<List
ref={provided.innerRef}
{...provided.droppableProps}
>
{provided.placeholder}
</List>
)
}
</Droppable>
-
將可被拖移的元件使用
Draggable包起來:-
draggableId和index必須。 -
與
Droppable一樣,children 為 render props pattern -
內部項目
ref須設定,provided.draggableProps和provided.dragHandleProps須套用至元件
-
<Draggable
draggableId={t.id}
index={i}
>
{p => (
<Item
ref={p.innerRef}
{...p.draggableProps}
{...p.dragHandleProps}
key={t.id}
>
{t.text}
</Item>
)}
</Draggable>
-
onDragEnd變更排序
// onDragEnd(result) result 資訊
const result = {
draggableId: 1,
type: 'TYPE',
reson: 'DROP',
source: {
droppableId: 1,
index: 0,
},
destination: {
droppableId: 1,
index: 1,
}
}
Reorder:
onDragEnd={result => {
const { source, destination, draggableId } = result;
if (!destination) {
return;
}
let arr = Array.from(this.state.todos);
const [remove] = arr.splice(source.index, 1);
arr.splice(destination.index, 0, remove);
this.setState({
todos: arr,
});
}}
在拖拉的時候,Droppable, Draggable 元件除了 provided
之外還提供了 snapshot
下面是 snapshot
提供的資訊:
const draggableSnapshot = {
isDragging: true,
draggingOver: 'droppable-id'
}
const droppableSnapshot = {
isDraggingOver: true,
draggingOverWith: 'draggable-id'
}
回到 DragDropContext 除了 onDragEnd
之外還有 onDragStart
和 onDragUpdate
兩個 events
。
這兩個事件可以協助我們加入一些樣式:
// onDragStart
const start = {
draggableId: 'draggalbe-id',
type: 'TYPE',
source: {
droppableId: 'droppable-id',
index: 0,
}
}
// onDragUpdate
const update = {
...start,
destination: {
droppableId: 'droppable-id',
index: 1,
}
}
- drag handle 是 Draggable 的一部分用來處理可拖拉的部分(可拖拉整個元件或局部)。
-
要停止拖拉可以設定
isDragDisabled -
Droppable 可以限制可被拖入的元件:
type isDropDisabled
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Algorithms, 3rd Edition
Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest、Clifford Stein / The MIT Press / 2009-7-31 / USD 94.00
Some books on algorithms are rigorous but incomplete; others cover masses of material but lack rigor. Introduction to Algorithms uniquely combines rigor and comprehensiveness. The book covers a broad ......一起来看看 《Introduction to Algorithms, 3rd Edition》 这本书的介绍吧!