我想从 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>
);
}
与免打扰链接:https ://stackblitz.com/edit/react-u1cts7?file=src%2FApp.js
没有免打扰的链接:https ://codesandbox.io/s/pedantic-minsky-7zelb?file=/src/App.js:0-1045
我想实现类似的功能:https ://codesandbox.io/s/mmrp44okvj?file=/index.js:0-2939
请注意,只有“+”按钮之前的选项卡应该是可拖动的。+ 按钮选项卡和最后两个选项卡不应该。
请就解决此问题的方法提出建议。任何帮助表示赞赏。