From 9bdfdd7db0fef066f10a9ba3c285b9811607ff82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Wed, 21 Feb 2024 08:06:17 +0100 Subject: [PATCH] dragdropclone --- src/components/draggables/labelInput.jsx | 30 ++++++++++------- src/components/dropDiv.jsx | 31 +++++++++++++++++ src/components/sheets.jsx | 31 +++++++++++++---- src/styles/main.css | 42 ++++++++++++++++++++++-- 4 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 src/components/dropDiv.jsx diff --git a/src/components/draggables/labelInput.jsx b/src/components/draggables/labelInput.jsx index 0e8a9a4..700bbf2 100644 --- a/src/components/draggables/labelInput.jsx +++ b/src/components/draggables/labelInput.jsx @@ -1,21 +1,27 @@ import React from 'react'; import { useDrag } from 'react-dnd'; -const MyDraggableComponent = ({ isDragging, text }) => { - const [{ opacity }, dragRef] = useDrag( - () => ({ - type: 'card', - collect: (monitor) => ({ - opacity: monitor.isDragging() ? 0.5 : 1 - }) +const DraggableItem = ({ name }) => { + const [{ isDragging }, drag] = useDrag({ + item: { name }, + type: 'draggable', + collect: (monitor) => ({ + isDragging: !!monitor.isDragging(), }), - [] - ) + }); return ( -
- +
+
) }; -export default MyDraggableComponent; +export default DraggableItem; diff --git a/src/components/dropDiv.jsx b/src/components/dropDiv.jsx new file mode 100644 index 0000000..833021d --- /dev/null +++ b/src/components/dropDiv.jsx @@ -0,0 +1,31 @@ +import React from 'react'; +import { useDrop } from "react-dnd"; + +const DropDiv = ({type, items}) => { + const [{ isOver }, drop] = useDrop(() => ({ + accept: type, + collect: (monitor) => ({ + isOver: !!monitor.isOver(), + }), + })); + console.log(items); + if (items.length === 0) { +
+
+ } + return ( +
+ {items.map((item) => { + return
{item.content}
+ })} +
+ ); +}; + +export default DropDiv; diff --git a/src/components/sheets.jsx b/src/components/sheets.jsx index 551bd45..605dd1d 100644 --- a/src/components/sheets.jsx +++ b/src/components/sheets.jsx @@ -1,14 +1,26 @@ -import React from 'react'; +import React, { useState } from 'react'; import { Link } from 'react-router-dom'; import '../styles/main.css'; // Import CSS/LESS file directly import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; -import LabelInput from './draggables/labelInput.jsx'; + +import DropDiv from './dropDiv.jsx'; +import DraggableItem from './draggables/labelInput.jsx'; + + +const Sheets = () => { + let items = []; + + function handleDrop(item) { + const id = items.length+1; + const draggable = {id:id , content: item}; + + items.push(draggable); + } -const Home = () => { return ( -
+