From 23bf495a5a49c20e0073dc65345dcf74c1ba53cd Mon Sep 17 00:00:00 2001 From: gianantoniopini <63844628+gianantoniopini@users.noreply.github.com> Date: Tue, 9 Feb 2021 05:59:32 +0100 Subject: [PATCH] List component: added support for drag & drop --- src/components/builder/lists/List.js | 92 +++++++++++++++------- src/components/builder/lists/ListItem.js | 99 ++++++++++++++---------- 2 files changed, 123 insertions(+), 68 deletions(-) diff --git a/src/components/builder/lists/List.js b/src/components/builder/lists/List.js index 7a5f2163..bfe77b9e 100644 --- a/src/components/builder/lists/List.js +++ b/src/components/builder/lists/List.js @@ -1,10 +1,11 @@ import { get, isEmpty } from 'lodash'; import React, { memo, useContext } from 'react'; +import { DragDropContext, Droppable } from 'react-beautiful-dnd'; import { useTranslation } from 'react-i18next'; import { MdAdd } from 'react-icons/md'; import ModalContext from '../../../contexts/ModalContext'; -import { useSelector } from '../../../contexts/ResumeContext'; -import { formatDateRange } from '../../../utils'; +import { useDispatch, useSelector } from '../../../contexts/ResumeContext'; +import { formatDateRange, reorder } from '../../../utils'; import Button from '../../shared/Button'; import EmptyList from './EmptyList'; import styles from './List.module.css'; @@ -24,42 +25,79 @@ const List = ({ const { t, i18n } = useTranslation(); const items = useSelector(path, []); const { emitter } = useContext(ModalContext); + const dispatch = useDispatch(); const handleAdd = () => emitter.emit(event); const handleEdit = (data) => emitter.emit(event, data); + const onDragEnd = (result) => { + const { source, destination } = result; + + if (!destination) { + return; + } + + const sourceDroppableId = source.droppableId; + const destinationDroppableId = destination.droppableId; + if (sourceDroppableId !== destinationDroppableId) { + return; + } + + const itemsReordered = reorder(items, source.index, destination.index); + dispatch({ + type: 'on_input', + payload: { + path, + value: itemsReordered, + }, + }); + }; + return (
{isEmpty(items) ? ( ) : ( - items.map((x, i) => ( - handleEdit(x)} - isFirst={i === 0} - isLast={i === items.length - 1} - /> - )) + + + {(dropProvided) => ( +
+ {items.map((x, i) => ( + handleEdit(x)} + isFirst={i === 0} + isLast={i === items.length - 1} + index={i} + /> + ))} + {dropProvided.placeholder} +
+ )} +
+
)}
diff --git a/src/components/builder/lists/ListItem.js b/src/components/builder/lists/ListItem.js index 7dfd406b..46b47f92 100644 --- a/src/components/builder/lists/ListItem.js +++ b/src/components/builder/lists/ListItem.js @@ -1,5 +1,6 @@ import { Menu, MenuItem } from '@material-ui/core'; import React, { memo, useState } from 'react'; +import { Draggable } from 'react-beautiful-dnd'; import { useTranslation } from 'react-i18next'; import { IoIosArrowDown, IoIosArrowUp } from 'react-icons/io'; import { MdMoreVert } from 'react-icons/md'; @@ -15,6 +16,7 @@ const ListItem = ({ isFirst, isLast, onEdit, + index, }) => { const dispatch = useDispatch(); const { t } = useTranslation(); @@ -66,49 +68,64 @@ const ListItem = ({ }; return ( -
-
- {title} - - {subtitle && ( - {subtitle} - )} - - {text && ( - {text} - )} -
- -
- - + {(dragProvided) => ( +
-
- - - - - - +
+ {title} + + {subtitle && ( + + {subtitle} + + )} + + {text && ( + + {text} + + )}
- {t('shared.buttons.edit')} - - - {t('shared.buttons.delete')} - - -
-
-
+ +
+ + +
+ + + + + + +
+ + {t('shared.buttons.edit')} + + + + {t('shared.buttons.delete')} + + +
+
+
+ )} + ); };