"Si un ouvrier veut bien faire son travail, il doit d'abord affûter ses outils." - Confucius, "Les Entretiens de Confucius. Lu Linggong"
Page de garde > La programmation > Implémentation du glisser-déposer pour organiser/trier les éléments avec React, Tailwind CSS et Dnd-kit

Implémentation du glisser-déposer pour organiser/trier les éléments avec React, Tailwind CSS et Dnd-kit

Publié le 2024-08-15
Parcourir:416

Implementing Drag and Drop to Arrange/Sort Items with React, Tailwind CSS, and Dnd-kit

Introduction

Have you ever wondered how applications like Trello or Asana manage their intuitive drag-and-drop interfaces? Imagine you have an application where users need to sort their items easily. Without a smooth drag-and-drop feature, this task can become tedious and frustrating. In this blog post, we’ll explore how to implement a dynamic drag-and-drop functionality using React, Tailwind CSS, and Dnd-kit to create a seamless user experience for arranging and sorting items.

Real-World Problem

In the real world, applications often require users to rearrange items based on priority, status, or other criteria. For instance, a user may need to reorder their ideas quickly during a brainstorming session. Without an efficient drag-and-drop feature, this process could involve cumbersome steps like manually editing item positions or using inefficient move-up/move-down buttons. Our goal is to provide a solution that simplifies this process, making it more intuitive and efficient for users.

Use Case

Let’s consider a use case of a brainstorming tool where users can organize their ideas. Users need the ability to:

  • Add new ideas to a list.

  • Sort and prioritize these ideas by dragging and dropping them into the desired order.

  • Move ideas between different categories (e.g., new ideas vs. old ideas).

To achieve this, we will build a React application using Vite for the project setup, Tailwind CSS for styling, and Dnd-kit for the drag-and-drop functionality. This setup will allow us to create a user-friendly interface that enhances productivity and user experience.

Setting Up the Project

  • Initialize the Vite Project

npm create vite@latest my-drag-drop-app --template react
cd my-drag-drop-app
npm install

  • Install Required Dependencies:

npm install tailwindcss dnd-kit react-hot-toast react-icons

  • Configure Tailwind CSS:

npx tailwindcss init

  • Update tailwind.config.js:
module.exports = {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  ],
  plugins: [],
}
  • Add Tailwind directives to index.css:
@tailwind base;
@tailwind components;
@tailwind utilities;

Implementing Drag and Drop

App.jsx

The App.jsx file is the main component that sets up the overall layout of the application and manages the global state.

Summary

  • Main component to manage the overall application state.

  • Utilizes useState to handle project data and updates.

  • Incorporates the Header and DragDropArrange components for UI and functionality.

  • Includes Toaster from react-hot-toast for notifications.

Key Functions:

  • State Management: Manages state for project data.

  • Handle New Data: Function to add new data to the project data state.

  • Layout: Sets up the layout including header, main content, and notifications.

import React, { useState } from 'react';
import { Toaster } from 'react-hot-toast';
import Header from './screens/Navigation/Header';
import DragDropArrange from './screens/DDA/DragDropArrange';
import projectDataJson from "./Data/data.json"

function App() {

  const [projectData, setProjectData] = useState(projectDataJson)
  function handleNewData(data){
    const tempData = projectData.newIdea;
    const maxNumber = (Math.random() * 100) * 1000;
    tempData.push({_id: maxNumber, idea: data});
    setProjectData({...data, newIdea: tempData})
  }

  return (
    
); } export default App;

Header.jsx

The Header.jsx file serves as the navigation bar, providing a button to open a form for adding new items.

Summary:

  • Contains navigation and a button to open the item input form.

  • Uses useState to manage the state of the item input form visibility.

  • Handles user interactions for adding new items.

Key Functions:

  • Item Form Toggle: Manages the visibility of the item input form.

  • Handle New Data: Passes the new item data to the parent component.

import React, { useState } from 'react';
import { PiNotepadFill } from "react-icons/pi";
import AddIdea from '../DDA/AddIdea';

const Header = ({handleNewData}) => {
  const [ideaTabOpen, setIdeaTabOpen] = useState(false)
  return (
    
      
      {ideaTabOpen && (
        
)} > ) } export default Header

AddIdea.jsx

The AddIdea.jsx file provides the form for adding new items, including validation and submission functionality.

Summary:

  • Component for adding new items to the list.

  • Uses useState to manage form input data and character count.

  • Validates input length and submits new data to the parent component.

Key Functions:

  • Handle Change: Manages form input and character count.

  • Handle Submit: Validates and submits the form data to the parent component.

import React, { useState } from 'react';
import toast from "react-hot-toast";
import { Helmet } from 'react-helmet';

const AddIdea = ({ handleNewData, setIdeaTabOpen }) => {
    const maxLengths = 100;

    const [formData, setFormData] = useState();

    const [remainingChars, setRemainingChars] = useState(80)

    const handleChange = (e) => {
        if (e.target.value.length > maxLengths) {
            toast.error(`${`Input can't be more than ${maxLengths} characters`}`);
        } else {
            setFormData(e.target.value);
            setRemainingChars(maxLengths - e.target.value.length);
        }
    };

    const handleSubmit = (e) => {
        e.preventDefault();
        if (!formData) {
            toast.error(`You don't have an idea.`);
            return
        }
        handleNewData(formData);
        setIdeaTabOpen(false)
    };

    return (
        

{remainingChars} characters remaining

Drag Drop & Arrange | New Idea
setIdeaTabOpen(false)}>
); }; export default AddIdea;

DragDropArrange.jsx

The DragDropArrange.jsx file is responsible for managing the drag-and-drop functionality and updating the order of items based on user interactions.

Summary:

  • Main component for handling drag-and-drop functionality.

  • Uses DndContext and SortableContext from @dnd-kit/core for drag-and-drop behavior.

  • Manages state for the data array and updates the order of items based on drag events.

  • Fetches initial data from projectData and sets it to the state.

Key Functions:

  • Handle Drag End: Manages the logic for rearranging items based on drag-and-drop actions.

  • Fetch Data: Fetches initial data and sets it to the component state.

import React, { useState, useEffect } from 'react';
import { DndContext, closestCenter } from '@dnd-kit/core';
import { arrayMove, SortableContext } from '@dnd-kit/sortable';
import { Helmet } from 'react-helmet';
import Arrange from './Arrange';
import Loader from '../Navigation/Loader';

const DragDropArrange = ({projectData}) =>  {
  const [dataArray, setDataArray] = useState({
    newIdea: undefined,
    oldIdea: undefined,
    updateValue: []
  });

  const handleDragEnd = ({ active, over }) => {
    if (!over) return;

    const { fromList, targetId } = active.data.current;
    const { toList, index } = over.data.current;

    if (fromList === toList) {
      const sortedItems = arrayMove(dataArray[toList], dataArray[toList].findIndex((idea) => idea._id === targetId), index);
      setDataArray((prev) => ({ ...prev, [toList]: sortedItems }));
    } else {
      const draggedItem = dataArray[fromList].find((idea) => idea._id === targetId);
      const updatedFromList = dataArray[fromList].filter((idea) => idea._id !== targetId);
      const updatedToList = [...dataArray[toList].slice(0, index), draggedItem, ...dataArray[toList].slice(index)];

      setDataArray((prev) => ({ ...prev, [fromList]: updatedFromList, [toList]: updatedToList }));
    }
  };

  const fetchData = async () => {
    const { newIdea, oldIdea } = projectData;
    setTimeout(() => {
      setDataArray((prev) => ({ ...prev, newIdea, oldIdea }));
    }, 500);
  };

  useEffect(() => {
    fetchData();
  }, []);

  return (
    
{dataArray.newIdea && dataArray.oldIdea && ( )} {!dataArray.newIdea && !dataArray.oldIdea && (
Loading...
> )}
Drag Drop & Arrange | Home
); }; export default DragDropArrange

Arrange.jsx

The Arrange.jsx file handles the arrangement of new and old ideas, displaying them in sortable contexts.

Summary:

  • Manages the arrangement of new and old ideas.
  • Uses SortableContext for sortable behavior.

  • Displays items and manages their order within each category.

Key Functions:

  • Display Items: Renders items in their respective categories.

  • Handle Sorting: Manages the sortable behavior of items.

import React from 'react';
import { SortableContext } from '@dnd-kit/sortable';
import Drag from "./Drag";
import Drop from "./Drop";
import Lottie from 'react-lottie';
import NoData from '../../Lottie/NoData.json';

const Arrange = ({ dataArray }) => {
  const { newIdea, oldIdea } = dataArray;

  const defaultOptions = {
    loop: true,
    autoplay: true,
    animationData: NoData,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice"
    }
  };

  return (
    

New Idea

item._id)}> {newIdea.length > 0 && ( {newIdea?.map((data) => ( ))} > )} {newIdea.length
> )}

Old Idea

item._id)}> {oldIdea.length > 0 && ( {oldIdea?.map((data) => ( ))} > )} {oldIdea.length
> )}
); } export default Arrange

Drag.jsx

The Drag.jsx file manages the draggable items, defining their behavior and style during the drag operation.

Summary:

  • Manages the behavior and style of draggable items.

  • Uses useDraggable from @dnd-kit/core for draggable behavior.

  • Defines the drag and drop interaction for items.

Key Functions:

  • useDraggable: Provides drag-and-drop functionality.

  • Style Management: Updates the style based on drag state.

import React from 'react';
import { useDraggable } from '@dnd-kit/core';
import { IoMdMove } from "react-icons/io";

const Drag = ({ data, listType }) => {

    const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({
        id: data._id,
        data: { fromList: listType, targetId: data._id },
    });

    const style = {
        transform: transform ? `translate3d(${transform.x}px, ${transform.y}px, 0)` : undefined,
        opacity: isDragging ? 0.5 : 1,
        pointerEvents: isDragging ? 'none' : 'auto',
    };

    return (
        
            

{data?.idea}

> ); }; export default Drag;

Drop.jsx

The Drop.jsx file defines the droppable areas where items can be dropped, including visual feedback during the drag operation.

Summary:

  • Manages the behavior of droppable areas.
  • Uses useDroppable from @dnd-kit/core for droppable behavior.

  • Provides visual feedback during drag-and-drop interactions.

Key Functions:

  • useDroppable: Provides droppable functionality.

  • Handle Drop: Manages drop actions and updates the state accordingly.

import React from 'react';
import { useDroppable } from '@dnd-kit/core';

const Drop= ({ index, setDragged, listType }) => {
    const { isOver, setNodeRef } = useDroppable({
        id: `${listType}-${index}`,
        data: { toList: listType, index },
    });

    const handleDrop = (e) => {
        e.preventDefault();
        setDragged({ toList: listType, index });
    };

    return (
        
e.preventDefault()} className={`w-auto h-16 rounded-lg flex items-center justify-center text-xs text-secondary_shadow ${isOver ? ` opacity-100` : `opacity-0`}`} style={{ pointerEvents: 'none' }} >
); }; export default Drop

Conclusion

By following this comprehensive guide, you can create a dynamic and user-friendly drag-and-drop interface for your applications. This setup not only enhances user experience but also makes managing and organizing items intuitive and efficient. The combination of React, Tailwind CSS, and Dnd-kit provides a robust foundation for building such interactive features.

Feel free to customize and extend this implementation to suit your specific needs. Happy coding!

Source Code

You can find the complete source code for this project in my GitHub repository:
Github Link

Déclaration de sortie Cet article est reproduit sur : https://dev.to/devwithshreyash/implementing-drag-and-drop-to-arrangesort-items-with-react-tailwind-css-and-dnd-kit-2b2f?1S'il y en a infraction, veuillez contacter [email protected] pour supprimer
Dernier tutoriel Plus>

Clause de non-responsabilité: Toutes les ressources fournies proviennent en partie d'Internet. En cas de violation de vos droits d'auteur ou d'autres droits et intérêts, veuillez expliquer les raisons détaillées et fournir une preuve du droit d'auteur ou des droits et intérêts, puis l'envoyer à l'adresse e-mail : [email protected]. Nous nous en occuperons pour vous dans les plus brefs délais.

Copyright© 2022 湘ICP备2022001581号-3