From 17671e4751db01a0b976ce9dc495bde5a5300544 Mon Sep 17 00:00:00 2001 From: Cayla Hamann Date: Sun, 21 Jun 2020 21:27:05 -0400 Subject: [PATCH] chore: refactor --- src/components/Navigation.js | 49 +++++++++++++++++++++++---- src/components/Navigation.module.scss | 14 ++++++++ src/utils/matchSearchString.js | 5 +++ 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/utils/matchSearchString.js diff --git a/src/components/Navigation.js b/src/components/Navigation.js index d74fe789a..cdd2a1395 100644 --- a/src/components/Navigation.js +++ b/src/components/Navigation.js @@ -5,11 +5,12 @@ import cx from 'classnames'; import { BreadcrumbContext } from './BreadcrumbContext'; import FeatherIcon from './FeatherIcon'; 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) => { +const renderNav = (pages, searches, depthLevel = 0) => { const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName); const groupedPages = pages.reduce((groups, page) => { @@ -28,15 +29,29 @@ const renderNav = (pages, depthLevel = 0) => { )} {pages.map((page) => { const [isExpanded, setIsExpanded] = useState( - crumbs.length === depthLevel || crumbs.includes(page.displayName) + searches !== undefined || + crumbs.includes(page.displayName) || + crumbs.length === depthLevel ); + const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName; return (
  • {page.url ? ( @@ -65,7 +80,7 @@ const renderNav = (pages, depthLevel = 0) => { [styles.isExpanded]: isExpanded, })} > - {renderNav(page.children, depthLevel + 1)} + {renderNav(page.children, searches, depthLevel + 1)} )}
  • @@ -75,20 +90,42 @@ const renderNav = (pages, depthLevel = 0) => { )); }; -const Navigation = ({ className }) => { +const searchPages = (pages, searchTerm, parent = []) => { + return [ + ...new Set( + pages.flatMap((page) => { + if (page.children) { + return searchPages(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, searchTerm }) => { + const searches = + searchTerm !== '' ? searchPages(pages, searchTerm) : undefined; return ( ); }; Navigation.propTypes = { className: PropTypes.string, + searchTerm: PropTypes.string, }; export default Navigation; diff --git a/src/components/Navigation.module.scss b/src/components/Navigation.module.scss index 9bb71c303..f747b3129 100644 --- a/src/components/Navigation.module.scss +++ b/src/components/Navigation.module.scss @@ -64,3 +64,17 @@ button.navLink { .isCurrentPage { font-weight: bold; } + +.isNotSearch { + display: none; +} + +.isSearch { + display: block; +} + +.isBeingSearched { + ul { + display: block; + } +} diff --git a/src/utils/matchSearchString.js b/src/utils/matchSearchString.js new file mode 100644 index 000000000..0b434680c --- /dev/null +++ b/src/utils/matchSearchString.js @@ -0,0 +1,5 @@ +const matchSearchString = (str, searchTerm) => { + return str.toLowerCase().includes(searchTerm.toLowerCase()); +}; + +export default matchSearchString;