基于react的拖拽组件库react-beautiful-dnd 与功能组件一起使用

在这里插入图片描述

import React, { useState } from "react";
import ReactDOM from "react-dom";
import styled from "@emotion/styled";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
import type { Quote as QuoteType } from "../types";

// 生成一个数组
const initial = Array.from({ length: 10 }, (v, k) => k).map(k => {
  const custom: Quote = {
    id: `id-${k}`,
    content: `Quote ${k}`
  };

  return custom;
});

const grid = 8;

// 这个是控制拖拽的(第一个参数是一个数组,第二个参数是没移动前的位子,第三个参数是移动后的位子)
const reorder = (list, startIndex, endIndex) => {
  const result = Array.from(list);
  const [removed] = result.splice(startIndex, 1);
  result.splice(endIndex, 0, removed);
  return result;
};

// 这是是控制 这个组件颜色的
const QuoteItem = styled.div`
  width: 400px;
  border: 1px solid grey;
  margin-bottom: ${grid}px;
  background-color: lightblue;
  padding: ${grid}px;
`;

function Quote({ quote, index }) {
  return (
    <Draggable draggableId={quote.id} index={index}>
      {provided => (
        <QuoteItem
          ref={provided.innerRef}
          {...provided.draggableProps}
          {...provided.dragHandleProps}
        >
          {quote.content}
        </QuoteItem>
      )}
    </Draggable>
  );
}

//这个是组件
const QuoteList = React.memo(function QuoteList({ quotes }) {
  return quotes.map((quote: QuoteType, index: number) => (
    <Quote quote={quote} index={index} key={quote.id} />
  ));
});

function QuoteApp() {
  const [state, setState] = useState({ quotes: initial });

  function onDragEnd(result) {
    if (!result.destination) {
      return;
    }

    if (result.destination.index === result.source.index) {
      return;
    }

    const quotes = reorder(
      state.quotes,
      result.source.index,
      result.destination.index
    );

    setState({ quotes });
  }

  return (
    <DragDropContext onDragEnd={onDragEnd}>
      <Droppable droppableId="list">
        {provided => (
          <div ref={provided.innerRef}
               {...provided.droppableProps}>
            <QuoteList quotes={state.quotes} />
            {/*{provided.placeholder}*/}
          </div>
        )}
      </Droppable>
    </DragDropContext>
  );
}
export default  QuoteApp


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
react-beautiful-dnd是一个用于实现拖放功能React。它提供了一个优雅的方式来实现复杂的拖放交互,并且在性能方面也表现出色。 官方示例展示了如何在React使用react-beautiful-dnd来创建一个基本的拖放列表。该示例使用两个列表,左侧列表中有一些颜色方块,右侧列表为空。 首先,我们需要引入所需的组件。然后,在页面上创建两个容器,一个用于左侧列表,一个用于右侧列表。接下来,我们需要定义一个状态来存储列表项的数据。在示例中,数据是一个包含颜色方块的数组。我们还需要定义一个函数来处理列表项的拖动和放置操作。 然后,我们可以使用react-beautiful-dnd提供的DragDropContext组件来包裹整个应用,并将其附加到页面的根元素上。这样可以确保拖放操作在整个应用中生效。 接下来,我们需要在左侧列表容器中渲染颜色方块列表,并使用Draggable组件对每个方块进行包装。我们还需要在Draggable组件上设置一个唯一的ID,并使用DragHandleProps和DraggableProps来传递拖动和放置操作的属性。 最后,我们需要在右侧列表容器中渲染一个Droppable组件,以表示可以将颜色方块拖放到其中。并且,我们需要在Droppable组件使用DroppableProps来传递拖放操作的属性。 通过以上步骤,我们就可以创建一个简单的拖放列表。当我们拖动一个颜色方块时,react-beautiful-dnd会自动处理其它方块的重新排序和位置更新。同时,我们还可以添加一些自定义的逻辑来实现更复杂的拖放交互效果。 总之,react-beautiful-dnd官方示例展示了如何使用这个来创建一个基本的拖放列表。通过这个示例,我们可以学习到如何在React使用react-beautiful-dnd,并了解其强大的拖放功能和灵活的配置选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术的大仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值