From 2191550061bb36cc8d4f84406006b5f5802afb5e Mon Sep 17 00:00:00 2001 From: Pragadesh-45 <54320162+Pragadesh-45@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:19:36 +0530 Subject: [PATCH] Feat table resize and reorder (#2641) * feat-Table-resize-and-Reorder * feat-Table-resize-and-Reorder * feat-Table-resize-and-Reorder/fixed-table-resize-update --- .../src/components/ReorderTable/index.js | 112 +++++++++++++++ .../RequestPane/QueryParams/StyledWrapper.js | 12 +- .../RequestPane/QueryParams/index.js | 134 +++++++++--------- .../src/components/Table/StyledWrapper.js | 63 ++++++++ .../bruno-app/src/components/Table/index.js | 110 ++++++++++++++ .../ReduxStore/slices/collections/index.js | 34 +++++ 6 files changed, 392 insertions(+), 73 deletions(-) create mode 100644 packages/bruno-app/src/components/ReorderTable/index.js create mode 100644 packages/bruno-app/src/components/Table/StyledWrapper.js create mode 100644 packages/bruno-app/src/components/Table/index.js diff --git a/packages/bruno-app/src/components/ReorderTable/index.js b/packages/bruno-app/src/components/ReorderTable/index.js new file mode 100644 index 000000000..9d8c11088 --- /dev/null +++ b/packages/bruno-app/src/components/ReorderTable/index.js @@ -0,0 +1,112 @@ +import React, { useEffect, useRef, useState, useCallback } from 'react'; +import { IconGripVertical, IconMinusVertical } from '@tabler/icons'; + +/** + * ReorderTable Component + * + * A table component that allows rows to be reordered via drag-and-drop. + * + * @param {Object} props - The component props + * @param {React.ReactNode[]} props.children - The table rows as children + * @param {function} props.updateReorderedItem - Callback function to handle reordered rows + */ + +const ReorderTable = ({ children, updateReorderedItem }) => { + const tbodyRef = useRef(); + const [rowsOrder, setRowsOrder] = useState(React.Children.toArray(children)); + const [hoveredRow, setHoveredRow] = useState(null); + const [dragStart, setDragStart] = useState(null); + + /** + * useEffect hook to update the rows order and handle row hover states + */ + useEffect(() => { + setRowsOrder(React.Children.toArray(children)); + handleRowHover(null, false); + }, [children, dragStart]); + + const handleRowHover = (index, hoverstatus = true) => { + setHoveredRow(hoverstatus ? index : null); + }; + + const handleDragStart = (e, index) => { + e.dataTransfer.effectAllowed = 'move'; + e.dataTransfer.setData('text/plain', index); + setDragStart(index); + }; + + const handleDragOver = (e, index) => { + e.preventDefault(); + e.dataTransfer.dropEffect = 'move'; + handleRowHover(index); + }; + + const handleDrop = (e, toIndex) => { + e.preventDefault(); + const fromIndex = parseInt(e.dataTransfer.getData('text/plain'), 10); + if (fromIndex !== toIndex) { + const updatedRowsOrder = [...rowsOrder]; + const [movedRow] = updatedRowsOrder.splice(fromIndex, 1); + updatedRowsOrder.splice(toIndex, 0, movedRow); + setRowsOrder(updatedRowsOrder); + + updateReorderedItem({ + updateReorderedItem: updatedRowsOrder.map((row) => row.props['data-uid']) + }); + + setTimeout(() => { + handleRowHover(toIndex); + }, 0); + } + }; + + return ( +
+ {rowsOrder.map((row, index) => ( +Name | -Value | -- |
+ ? queryParams.map((param, index) => ( + | ||||
+ handleQueryParamChange(e, param, 'name')} + /> + | +
+ |
+
+
handleQueryParamChange(e, param, 'name')}
+ type="checkbox"
+ checked={param.enabled}
+ tabIndex="-1"
+ className="mr-3 mousetrap"
+ onChange={(e) => handleQueryParamChange(e, param, 'enabled')}
/>
- |
-
- |
-
-
- handleQueryParamChange(e, param, 'enabled')}
- />
-
-
- |
-
+ {name} + + | + ))} +
---|