1
1
import {
2
2
GridItem ,
3
+ Input ,
3
4
Heading ,
4
5
List ,
5
6
ListItem ,
7
+ Flex ,
6
8
SimpleGrid ,
7
9
Text ,
8
10
VStack ,
9
11
} from '@chakra-ui/react'
10
12
import type { GetStaticProps } from 'next'
11
-
12
13
import { ComponentOverviewItem } from 'components/component-overview-item'
13
14
import MDXLayout from 'layouts/mdx'
14
15
import { getGroupedComponents } from 'utils/contentlayer-utils'
15
16
import type { FrontmatterHeading } from 'src/types/frontmatter'
17
+ import { useState } from 'react'
16
18
17
19
type Component = {
18
20
title : string
@@ -32,6 +34,8 @@ type Props = {
32
34
}
33
35
34
36
export const ComponentsOverview = ( { categories, headings } : Props ) => {
37
+ const { filteredCategories, filterComponentsByTitle } =
38
+ useComponentFilter ( categories )
35
39
return (
36
40
< MDXLayout
37
41
frontmatter = { {
@@ -45,8 +49,14 @@ export const ComponentsOverview = ({ categories, headings }: Props) => {
45
49
Chakra UI provides prebuild components to help you build your projects
46
50
faster. Here is an overview of the component categories:
47
51
</ Text >
52
+ < Input
53
+ w = 'full'
54
+ size = 'md'
55
+ placeholder = 'Search overview'
56
+ onChange = { ( e ) => filterComponentsByTitle ( e . target . value ) }
57
+ />
48
58
< List w = 'full' spacing = { 12 } >
49
- { categories . map ( ( { title, components, id } ) => (
59
+ { filteredCategories . map ( ( { title, components, id } ) => (
50
60
< ListItem
51
61
key = { title }
52
62
display = 'flex'
@@ -112,4 +122,27 @@ export const getStaticProps: GetStaticProps<Props> = async () => {
112
122
}
113
123
}
114
124
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
+
115
148
export default ComponentsOverview
0 commit comments