Skip to content

Commit

Permalink
hide tab when no results
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherChudzicki committed Jul 3, 2024
1 parent d43fa26 commit 4ca183c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TabButtonList,
TabPanel,
styled,
useTabContext,
} from "ol-components"
import { ResourceCategoryEnum, LearningResourcesSearchResponse } from "api"

Expand Down Expand Up @@ -73,13 +74,22 @@ const ResourceCategoryTabList: React.FC<ResourceCategoryTabsProps> = ({
onTabChange,
className,
}) => {
const counts = resourceCategoryCounts(aggregations)
const tabContext = useTabContext()
const activeTab = tabContext?.value
const categoryCounts = resourceCategoryCounts(aggregations)
const allCount = aggregations?.resource_category
? (aggregations.resource_category || []).reduce((count, bucket) => {
count = count + bucket.doc_count
return count
}, 0)
: undefined
const counts = categoryCounts
? {
...categoryCounts,
all: allCount ?? 0,
}
: null
const countsLoaded = !!counts

return (
<TabsList
Expand All @@ -98,24 +108,24 @@ const ResourceCategoryTabList: React.FC<ResourceCategoryTabsProps> = ({
onTabChange?.()
}}
>
{tabs.map((t) => {
let count: number | undefined
if (t.name === "all") {
count = allCount
} else {
count =
counts && t.resource_category
? counts[t.resource_category] ?? 0
: undefined
}
return (
<TabButton
key={t.name}
value={t.name}
label={appendCount(t.label, count)}
/>
)
})}
{tabs
.map((tab) => ({
tab,
count: counts?.[tab.resource_category ?? "all"] ?? 0,
}))
.filter(({ count, tab }) => {
if (!countsLoaded || activeTab === tab.name) return true
return count
})
.map(({ tab, count }) => {
return (
<TabButton
key={tab.name}
value={tab.name}
label={countsLoaded ? appendCount(tab.label, count) : tab.label}
/>
)
})}
</TabsList>
)
}
Expand Down
2 changes: 1 addition & 1 deletion frontends/ol-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export {
TabButtonList,
} from "./components/TabButtons/TabButtonList"

export { default as TabContext } from "@mui/lab/TabContext"
export { default as TabContext, useTabContext } from "@mui/lab/TabContext"
export type { TabContextProps } from "@mui/lab/TabContext"
export { default as TabPanel } from "@mui/lab/TabPanel"
export type { TabPanelProps } from "@mui/lab/TabPanel"
Expand Down

0 comments on commit 4ca183c

Please sign in to comment.