UNPKG

@parkassist/pa-ui-library

Version:
78 lines 2.64 kB
var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React, { useEffect, useState } from "react"; import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"; import { Column } from "../Layout/Flex"; import { ItemContainer, ListContainer } from "./components/Containers"; import styled from 'styled-components'; const Container = styled.div(() => ({ display: 'flex', width: '100%', gap: '12px' })); export default function DragDropItems(_a) { var { items, reorderedList, onChange = () => null, disableDragging = false } = _a, props = __rest(_a, ["items", "reorderedList", "onChange", "disableDragging"]); const [itemList, setItemList] = useState(items); useEffect(() => { setItemList(items); }, [items]); const handleDrop = ({ destination, source }) => { if (!destination || destination.droppableId === source.droppableId && destination.index === source.index) { return; } const updatedList = [...itemList]; const [reorderedItem] = updatedList.splice(source.index, 1); updatedList.splice(destination.index, 0, reorderedItem); setItemList(updatedList); reorderedList(updatedList); }; return _jsx(Column, { style: { width: '100%' }, children: _jsx(DragDropContext, { onChange: onChange, onDragEnd: handleDrop, children: _jsx(Container, { children: _jsx(Column, { style: { width: '100%' }, children: _jsx(Droppable, { droppableId: 'droppable-column', children: provided => _jsxs(ListContainer, { dndProps: provided, children: [itemList.map((item, index) => _jsx(Draggable, { draggableId: item.id.toString(), index: index, isDragDisabled: disableDragging, children: provided => { return _jsx(ItemContainer, { dndProps: provided, children: item.component }); } }, item.id.toString())), provided.placeholder] }) }) }) }) }) }); }