-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from newrelic/cayla/search-box
Add a search box and filter down links that match the search term
- Loading branch information
Showing
10 changed files
with
372 additions
and
195 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,127 +1,55 @@ | ||
import React, { Fragment, useState, useContext } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Link } from 'gatsby'; | ||
import cx from 'classnames'; | ||
import { BreadcrumbContext } from './BreadcrumbContext'; | ||
import FeatherIcon from './FeatherIcon'; | ||
import NewRelicIcon from './NewRelicIcon'; | ||
import NavigationItems from './NavigationItems'; | ||
import pages from '../data/sidenav.json'; | ||
|
||
import matchSearchString from '../utils/matchSearchString'; | ||
import styles from './Navigation.module.scss'; | ||
|
||
// recursively create navigation | ||
const renderNav = (pages, depthLevel = 0) => { | ||
// TODO: Refactor this function into a component | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName); | ||
const isHomePage = crumbs.length === 0 && depthLevel === 0; | ||
const iconLibrary = { | ||
'Collect data': 'collectData', | ||
'Build apps': 'buildApps', | ||
'Automate workflows': 'automation', | ||
'Explore docs': 'developerDocs', | ||
}; | ||
|
||
const groupedPages = pages.reduce((groups, page) => { | ||
const { group = '' } = page; | ||
|
||
return { | ||
...groups, | ||
[group]: [...(groups[group] || []), page], | ||
}; | ||
}, {}); | ||
|
||
return Object.entries(groupedPages).map(([group, pages]) => ( | ||
<Fragment key={group}> | ||
{group && ( | ||
<li className={cx(styles.navLink, styles.groupName)}>{group}</li> | ||
)} | ||
{pages.map((page) => { | ||
const [isExpanded, setIsExpanded] = useState( | ||
isHomePage || crumbs.includes(page.displayName) | ||
); | ||
const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName; | ||
const headerIcon = depthLevel === 0 && ( | ||
<NewRelicIcon | ||
className={styles.headerIcon} | ||
name={iconLibrary[page.displayName]} | ||
/> | ||
); | ||
|
||
return ( | ||
<li key={page.displayName} data-depth={depthLevel}> | ||
{page.url ? ( | ||
<Link | ||
className={cx( | ||
{ | ||
[styles.isCurrentPage]: isCurrentPage, | ||
}, | ||
styles.navLink | ||
)} | ||
to={page.url} | ||
> | ||
<span> | ||
{headerIcon} | ||
{page.displayName} | ||
</span> | ||
{isCurrentPage && ( | ||
<FeatherIcon | ||
className={styles.currentPageIndicator} | ||
name="chevron-right" | ||
/> | ||
)} | ||
</Link> | ||
) : ( | ||
<button | ||
type="button" | ||
className={styles.navLink} | ||
onClick={() => setIsExpanded(!isExpanded)} | ||
onKeyPress={() => setIsExpanded(!isExpanded)} | ||
tabIndex={0} | ||
> | ||
{depthLevel > 0 && ( | ||
<FeatherIcon | ||
className={cx( | ||
{ [styles.isExpanded]: isExpanded }, | ||
styles.nestedChevron | ||
)} | ||
name="chevron-right" | ||
/> | ||
)} | ||
{headerIcon} | ||
{page.displayName} | ||
</button> | ||
)} | ||
{page.children && ( | ||
<ul | ||
className={cx(styles.nestedNav, { | ||
[styles.isExpanded]: isExpanded, | ||
})} | ||
> | ||
{renderNav(page.children, depthLevel + 1)} | ||
</ul> | ||
)} | ||
</li> | ||
); | ||
})} | ||
</Fragment> | ||
)); | ||
const filterPageNames = (pages, searchTerm, parent = []) => { | ||
return [ | ||
...new Set( | ||
pages.flatMap((page) => { | ||
if (page.children) { | ||
return filterPageNames(page.children, searchTerm, [ | ||
...parent, | ||
page.displayName, | ||
]); | ||
} else if (matchSearchString(page.displayName, searchTerm)) { | ||
return [...parent, page.displayName]; | ||
} else if (parent.some((el) => matchSearchString(el, searchTerm))) { | ||
return [...parent]; | ||
} | ||
}) | ||
), | ||
]; | ||
}; | ||
|
||
const Navigation = ({ className }) => { | ||
const Navigation = ({ className, searchTerm }) => { | ||
const searchTermSanitized = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
const filteredPageNames = | ||
searchTerm !== '' ? filterPageNames(pages, searchTermSanitized) : undefined; | ||
|
||
return ( | ||
<nav | ||
className={cx(styles.container, className)} | ||
role="navigation" | ||
aria-label="Navigation" | ||
> | ||
<ul className={styles.listNav}>{renderNav(pages)}</ul> | ||
<ul className={styles.listNav}> | ||
<NavigationItems | ||
searchTerm={searchTermSanitized} | ||
pages={pages} | ||
filteredPageNames={filteredPageNames} | ||
/> | ||
</ul> | ||
</nav> | ||
); | ||
}; | ||
|
||
Navigation.propTypes = { | ||
className: PropTypes.string, | ||
searchTerm: PropTypes.string, | ||
}; | ||
|
||
export default Navigation; |
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
Oops, something went wrong.