无法拖动React Bootstrap选项卡-React Beautiful dnd

时间:2020-10-12 17:39:25

标签: reactjs drag-and-drop react-bootstrap react-beautiful-dnd

我想从react-bootstrap拖放标签,并且我正在使用react-beautiful-dnd实现它。但是由于某种原因,我无法获得它。没有标签显示。

这是我的代码:

import React from "react";
import { Tabs, Tab } from "react-bootstrap";
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";

export default function App() {
  const getItems = count =>
    Array.from({ length: count }, (v, k) => k).map(k => ({
      id: `item-${k}`,
      content: `item ${k}`
    }));
  const [selectedTab, setSelectedTab] = React.useState("item-0");
  const [items, setItems] = React.useState(getItems(6));

  const reorder = (list, startIndex, endIndex) => {
    const result = Array.from(list);
    const [removed] = result.splice(startIndex, 1);
    result.splice(endIndex, 0, removed);

    return result;
  };

  const onDragEnd = result => {
    // dropped outside the list
    if (!result.destination) {
      return;
    }

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

    setItems(items);
  };

  return (
    <div className="App">
      <DragDropContext onDragEnd={onDragEnd}>
        <Droppable droppableId="tab-drop" direction="horizontal">
          {droppable => (
            <React.Fragment>
              <Tabs
                ref={droppable.innerRef}
                activeKey={selectedTab}
                onSelect={chartId => {
                  if (chartId === this.state.selectedTab) {
                    return;
                  }
                  setSelectedTab(chartId);
                }}
                {...droppable.droppableProps}
              >
                {items.map((item, index) => (
                  <Draggable
                    key={`tab-${item.id}-order-${index}`}
                    draggableId={`${item.id}-order-${index}`}
                    index={index}
                  >
                    {(draggable, snapshot) => (
                      <Tab
                        ref={draggable.innerRef}
                        eventKey={item.id}
                        title={<div>{item.content}</div>}
                        {...draggable.draggableProps}
                        {...draggable.dragHandleProps}
                        style={getItemStyle(
                          draggable.draggableProps.style,
                          snapshot.isDragging
                        )}
                      />
                    )}
                  </Draggable>
                ))}
                <Tab title={<button>+</button>} />
                <Tab
                  tabClassName="ml-auto active tabBottomBorder"
                  title={<button>Format</button>}
                />
                <Tab
                  tabClassName="active tabBottomBorder"
                  title={<button>Edit</button>}
                />
              </Tabs>
              {droppable.placeholder}
            </React.Fragment>
          )}
        </Droppable>
      </DragDropContext>
    </div>
  );
}

这是不使用DND的示例:

import React from "react";
import "./styles.css";
import { Tabs, Tab } from "react-bootstrap";

export default function App() {
  const getItems = (count) =>
    Array.from({ length: count }, (v, k) => k).map((k) => ({
      id: `item-${k}`,
      content: `item ${k}`
    }));
  const items = getItems(6);
  const [selectedTab, setSelectedTab] = React.useState("item-0");

  return (
    <div className="App">
      <Tabs
        activeKey={selectedTab}
        onSelect={(chartId) => {
          if (chartId === selectedTab) {
            return;
          }
          setSelectedTab(chartId);
        }}
      >
        {items.map((item, index) => (
          <Tab eventKey={item.id} title={<div>{item.content}</div>} />
        ))}
        <Tab title={<button>+</button>} />
        <Tab
          tabClassName="ml-auto active tabBottomBorder"
          title={<button>Format</button>}
        />
        <Tab
          tabClassName="active tabBottomBorder"
          title={<button>Edit</button>}
        />
      </Tabs>
    </div>
  );
}

与DND链接:https://stackblitz.com/edit/react-u1cts7?file=src%2FApp.js

没有DND的链接:https://codesandbox.io/s/pedantic-minsky-7zelb?file=/src/App.js:0-1045

我想实现与https://codesandbox.io/s/mmrp44okvj?file=/index.js:0-2939

类似的功能

请注意,只能拖动“ +”按钮之前的选项卡。 +按钮标签和最后两个标签都不应该。

请提供解决此问题的建议。任何帮助表示赞赏。

0 个答案:

没有答案