From a834c23370aa9fdb0f496addb1668525446daf66 Mon Sep 17 00:00:00 2001 From: Pritam Bag <130136389+PritamBag@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:57:53 +0530 Subject: [PATCH] feat(global): :sparkles: add double click feature adding, removing, double click added for style classes Ref: #54 --- .../right-drawer-components/DialogDesign.js | 129 +++++++++++------- 1 file changed, 83 insertions(+), 46 deletions(-) diff --git a/app/components/page-builder/right-drawer-components/DialogDesign.js b/app/components/page-builder/right-drawer-components/DialogDesign.js index b659bd0..d246c93 100644 --- a/app/components/page-builder/right-drawer-components/DialogDesign.js +++ b/app/components/page-builder/right-drawer-components/DialogDesign.js @@ -1,19 +1,20 @@ -import { useState } from "react"; +import { useState, useRef } from "react"; import { - CoreBox, CoreClasses, CoreH6, CoreIcon, CoreIconButton, CoreStack, + CoreBox, CoreClasses, CoreIcon, CoreIconButton, CoreStack, CoreTypographyBody2, CoreTypographyOverline } from "@wrappid/core"; export default function DialogDesign() { - const [selectedItemsAdding, setSelectedItemsAdding] = useState([]); // For selecting items to add - const [selectedItemsRemoving, setSelectedItemsRemoving] = useState([]); // For selecting items to remove - const [transferredItems, setTransferredItems] = useState([]); // Transferred items state + const [selectedItemsAdding, setSelectedItemsAdding] = useState([]); + const [selectedItemsRemoving, setSelectedItemsRemoving] = useState([]); + const [transferredItems, setTransferredItems] = useState([]); + const lastClickRef = useRef({ action: null, item: null, time: 0 }); + const DOUBLE_CLICK_THRESHOLD = 1000; // 1000 milliseconds threshold for consecutive clicks - // Returns classes based on nesting depth const getHeaderClasses = (depth) => { - const baseClasses = [CoreClasses.POSITION.POSITION_STICKY, CoreClasses.PADDING.PY1, CoreClasses.BG.BG_WHITE, CoreClasses.MARGIN.MB1]; + const baseClasses = [CoreClasses.POSITION.POSITION_STICKY, CoreClasses.PADDING.P1, CoreClasses.BG.BG_GREY_300, CoreClasses.COLOR.TEXT_BLACK_50]; switch (depth) { case 0: @@ -27,37 +28,65 @@ export default function DialogDesign() { } }; - // Handles item selection (toggle for both adding/removing) - const handleItemClick = (item) => { - setSelectedItemsAdding((prev) => - prev.includes(item) ? prev.filter((i) => i !== item) : [...prev, item] - ); + const handleItemClick = (item, action) => { + const now = Date.now(); + const { item: lastItem, time: lastTime, action: lastAction } = lastClickRef.current; + + if (item === lastItem && action === lastAction && now - lastTime < DOUBLE_CLICK_THRESHOLD) { + if (action === "add") { + // Transfer the item + if (!transferredItems.includes(item)) { + setTransferredItems(prev => [...prev, item]); + setSelectedItemsAdding(prev => prev.filter(i => i !== item)); + } + } else if (action === "remove") { + // Remove the item from transferred items + setTransferredItems(prev => prev.filter(i => i !== item)); + setSelectedItemsRemoving(prev => prev.filter(i => i !== item)); + } + } else { + if (action === "add") { + // Toggle item in selectedItemsAdding + setSelectedItemsAdding(prev => + prev.includes(item) ? prev.filter(i => i !== item) : [...prev, item] + ); + } else if (action === "remove") { + // Toggle item in selectedItemsRemoving + setSelectedItemsRemoving(prev => + prev.includes(item) ? prev.filter(i => i !== item) : [...prev, item] + ); + } + } + + // Update the last click reference + lastClickRef.current = { action, item, time: now }; }; - // Handles the transfer of items to the "added" list const handleTransfer = () => { - setTransferredItems((prev) => [...prev, ...selectedItemsAdding]); - setSelectedItemsAdding([]); // Reset adding selection + setTransferredItems(prev => [...prev, ...selectedItemsAdding]); + setSelectedItemsAdding([]); }; - // Handles removing items from the transferred list const handleRemoveTransfer = () => { - setTransferredItems((prev) => prev.filter((item) => !selectedItemsRemoving.includes(item))); - setSelectedItemsRemoving([]); // Reset removing selection + setTransferredItems(prev => prev.filter(item => !selectedItemsRemoving.includes(item))); + setSelectedItemsRemoving([]); }; - // Recursively renders keys with appropriate nesting and depth const renderCoreKeys = (obj, parentKey = "", depth = 0) => { return Object.keys(obj).map((key) => { const value = obj[key]; const fullKey = parentKey ? `${parentKey}.${key}` : key; + if (transferredItems.includes(fullKey)) { + return null; + } + if (typeof value === "object" && !Array.isArray(value)) { return ( - + {key} - + {renderCoreKeys(value, fullKey, depth + 1)} @@ -69,10 +98,10 @@ export default function DialogDesign() { return ( handleItemClick(fullKey)} - styleClasses={[CoreClasses.CURSOR.CURSOR_POINTER, selectedItemsAdding.includes(fullKey) ? CoreClasses.BG.BG_GREY_200 : null]} + onClick={() => handleItemClick(fullKey, "add")} + styleClasses={[CoreClasses.CURSOR.CURSOR_POINTER]} > - {key} + {key} ); }); @@ -80,12 +109,25 @@ export default function DialogDesign() { return ( - {/* Available Items Section */} - - {renderCoreKeys(CoreClasses)} - + + + + Add Style Classes + + - {/* Transfer Buttons */} + + {renderCoreKeys(CoreClasses)} + + + @@ -96,34 +138,29 @@ export default function DialogDesign() { - {/* Transferred Items Section */} - - - {/* Clear button to reset transferred items */} - { setTransferredItems([]); }} styleClasses={[CoreClasses.MARGIN.MB3, CoreClasses.ALIGNMENT.ALIGN_CONTENT_END]}> + + + + Added Styles + + + { setTransferredItems([]); }} styleClasses={[CoreClasses.ALIGNMENT.ALIGN_CONTENT_END]}> - - - Added style classes - - - {/* Render transferred items */} + {transferredItems.map((item) => ( setSelectedItemsRemoving((prev) => - prev.includes(item) ? prev.filter((i) => i !== item) : [...prev, item] - )} - styleClasses={[CoreClasses.CURSOR.CURSOR_POINTER, selectedItemsRemoving.includes(item) ? CoreClasses.BG.BG_GREY_200 : null]} + onClick={() => handleItemClick(item, "remove")} + styleClasses={[CoreClasses.CURSOR.CURSOR_POINTER]} > - {item} + {item} ))} ); -} +} \ No newline at end of file