Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Last active October 3, 2022 18:52
Show Gist options
  • Select an option

  • Save FOLLGAD/2ed25f8b3fd0c85e82c978a987f1cc4f to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/2ed25f8b3fd0c85e82c978a987f1cc4f to your computer and use it in GitHub Desktop.
Ant Design (antd) re-orderable menu using react-beautiful-dnd
import {
DragDropContext,
Draggable,
Droppable,
} from "react-beautiful-dnd";
import React, { useCallback, useMemo } from "react";
import { Menu } from "antd";
const renderMenuFn =
(item, props) =>
props.draggable ? (
<Draggable key={props.id} draggableId={props.id} index={props.index}>
{(provided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
{item}
</div>
)}
</Draggable>
) : (
item
);
const ReorderableMenu = () => {
const menuItems = useMemo(() => [
{
id: "id1",
index: 0,
draggable: true,
},
{
id: "id2",
index: 1,
draggable: false, // disabled
},
{
id: "id3",
index: 2,
draggable: true,
},
], [])
const handleDragEnd = useCallback((result) => {
// ...
}, [])
return (
<DragDropContext onDragEnd={handleDragEnd}>
<Droppable droppableId="reorderable-menu">
{(provided) => (
<div ref={provided.innerRef} {...provided.droppableProps}>
<Menu
items={menuItems}
// override internal render fns
_internalRenderMenuItem={renderMenuFn}
_internalRenderSubMenuItem={renderMenuFn}
/>
</div>
)}
</Droppable>
)
}
</DragDropContext>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment