-- atoms.tsx
interface IToDoState {
[key: string]: string[];
}
export const toDoState = atom<IToDoState>({
key: "toDo",
default: {
"To Do": ["a", "b"],
Doing: ["c", "d", "e"],
Done: ["f"],
},
});
-- App.tsx
<Boards>
<Droppable droppableId="one">
{(magic) => (
<Board ref={magic.innerRef} {...magic.droppableProps}>
{toDos.map((toDo, index) => (
<DragabbleCard key={toDo} index={index} toDo={toDo} />
))}
{magic.placeholder}
</Board>
)}
</Droppable>
</Boards>
map()
을 써서 board 들이 렌더되게 할 수 있기 때문에.-- Board.tsx
import { Droppable } from "react-beautiful-dnd";
import styled from "styled-components";
import DragabbleCard from "./DragabbleCard";
const Wrapper = styled.div`
padding: 20px 10px;
background-color: ${(props) => props.theme.boardColor};
border-radius: 5px;
min-height: 200px;
`;
interface IBoardProps {
toDos: string[];
boardId: string;
}
function Board({ toDos, boardId }: IBoardProps) {
return (
<Title>{boardId}</Title>
<Droppable droppableId={boardId}>
{(magic) => (
<Wrapper ref={magic.innerRef} {...magic.droppableProps}>
{toDos.map((toDo, index) => (
<DragabbleCard key={toDo} index={index} toDo={toDo} />
))}
{magic.placeholder}
</Wrapper>
)}
</Droppable>
);
}
export default Board;
Object.keys() 메소드는 주어진 객체의 속성 이름들을 일반적인 반복문과 동일한 순서로 순회되는 열거할 수 있는 배열로 반환합니다.
(즉, object 가 가진 key 만 array 로 뽑아내준다!)
ex) Object.keys(obj).map((item)=>obj[item])
- 예시 1)
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
// 결과값 -> Array ["a", "b", "c"]
object1['a']
// 결과값 -> :"somestring"
borardId
가 있으니, boardId
가 각각 가지는 자식들을 확인할 수 있다.-- App.tsx
<Boards>
{Object.keys(toDos).map((boardId) => (
<Board boardId={boardId} key={boardId} toDos={toDos[boardId]} />
))}
</Boards>
draggableId
: 드래그 되었던 Draggable의 idsource
: Draggable이 시작된 위치destination
: Draggable이 끝난 위치-- App.tsx
const onDragEnd = (info: DropResult) => {
console.log(info);
const { destination, draggableId, source } = info;
if(!destination) return;
if (destination?.droppableId === source.droppableId) {
// same board movement.
setToDos((allBoards) => {
const boardCopy = [...allBoards[source.droppableId]];
boardCopy.splice(source.index, 1);
boardCopy.splice(destination?.index, 0, draggableId);
return {
...allBoards,
[source.droppableId]: boardCopy,
// "Doing" : boardCopy,
// 히히 이제 doing 은 board 의 복사본이다.
};
});
}
source
와 destination
각각 복사해주어야한다!-- App.tsx
if (destination.droppableId !== source.droppableId) {
// cross board movement
setToDos((allBoards) => {
const sourceBoard = [...allBoards[source.droppableId]];
const destinationBoard = [...allBoards[destination.droppableId]];
sourceBoard.splice(source.index, 1);
destinationBoard.splice(destination?.index, 0, draggableId);
return {
...allBoards,
[source.droppableId]: sourceBoard,
[destination.droppableId]: destinationBoard,
};
});
}