Code Monkey home page Code Monkey logo

simple-dnd's Introduction

simple-dnd

Description

This is a simple example of the basic of drag and drop in react. Here contain two example with one is using react's onDrag prop and the other is using addEventListener to handle the drag and drop event.

Usage

  1. For the wrapper one, you need to wrap the content like this
  <div>
        <DnDContext onDragEnd={OnDragEnd}>
          {Object.entries(table).map(([key, value]) => {
            return (
              <Droppable
                className="border-black border p-5 m-5"
                droppableId={key}
                key={key}
                // WrapperTag={Wrapper}
              >
                {(droppableContext) => {
                  {
                    return value.map((content, index) => (
                      <Draggable
                        draggableId={content}
                        key={content}
                        droppableContext={droppableContext}
                        index={index}
                      >
                        {content}
                      </Draggable>
                    ));
                  }
                }}
              </Droppable>
            );
          })}
        </DnDContext>
      </div>
  1. For the hook one, in each component you need to use the hook like this:
const App = () => {
	const [table, setTable] = useState<{ [key: DnDId]: string[] }>({
		1: ['aaa', 'bbb'],
		2: ['ccc', 'ddd'],
		3: ['eee', 'ggg'],
	});

	const OnDragEnd = (result) => {
    // some code
	};

	return (
		<DndContext onDragEnd={OnDragEnd}>
			{Object.entries(table).map(([key, value], index) => {
				return (
					<Column className='border-black border p-5 m-5' dndId={key} key={index}>
						{value.map((content, index) => (
							<Card dndId={index} key={content + index + key}>{content}</Card>
						))}
					</Column>
				);
			})}
		</DndContext>
	);
}

const Column = ({children}) => {
  const { dropRef, isOver } = useDroppable({id});
  return (
    <div ref={dropRef} className={`border p-5 m-5 ${isOver ? 'bg-gray-200' : ''}`}>
      {children}
    </div>
  )
}

const Draggable = ({children}) => {
  const { dragRef, isDragging } = useDraggable({id});
  return (
    <div ref={dragRef} className="border p-5 m-5">
      {children}
    </div>
  )
}

Reference

simple-dnd's People

Contributors

momingse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.