0

In React I am trying to create multiple tiles in my dashboard. Here is the sample frame. enter image description here

In the above picture, there are 5 rows and each rows have different columns. I was trying to implement the rearrangements of these tiles in both X and Y directions. All these rows can be arranged in Y directions. Each row can be arranged in X direction.

To implement this I started using react-dnd and here is my code to do that.

import React from "react";
import { useDrag, useDrop, DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";

const DraggableItem = ({ children }) => {
  const [{ isDragging }, dragRef] = useDrag(() => ({
    type: "item",
    collect: (monitor) => ({
      isDragging: monitor.isDragging(),
    }),
  }));

  return (
    <div ref={dragRef} style={{ opacity: isDragging ? 0.5 : 1 }}>
      {children}
    </div>
  );
};

const DroppableArea = () => {
  const [, dropRef] = useDrop(() => ({
    accept: "item",
    drop: (item, monitor) => {
      // handle drop event
      console.log("Item dropped:", item);
    },
  }));

  return <div ref={dropRef}>Droppable Area</div>;
};

export const ChartDashboard = () => (
  <DndProvider backend={HTML5Backend}>
    <main className="w-full p-4 pt-20 overflow-scroll h-screen">
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-4">
        <DraggableItem>
          <div className="border-2 border-dashed border-gray-300 rounded-lg dark:border-gray-600 h-32 md:h-64">
            1
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-32 md:h-64">
            2
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-32 md:h-64">
            3
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-32 md:h-64">
            4
          </div>
        </DraggableItem>
      </div>
      <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-96 mb-4">
        5
      </div>
      <div className="grid grid-cols-2 gap-4 mb-4">
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            6
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            7
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            8
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            9
          </div>
        </DraggableItem>
      </div>
      <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-96 mb-4">
        10
      </div>
      <div className="grid grid-cols-2 gap-4">
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            11
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            12
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            13
          </div>
        </DraggableItem>
        <DraggableItem>
          <div className="border-2 border-dashed rounded-lg border-gray-300 dark:border-gray-600 h-48 md:h-72">
            14
          </div>
        </DraggableItem>
      </div>
      <DroppableArea />
    </main>
  </DndProvider>
);

I need help in acvieving this feature. I am not sure which library is best to implement this feature. Can anyone help me with this? There are other librarys this supports drag and drop. for example: react-beautiful-dnd, react-sortable-hoc etc.

Any library will work.

Saswat Arabinda
  • 407
  • 2
  • 6
  • 16
  • I have used `react-dnd`, `react-beautiful-dnd` and `@hello-pangea/dnd` in the fast. I was attracted to `react-dnd` due to their beautiful website and examples. But after using it for a few months I found out it is the most complex to use and there is not enough documentation to support the issues you face. Then I switched to `react-beautiful-dnd`, and I found out it is very simple to use and fulfilling all my needs without any complexity like 1react-dnd`. But as mentioned on their page `Atlassian` is not actively managing this library anymore. Characters finished (please check next comment) – Simran Singh May 31 '23 at 04:07
  • Then someone from `mui` community recommend me a new library called `@hello-pangea/dnd`. It was not very famous at that time, but the good thing was it was a fork of `react-beautiful-dnd` and they basically improvise and actively updated and did bug fixes. Usage is exactly same as `react-beautiful-dnd`. You can follow the same tutorial if you need to. And what you're trying to build is a kind of horizontal Kanban layout with 5 different lanes like this example here `https://dnd.hellopangea.com/?path=/story/examples-board--simple` and you can achieve it very easily with this library. – Simran Singh May 31 '23 at 04:12
  • @SimranSingh Thank you so much for your reply. I found one example in another post where they used `react-beautiful-dnd` to implement the same functionality. `https://codesandbox.io/s/jp4ow4r45v?file=/index.js:131-150` This one does not work with React 18. I wonder if `@hello-pangea/dnd` supports React 18? – Saswat Arabinda May 31 '23 at 04:20
  • Yes, It does. we are using it our React 18 project. – Simran Singh May 31 '23 at 04:26

0 Answers0