1 "use client"
2
3 import { Input } from "@/components/input"
4 import { Skeleton } from "@/components/skeleton"
5 import { clx } from "@/utils/clx"
6 import * as React from "react"
7
8 import { useDataTableContext } from "@/blocks/data-table/context/use-data-table-context"
9
10 interface DataTableSearchProps {
11 /**
12 * If true, the search input will be focused on mount.
13 */
14 autoFocus?: boolean
15 /**
16 * Additional classes to pass to the search input.
17 */
18 className?: string
19 /**
20 * The placeholder text to show in the search input.
21 */
22 placeholder?: string
23 }
24
25 /**
26 * This component adds a search input to the data table, allowing users
27 * to search through the table's data.
28 */
29 const DataTableSearch = (props: DataTableSearchProps) => {
30 const { className, ...rest } = props
31 const { instance } = useDataTableContext()
32
33 if (!instance.enableSearch) {
34 throw new Error(
35 "DataTable.Search was rendered but search is not enabled. Make sure to pass search to 'useDataTable'"
36 )
37 }
38
39 if (instance.showSkeleton) {
40 return <DataTableSearchSkeleton />
41 }
42
43 return (
44 <Input
45 size="small"
46 type="search"
47 value={instance.getSearch()}
48 onChange={(e) => instance.onSearchChange(e.target.value)}
49 className={clx(
50 {
51 "pr-[calc(15px+2px+8px)]": instance.isLoading,
52 },
53 className
54 )}
55 {...rest}
56 />
57 )
58 }
59 DataTableSearch.displayName = "DataTable.Search"
60
61 const DataTableSearchSkeleton = () => {
62 return <Skeleton className="h-7 w-[128px]" />
63 }
64
65 export { DataTableSearch }
66 export type { DataTableSearchProps }
67