Skip to content

Commit

Permalink
Automatically update the expandedKeys when using setActiveItemKey in …
Browse files Browse the repository at this point in the history
…useSideMenu api (pkp#424)
  • Loading branch information
blesildaramirez authored Oct 9, 2024
1 parent 9706e18 commit 9e986d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/composables/useSideMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/composables/useSideMenu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit 9e986d6

Please sign in to comment.