From 9e986d6a9874b10859d665397ca564ca0e1388bb Mon Sep 17 00:00:00 2001 From: Blesilda Ramirez Date: Wed, 9 Oct 2024 21:31:12 +0800 Subject: [PATCH] Automatically update the expandedKeys when using setActiveItemKey in useSideMenu api (#424) --- src/composables/useSideMenu.js | 40 +++++++++++++++++++++++++++++++-- src/composables/useSideMenu.mdx | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/composables/useSideMenu.js b/src/composables/useSideMenu.js index a1c6cc509..11edc17ee 100644 --- a/src/composables/useSideMenu.js +++ b/src/composables/useSideMenu.js @@ -53,14 +53,41 @@ export function useSideMenu(_items, opts = {}) { return expandedKeys; } + function findParentKeys(items, key, parents = []) { + for (const item of items) { + // if item key matches the active key, return the item parents + if (item.key === key) { + return parents; + } + + // if the item has nested items, continue searching + if (item.items) { + const result = findParentKeys(item.items, key, [...parents, item.key]); + if (result) { + return result; + } + } + } + + // if the key was not found, return null + return null; + } + function setActiveItemKey(key = '') { activeItemKey.value = key; + + const parentKeys = findParentKeys(items.value, key); + if (parentKeys) { + setExpandedKeys([...parentKeys, ...Object.keys(_expandedKeys)]); + } } function compareUrlPaths(url1, url2) { - const parsedUrl1 = new URL(url1); - const parsedUrl2 = new URL(url2); + const parsedUrl1 = isValidURL(url1) ? new URL(url1) : false; + const parsedUrl2 = isValidURL(url2) ? new URL(url2) : false; return ( + parsedUrl1 && + parsedUrl2 && parsedUrl1.pathname === parsedUrl2.pathname && parsedUrl1.hostname === parsedUrl2.hostname ); @@ -110,6 +137,15 @@ export function useSideMenu(_items, opts = {}) { return result; } + function isValidURL(string) { + try { + new URL(string); + return true; + } catch (_) { + return false; + } + } + const sideMenuProps = computed(() => ({ items: items.value, expandedKeys: expandedKeys.value, diff --git a/src/composables/useSideMenu.mdx b/src/composables/useSideMenu.mdx index 7a39e0f33..0c06b1ac4 100644 --- a/src/composables/useSideMenu.mdx +++ b/src/composables/useSideMenu.mdx @@ -102,7 +102,7 @@ const {selectedItem} = useSideMenu(items, 'menuItem2_1'); // will return {label: ### setActiveItemKey -This function allows you to set a new active item key dynamically from within your component or from other components that consume the `useSideMenu` composable. It accepts a string that represents the key of the item you want to set as active. This value will be assigned to the `activeItemKey` ref. +This function allows you to set a new active item key dynamically from within your component or from other components that consume the `useSideMenu` composable. It accepts a string that represents the key of the item you want to set as active. This value will be assigned to the `activeItemKey` ref. Additionally, the function updates the `expandedKeys` to include the parent keys of the active item, while retaining the initially expanded keys set in the API. ```javascript const {activeItemKey, setActiveItemKey} = useSideMenu(items, {