Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When used inside a reactflow node, it will cause the node position to deviate #2652

Open
sauron0914 opened this issue Jun 30, 2024 · 0 comments

Comments

@sauron0914
Copy link

npm install react-flow-renderer react-beautiful-dnd
import React from 'react';
import ReactFlow, { Handle } from 'react-flow-renderer';
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';

const onDragEnd = (result, items, setItems) => {
  if (!result.destination) return;

  const reorderedItems = Array.from(items);
  const [removed] = reorderedItems.splice(result.source.index, 1);
  reorderedItems.splice(result.destination.index, 0, removed);

  setItems(reorderedItems);
};

const CustomNode = ({ data }) => {
  const [items, setItems] = React.useState(data.items);

  return (
    <div className="custom-node">
      <Handle type="target" position="top" />
      <DragDropContext onDragEnd={(result) => onDragEnd(result, items, setItems)}>
        <Droppable droppableId="droppable">
          {(provided) => (
            <div {...provided.droppableProps} ref={provided.innerRef}>
              {items.map((item, index) => (
                <Draggable key={item.id} draggableId={item.id} index={index}>
                  {(provided) => (
                    <div
                      ref={provided.innerRef}
                      {...provided.draggableProps}
                      {...provided.dragHandleProps}
                      className="draggable-item"
                    >
                      {item.content}
                    </div>
                  )}
                </Draggable>
              ))}
              {provided.placeholder}
            </div>
          )}
        </Droppable>
      </DragDropContext>
      <Handle type="source" position="bottom" />
    </div>
  );
};

const initialElements = [
  {
    id: '1',
    type: 'customNode',
    position: { x: 100, y: 100 },
    data: {
      items: [
        { id: 'item-1', content: 'Item 1' },
        { id: 'item-2', content: 'Item 2' },
        { id: 'item-3', content: 'Item 3' },
      ],
    },
  },
];

const App = () => {
  return (
    <ReactFlow
      elements={initialElements}
      nodeTypes={{ customNode: CustomNode }}
      style={{ width: '100%', height: '100vh' }}
    />
  );
};

export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant