-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add select/unselect all context menu to directory component (#2337)
add select/unselect all context menu to directory component
- Loading branch information
1 parent
48c9c2e
commit ac91d0d
Showing
2 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
frontend/src/components/Modals/ManageWorkspace/Documents/Directory/ContextMenu/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useRef, useEffect } from "react"; | ||
|
||
export default function ContextMenu({ | ||
contextMenu, | ||
closeContextMenu, | ||
files, | ||
selectedItems, | ||
setSelectedItems, | ||
}) { | ||
const contextMenuRef = useRef(null); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = (event) => { | ||
if ( | ||
contextMenuRef.current && | ||
!contextMenuRef.current.contains(event.target) | ||
) { | ||
closeContextMenu(); | ||
} | ||
}; | ||
|
||
document.addEventListener("mousedown", handleClickOutside); | ||
return () => { | ||
document.removeEventListener("mousedown", handleClickOutside); | ||
}; | ||
}, [closeContextMenu]); | ||
|
||
const isAllSelected = () => { | ||
const allItems = files.items.flatMap((folder) => [ | ||
folder.name, | ||
...folder.items.map((file) => file.id), | ||
]); | ||
return allItems.every((item) => selectedItems[item]); | ||
}; | ||
|
||
const toggleSelectAll = () => { | ||
if (isAllSelected()) { | ||
setSelectedItems({}); | ||
} else { | ||
const newSelectedItems = {}; | ||
files.items.forEach((folder) => { | ||
newSelectedItems[folder.name] = true; | ||
folder.items.forEach((file) => { | ||
newSelectedItems[file.id] = true; | ||
}); | ||
}); | ||
setSelectedItems(newSelectedItems); | ||
} | ||
closeContextMenu(); | ||
}; | ||
|
||
if (!contextMenu.visible) return null; | ||
|
||
return ( | ||
<div | ||
ref={contextMenuRef} | ||
style={{ | ||
position: "fixed", | ||
top: `${contextMenu.y}px`, | ||
left: `${contextMenu.x}px`, | ||
zIndex: 1000, | ||
}} | ||
className="bg-zinc-800 border border-zinc-700 rounded-md shadow-lg" | ||
> | ||
<button | ||
onClick={toggleSelectAll} | ||
className="block w-full text-left px-4 py-2 text-sm text-white hover:bg-zinc-700" | ||
> | ||
{isAllSelected() ? "Unselect All" : "Select All"} | ||
</button> | ||
<button | ||
onClick={closeContextMenu} | ||
className="block w-full text-left px-4 py-2 text-sm text-white hover:bg-zinc-700" | ||
> | ||
Cancel | ||
</button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters