TRELLO Clone #2 React-beautiful-dnd

Leesuยท2022๋…„ 12์›” 5์ผ
0
post-thumbnail

React-beautiful-dnd

npm i react-beautiful-dnd
npm i --save-dev @types/react-beautiful-dnd
  • todo, doing, done ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งˆ์šฐ์Šค ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ!

DragDropContext

  • DragDropContext์€ Drag and Drop์ด ์ผ์–ด๋‚˜๋Š” ์ „์ฒด์˜์—ญ.

  • Droppable, Dragpable ์ด ์ง€์ •๋œ ์˜์—ญ์„ ํฌํ•จ ํ•˜๊ณ  ์žˆ์–ด์•ผํ•˜๋ฉฐ,
    ์œ ์ €๊ฐ€ ๋“œ๋ž˜๊ทธ๋ฅผ ๋๋‚ธ ์‹œ์ ์— ๋ถˆ๋ ค์ง€๋Š”onDragEnd={} ํ•จ์ˆ˜๋ฅผ ํ•„์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค.

  • ์‚ฌ์šฉ

{ import { DragDropContext } from "react-beautiful-dnd";

function App() {
	const onDragEnd = (...) => {...};
	return (
    	<DragDropContext onDragEnd={onDragEnd}>
        	...
    	</DragDropContext>
    )
}

Droppable

  • Dropํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ(Drop์ด ์ด๋ฃจ์–ด์ง€๋Š” ์˜์—ญ)

  • ์œ ์ €๊ฐ€ ๋“œ๋กญํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ,
    ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” droppableId prop์„ ํ•„์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • Droppable ์˜ children ์€ react ์š”์†Œ์ด๋ฉด ์•ˆ๋˜๋ฉฐ, ํ•จ์ˆ˜์—ฌ์•ผํ•œ๋‹ค.

  • ์‚ฌ์šฉ

{ import { DragDropContext, Droppable } from "react-beautiful-dnd";

function App() {
	const onDragEnd = (...) => {...};
	return (
    	<DragDropContext onDragEnd={onDragEnd}>
        	<Droppable droppableId="one">
				{() => <ul></ul>}
            </Droppable>
    	</DragDropContext>
    )
}

Draggable

  • Drag๊ฐ€ ์ผ์–ด๋‚˜๋Š” ์š”์†Œ(item).
  • Drag์ด๋ฒคํŠธ๊ฐ€ ์‹œ์ž‘๋˜๋ฉด DragDropContext ๋ฐ”์ธ๋”ฉ ๋œ onDragStart={...}๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.
  • ์›€์ง์ผ์š”์†Œ๋“ค์ด ๊ฐ€์ง€๋Š” ๊ณ ์œ ๊ฐ’ draggableId, index prop ์„ ํ•„์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค.
  • ์‚ฌ์šฉ
{ import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";

function App() {
	const onDragEnd = (...) => {...};
	return (
    	<DragDropContext onDragEnd={onDragEnd}>
        	<Droppable droppableId="one">
				{() => 
                	<ul>
                		<Draggable draggableId="first" index={0}>
                        	{() => <li>One</li>}
                        </Draggable>
                		<Draggable draggableId="second" index={1}>
                        	{() => <li>Two</li>}
                        </Draggable>                     
                	</ul>}
            </Droppable>
    	</DragDropContext>
    )
}

  • ์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ ๊ธฐ๋ณธ์ ์ธ ์…‹ํŒ…์ด๋‹ค. ๋‹น์—ฐํžˆ ์•„๋ฌด๋Ÿฐ ๊ธฐ๋Šฅ์€ ์•ˆํ•จ.

Provided

  • droppable,draggable ์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜, provided
  • provided.innerRef๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ๋™์ž‘์„ ์‹คํ–‰ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๊ธฐ์—
    ํ•„์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด๊ฐ€์•ผํ•œ๋‹ค.
    • droppable
    • draggable
{ import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";

function App() {
	const onDragEnd = (...) => {...};
	return (
    	<DragDropContext onDragEnd={onDragEnd}>
        	<Droppable droppableId="one">
				{(magic) => 
                	 <ul ref={magic.innerRef} {...magic.droppableProps}>
                		<Draggable draggableId="first" index={0}>
                        	{(magic) =>  
                            <li ref={magic.innerRef} {...magic.draggableProps}>
                            	One
                            </li>
                            }
                        </Draggable>                     
                	</ul>}
            </Droppable>
    	</DragDropContext>
    )
}

  • one ์„ Drag ํ•˜๋Š” ์ค‘ ... ใ…‹ใ…‹
  • ์ž„์‹œ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์Šคํƒ€์ผ์„ ์•ฝ๊ฐ„ ์ค˜๋ณด์ž
const toDos = ["a", "b", "c", "d", "e", "f"];

  • ์—ฌ๊ธฐ๊นŒ์ง€ ์™„์„ฑ์€ ํ–ˆ์œผ๋‚˜
  • ๋ฆฌ์ŠคํŠธ ์ค‘ ํ•œ ๊ฐœ์˜ ์•„์ดํ…œ์ด ๋‚˜๊ฐ€๋ฉด ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž‘์•„์ง€๊ฑฐ๋‚˜ ์ปค์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒ.
  • ํฌ๊ธฐ๋ฅผ ๊ณ ์ •ํ•˜๊ธฐ ์œ„ํ•ด Droppable ์˜ provided ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์•ˆ์— ์žˆ๋Š” placeholder ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์ž.

placeholder

  • Draggable ์—˜๋ฆฌ๋จผํŠธ๋ฅผ dragํ•˜๋Š” ๋™์•ˆ position: fixed(์˜์—ญ์„ ๊ณ ์ •์‹œํ‚ด)๋ฅผ ์ ์šฉํ•œ๋‹ค.

  • Draggable์„ ๋“œ๋ž˜๊ทธํ•  ๋•Œ Droppable ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž‘์•„์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค.

  • ์ตœ์ข… ์ฝ”๋“œ

function App() { 
  const onDragEnd = () => {...};
  return (
    <DragDropContext onDragEnd={onDragEnd}>
      <Wrapper>
        <Boards>
          <Droppable droppableId="one">
            {(magic) => (
              <Board ref={magic.innerRef} {...magic.droppableProps}>
                {toDos.map((toDo, index) => (
                  <Draggable draggableId={toDo} index={index}>
                    {(magic) => (
                      <Card
                        ref={magic.innerRef}
                        {...magic.dragHandleProps}
                        {...magic.draggableProps}
                      >
                        {toDo}
                      </Card>
                    )}
                  </Draggable>
                ))}
                {magic.placeholder}
              </Board>
            )}
          </Droppable>
        </Boards>
      </Wrapper>
    </DragDropContext>
  );
}    
profile
๊ธฐ์–ต๋ ฅ ์•ˆ ์ข‹์€ FE ๊ฐœ๋ฐœ์ž์˜ ๋ฉ”๋ชจ์žฅ

0๊ฐœ์˜ ๋Œ“๊ธ€