Skip to content

Commit

Permalink
feat(docz): allow for custom properties in useMenus hook (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaiF authored and rakannimer committed Nov 11, 2019
1 parent 87eea50 commit 20df733
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
17 changes: 6 additions & 11 deletions core/docz/src/hooks/useMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ import { Entry, MenuItem, doczState } from '../state'

const noMenu = (entry: Entry) => !entry.menu
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[]) =>
entries.filter(fromMenu(menu)).map(entryAsMenu)
entries.filter(fromMenu(menu))

const parseMenu = (entries: Entry[]) => (name: string) => ({
name,
Expand All @@ -26,9 +21,9 @@ const parseMenu = (entries: Entry[]) => (name: string) => ({
type Menus = MenuItem[]

const menusFromEntries = (entries: Entry[]) => {
const entriesWithoutMenu = entries.filter(noMenu).map(entryAsMenu) as any
const entriesWithoutMenu = entries.filter(noMenu)
const menus = flatArrFromObject(entries, 'menu').map(parseMenu(entries))
return unionBy('name', menus, entriesWithoutMenu)
return unionBy('name', menus, entriesWithoutMenu as any)
}

const parseItemStr = (item: MenuItem | string) =>
Expand Down Expand Up @@ -135,11 +130,11 @@ export const useMenus = (opts?: UseMenusParams) => {
const { entries, config } = useContext(doczState.context)
if (!entries) return null

const arr = entries.map(({ value }) => value)
const arr = entries.map(({ value }) => value) as Entry[]
const entriesMenu = menusFromEntries(arr)
const sorted = useMemo(() => {
const merged = mergeMenus(entriesMenu as MenuItem[], config.menu)
const result = sortMenus(merged, config.menu)
const merged = mergeMenus(entriesMenu as any[], config.menu)
const result = sortMenus(merged, config.menu) as MenuItem[]
return filterMenus(result, opts && opts.filter)
}, [entries, config])

Expand Down
1 change: 1 addition & 0 deletions core/docz/src/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface MenuItem {
menu?: MenuItem[]
order?: number
parent?: string
[key: string]: any
}

export type ThemeConfig = Record<string, any>
Expand Down

0 comments on commit 20df733

Please sign in to comment.