fix hiding/showing issue

This commit is contained in:
Red Adaya
2024-09-23 11:04:46 +08:00
parent 640ffd3845
commit 2d3703a215
3 changed files with 33 additions and 8 deletions
-8
View File
@@ -35,12 +35,4 @@
float: right;
}
}
.sub-dropdown {
position: absolute;
top: 0;
left: 100%;
min-width: 100%;
white-space: nowrap;
}
}
@@ -85,6 +85,14 @@ export const Test: Story = {
subItems: [
{ label: "Option 3 -> 3 -> 1", onClick: () => null },
{ label: "Option 3 -> 3 -> 2", onClick: () => null },
{
label: "Option 3 -> 3",
onClick: () => console.log("Clicked Option 3"),
subItems: [
{ label: "Option 3 -> 3 -> 1", onClick: () => null },
{ label: "Option 3 -> 3 -> 2", onClick: () => null },
],
},
],
},
],
+25
View File
@@ -188,11 +188,36 @@ const Dropdown = memo(({ items, anchorRef, boundaryRef, className }: DropdownPro
) => {
event.stopPropagation();
// Build the full key for the current item
const key = parentKey ? `${parentKey}-${index}` : `${index}`;
setVisibleSubMenus((prev) => {
// Create a copy of the previous state
const updatedState = { ...prev };
// Ensure the current submenu and its ancestors are visible
updatedState[key] = { visible: true, label: item.label };
// Extract ancestors of the key (e.g., "2-2-1" -> "2-2" -> "2")
const ancestors = key.split("-").reduce((acc, part, idx) => {
if (idx === 0) return [part];
return [...acc, `${acc[idx - 1]}-${part}`];
}, [] as string[]);
// Mark ancestors visible
ancestors.forEach((ancestorKey) => {
if (updatedState[ancestorKey]) {
updatedState[ancestorKey].visible = true;
}
});
// Hide any submenu that is not part of the current hierarchy
for (const pkey in updatedState) {
if (!ancestors.includes(pkey) && pkey !== key) {
updatedState[pkey].visible = false;
}
}
return updatedState;
});