From aeeb311d88825eaa05a99868a00ae2b0e88d52fd Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Mon, 11 Mar 2019 21:31:27 -0300 Subject: [PATCH] feat(docz): add filter option for useMenus --- core/docz/src/hooks/useMenus.ts | 17 +++++++++++++++-- core/docz/src/state.tsx | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/docz/src/hooks/useMenus.ts b/core/docz/src/hooks/useMenus.ts index e1a051c29..b93a4ce85 100644 --- a/core/docz/src/hooks/useMenus.ts +++ b/core/docz/src/hooks/useMenus.ts @@ -12,6 +12,7 @@ const fromMenu = (menu: string) => (entry: Entry) => entry.menu === menu const entryAsMenu = (entry: Entry) => ({ name: entry.name, route: entry.route, + parent: entry.parent, }) const entriesOfMenu = (menu: string, entries: Entry[]) => @@ -35,10 +36,10 @@ const parseItemStr = (item: MenuItem | string) => const normalize = (item: MenuItem | string): MenuItem => { const selected = parseItemStr(item) as MenuItem - return { ...selected, id: selected.id || ulid(), + parent: get('parent', selected) || get('parent', item), menu: Array.isArray(selected.menu) ? selected.menu.map(normalize) : selected.menu, @@ -114,8 +115,19 @@ const search = (val: string, menu: MenuItem[]) => { return match(flattenedDeduplicated, val, { keys: ['name'] }) } +type FilterFn = (item: MenuItem) => boolean + +const filterMenus = (items: MenuItem[], filter?: FilterFn) => { + if (!filter) return items + return items.filter(filter).map(item => { + if (!item.menu) return item + return { ...item, menu: item.menu.filter(filter) } + }) +} + export interface UseMenusParams { query?: string + filter?: FilterFn } export const useMenus = (opts?: UseMenusParams) => { @@ -127,7 +139,8 @@ export const useMenus = (opts?: UseMenusParams) => { const entriesMenu = menusFromEntries(arr) const sorted = useMemo(() => { const merged = mergeMenus(entriesMenu as MenuItem[], config.menu) - return sortMenus(merged, config.menu) + const result = sortMenus(merged, config.menu) + return filterMenus(result, opts && opts.filter) }, [entries, config]) return query && query.length > 0 diff --git a/core/docz/src/state.tsx b/core/docz/src/state.tsx index 0eab1e3a4..0209129fe 100644 --- a/core/docz/src/state.tsx +++ b/core/docz/src/state.tsx @@ -27,6 +27,7 @@ export interface MenuItem { href?: string menu?: MenuItem[] order?: number + parent?: string } export type ThemeConfig = Record