Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix corner cases for side nav when finding selected id #101

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions public/components/common/side_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ import { EuiPage, EuiPageBody, EuiPageSideBar, EuiSideNav, EuiSideNavItemType }
import React from 'react';

export const renderPageWithSidebar = (BodyComponent: React.ReactNode) => {
function setIsSelected(items: EuiSideNavItemType<React.ReactNode>[], hash: string) {
// set items.isSelected based on location.hash passed in
// tries to find an item where href is a prefix of the hash
// if none will try to find an item where the hash is a prefix of href
function setIsSelected(
items: EuiSideNavItemType<React.ReactNode>[],
hash: string,
initial = true,
reverse = false
): boolean {
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.href === hash) {
if (item.href && ((reverse && item.href.startsWith(hash)) || hash.startsWith(item.href))) {
item.isSelected = true;
return true;
}
if (item.items?.length && setIsSelected(item.items, hash)) return true;
if (item.items?.length && setIsSelected(item.items, hash, false, reverse)) return true;
}
return false;
return initial && setIsSelected(items, hash, false, !reverse);
}

const items = [
Expand Down