summaryrefslogtreecommitdiff
path: root/components/data-table/data-table-global-filter-detail.tsx
blob: 1db85bec669d6809471c8993751d5af403a9c634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use client"

import * as React from "react"
import { Input } from "@/components/ui/input"
import { useDebouncedCallback } from "@/hooks/use-debounced-callback"
import { type Table } from "@tanstack/react-table"

interface DataTableGlobalFilterDetailProps<TData> {
  table: Table<TData>
  placeholder?: string
  className?: string
}

/**
 * 디테일 시트 전용 글로벌 필터 - URL 상태와 연결되지 않는 독립적인 검색
 */
export function DataTableGlobalFilterDetail<TData>({
  table,
  placeholder = "Search...",
  className = "h-8 w-24 sm:w-40"
}: DataTableGlobalFilterDetailProps<TData>) {
  const [value, setValue] = React.useState("")

  // Debounced callback that sets the table's global filter
  const debouncedSetGlobalFilter = useDebouncedCallback((value: string) => {
    table.setGlobalFilter(value)
  }, 300)

  // When user types, update local value immediately,
  // then call the debounced function to update the table filter
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const val = e.target.value
    setValue(val)
    debouncedSetGlobalFilter(val)
  }

  return (
    <Input
      value={value}
      onChange={handleChange}
      placeholder={placeholder}
      className={className}
    />
  )
}