Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 0ed8e79

Browse files
Merge pull request #1571 from Riley1101/feat/search_overview
2 parents f8041b4 + 7f65b86 commit 0ed8e79

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

pages/docs/components/index.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import {
22
GridItem,
3+
Input,
34
Heading,
45
List,
56
ListItem,
7+
Flex,
68
SimpleGrid,
79
Text,
810
VStack,
911
} from '@chakra-ui/react'
1012
import type { GetStaticProps } from 'next'
11-
1213
import { ComponentOverviewItem } from 'components/component-overview-item'
1314
import MDXLayout from 'layouts/mdx'
1415
import { getGroupedComponents } from 'utils/contentlayer-utils'
1516
import type { FrontmatterHeading } from 'src/types/frontmatter'
17+
import { useState } from 'react'
1618

1719
type Component = {
1820
title: string
@@ -32,6 +34,8 @@ type Props = {
3234
}
3335

3436
export const ComponentsOverview = ({ categories, headings }: Props) => {
37+
const { filteredCategories, filterComponentsByTitle } =
38+
useComponentFilter(categories)
3539
return (
3640
<MDXLayout
3741
frontmatter={{
@@ -45,8 +49,14 @@ export const ComponentsOverview = ({ categories, headings }: Props) => {
4549
Chakra UI provides prebuild components to help you build your projects
4650
faster. Here is an overview of the component categories:
4751
</Text>
52+
<Input
53+
w='full'
54+
size='md'
55+
placeholder='Search overview'
56+
onChange={(e) => filterComponentsByTitle(e.target.value)}
57+
/>
4858
<List w='full' spacing={12}>
49-
{categories.map(({ title, components, id }) => (
59+
{filteredCategories.map(({ title, components, id }) => (
5060
<ListItem
5161
key={title}
5262
display='flex'
@@ -112,4 +122,27 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
112122
}
113123
}
114124

125+
const useComponentFilter = (categories: Category[]) => {
126+
const [filteredCategories, setFilteredCategories] =
127+
useState<Category[]>(categories)
128+
129+
const filterComponentsByTitle = (searchText: string) => {
130+
const filtered: Category[] = []
131+
categories.forEach((category) => {
132+
const matchingComponents = category.components.filter((component) =>
133+
component.title.toLowerCase().includes(searchText.toLowerCase()),
134+
)
135+
if (matchingComponents.length > 0) {
136+
const filteredCategory = { ...category, components: matchingComponents }
137+
filtered.push(filteredCategory)
138+
}
139+
})
140+
setFilteredCategories(filtered)
141+
}
142+
return {
143+
filteredCategories,
144+
filterComponentsByTitle,
145+
}
146+
}
147+
115148
export default ComponentsOverview

0 commit comments

Comments
 (0)