{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "data-table",
  "type": "registry:component",
  "title": "Data table",
  "description": "Search, sort, and paginate. Bring your own data.",
  "categories": [
    "tables-&-lists"
  ],
  "dependencies": [
    "@tabler/icons-react"
  ],
  "registryDependencies": [
    "button",
    "input",
    "table"
  ],
  "files": [
    {
      "path": "src/registry/overtrue/data-table.tsx",
      "type": "registry:component",
      "target": "components/overtrue/data-table.tsx",
      "content": "\"use client\";\nimport { useMemo, useRef, useState, type ReactNode } from \"react\";\nimport {\n  IconArrowDown as ArrowDown,\n  IconArrowUp as ArrowUp,\n  IconArrowsSort as ArrowsSort,\n  IconChevronLeft as ChevronLeft,\n  IconChevronRight as ChevronRight,\n} from \"@tabler/icons-react\";\nimport { Button } from \"@/components/ui/button\";\nimport { SearchField } from \"./search-field\";\nimport {\n  Table,\n  TableBody,\n  TableCell,\n  TableHead,\n  TableHeader,\n  TableRow,\n} from \"@/components/ui/table\";\nexport interface DataColumn<T> {\n  key: string;\n  label: string;\n  value: (row: T) => string | number;\n  render?: (row: T) => ReactNode;\n  sortable?: boolean;\n}\nexport function DataTable<T>({\n  rows,\n  columns,\n  getRowId,\n  title = \"Records\",\n  pageSize = 5,\n}: {\n  rows: T[];\n  columns: DataColumn<T>[];\n  getRowId: (row: T) => string;\n  title?: string;\n  pageSize?: number;\n}) {\n  const searchRef = useRef<HTMLInputElement>(null);\n  const [query, setQuery] = useState(\"\");\n  const [sort, setSort] = useState<{ key: string; direction: 1 | -1 } | null>(\n    null,\n  );\n  const [page, setPage] = useState(0);\n  const size = Number.isFinite(pageSize)\n    ? Math.max(1, Math.floor(pageSize) || 5)\n    : 5;\n  const filtered = useMemo(() => {\n    const found = rows.filter((row) =>\n      columns.some((column) =>\n        String(column.value(row))\n          .toLowerCase()\n          .includes(query.trim().toLowerCase()),\n      ),\n    );\n    const column = columns.find((item) => item.key === sort?.key);\n    if (column && sort)\n      found.sort((a, b) => {\n        const x = column.value(a),\n          y = column.value(b);\n        return (\n          (typeof x === \"number\" && typeof y === \"number\"\n            ? x - y\n            : String(x).localeCompare(String(y))) * sort.direction\n        );\n      });\n    return found;\n  }, [rows, columns, query, sort]);\n  const lastPage = Math.max(0, Math.ceil(filtered.length / size) - 1);\n  const safePage = Math.min(page, lastPage);\n  return (\n    <section\n      data-slot=\"data-table\"\n      className=\"min-w-0 overflow-hidden rounded-lg border bg-card shadow-none\"\n      aria-label={title}\n    >\n      <div className=\"flex flex-wrap items-center justify-between gap-3 border-b p-4\">\n        <h2 className=\"text-sm font-semibold text-card-foreground\">{title}</h2>\n        <SearchField\n          ref={searchRef}\n          label={`Search ${title.toLowerCase()}`}\n          value={query}\n          onValueChange={(value) => {\n            setQuery(value);\n            setPage(0);\n          }}\n          placeholder=\"Search records…\"\n          containerClassName=\"w-full max-w-56\"\n        />\n      </div>\n      <div className=\"overflow-x-auto\">\n        <Table className=\"whitespace-nowrap\">\n          <TableHeader>\n            <TableRow>\n              {columns.map((column) => (\n                <TableHead\n                  key={column.key}\n                  aria-sort={\n                    sort?.key === column.key\n                      ? sort.direction === 1\n                        ? \"ascending\"\n                        : \"descending\"\n                      : undefined\n                  }\n                  className=\"h-10 bg-muted/30 px-4 text-xs font-medium\"\n                >\n                  {column.sortable ? (\n                    <button\n                      type=\"button\"\n                      onClick={() => {\n                        setSort({\n                          key: column.key,\n                          direction:\n                            sort?.key === column.key && sort.direction === 1\n                              ? -1\n                              : 1,\n                        });\n                        setPage(0);\n                      }}\n                      className=\"-ms-2 flex items-center gap-1.5 rounded-md px-2 py-2 hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                    >\n                      {column.label}\n                      {sort?.key === column.key ? (\n                        sort.direction === 1 ? (\n                          <ArrowUp aria-hidden=\"true\" className=\"size-3\" />\n                        ) : (\n                          <ArrowDown aria-hidden=\"true\" className=\"size-3\" />\n                        )\n                      ) : (\n                        <ArrowsSort\n                          aria-hidden=\"true\"\n                          className=\"size-3 text-muted-foreground\"\n                        />\n                      )}\n                    </button>\n                  ) : (\n                    column.label\n                  )}\n                </TableHead>\n              ))}\n            </TableRow>\n          </TableHeader>\n          <TableBody>\n            {filtered\n              .slice(safePage * size, (safePage + 1) * size)\n              .map((row) => (\n                <TableRow key={getRowId(row)}>\n                  {columns.map((column) => (\n                    <TableCell key={column.key} className=\"px-4 py-3.5 text-sm\">\n                      {column.render ? column.render(row) : column.value(row)}\n                    </TableCell>\n                  ))}\n                </TableRow>\n              ))}\n          </TableBody>\n        </Table>\n      </div>\n      {!filtered.length && (\n        <div role=\"status\" className=\"px-4 py-10 text-center\">\n          <p className=\"m-0 text-sm font-medium\">\n            {query.trim() ? \"No matching records.\" : \"No records yet.\"}\n          </p>\n          <p className=\"m-0 mt-1 text-xs leading-5 text-muted-foreground\">\n            {query.trim()\n              ? \"Try another search or clear it to see all records.\"\n              : \"Records will appear here when they are available.\"}\n          </p>\n          {query.trim() && (\n            <Button\n              type=\"button\"\n              variant=\"outline\"\n              size=\"sm\"\n              className=\"mt-4 shadow-none\"\n              onClick={() => {\n                setQuery(\"\");\n                setPage(0);\n                searchRef.current?.focus();\n              }}\n            >\n              Clear search\n            </Button>\n          )}\n        </div>\n      )}\n      <div className=\"flex flex-wrap items-center justify-between gap-3 border-t px-4 py-3\">\n        <p className=\"text-xs text-muted-foreground\" aria-live=\"polite\">\n          {filtered.length\n            ? `${safePage * size + 1}–${Math.min((safePage + 1) * size, filtered.length)}`\n            : \"0\"}{\" \"}\n          of {filtered.length} records\n        </p>\n        <div className=\"flex gap-1\">\n          <Button\n            type=\"button\"\n            variant=\"outline\"\n            size=\"icon\"\n            className=\"size-8 border-border bg-card text-foreground shadow-none hover:bg-muted\"\n            aria-label=\"Previous page\"\n            disabled={!safePage}\n            onClick={() => setPage(safePage - 1)}\n          >\n            <ChevronLeft className=\"size-3.5\" />\n          </Button>\n          <Button\n            type=\"button\"\n            variant=\"outline\"\n            size=\"icon\"\n            className=\"size-8 border-border bg-card text-foreground shadow-none hover:bg-muted\"\n            aria-label=\"Next page\"\n            disabled={safePage >= lastPage}\n            onClick={() => setPage(safePage + 1)}\n          >\n            <ChevronRight className=\"size-3.5\" />\n          </Button>\n        </div>\n      </div>\n    </section>\n  );\n}\n"
    },
    {
      "path": "src/registry/overtrue/search-field.tsx",
      "type": "registry:component",
      "target": "components/overtrue/search-field.tsx",
      "content": "\"use client\";\nimport { useId, useImperativeHandle, useRef, type ComponentProps } from \"react\";\nimport { IconSearch, IconX } from \"@tabler/icons-react\";\nimport { Input } from \"@/components/ui/input\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface SearchFieldProps extends Omit<\n  ComponentProps<typeof Input>,\n  \"type\" | \"value\" | \"defaultValue\" | \"onChange\"\n> {\n  label: string;\n  value: string;\n  onValueChange: (value: string) => void;\n  containerClassName?: string;\n}\n/** Controlled search input. Filtering and debouncing belong to the caller. */\nexport function SearchField({\n  label,\n  value,\n  onValueChange,\n  id,\n  ref,\n  containerClassName,\n  className,\n  placeholder = \"Search…\",\n  ...props\n}: SearchFieldProps) {\n  const generatedId = useId();\n  const inputId = id ?? generatedId;\n  const inputRef = useRef<HTMLInputElement>(null);\n  useImperativeHandle(ref, () => inputRef.current!, []);\n  return (\n    <div\n      data-slot=\"search-field\"\n      className={cn(\"relative min-w-0\", containerClassName)}\n    >\n      <label htmlFor={inputId} className=\"sr-only\">\n        {label}\n      </label>\n      <IconSearch\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute start-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground\"\n      />\n      <Input\n        {...props}\n        ref={inputRef}\n        id={inputId}\n        type=\"search\"\n        value={value}\n        onChange={(event) => onValueChange(event.target.value)}\n        placeholder={placeholder}\n        className={cn(\n          \"h-10 w-full rounded-md border-input bg-card dark:bg-card ps-9 pe-9 text-base text-foreground shadow-xs placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/20 sm:text-sm [&::-webkit-search-cancel-button]:appearance-none\",\n          className,\n        )}\n      />\n      {value && !props.readOnly && (\n        <button\n          type=\"button\"\n          aria-label={`Clear ${label.toLowerCase()}`}\n          disabled={props.disabled}\n          className=\"absolute end-1 top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded-sm text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50\"\n          onClick={() => {\n            onValueChange(\"\");\n            inputRef.current?.focus();\n          }}\n        >\n          <IconX aria-hidden=\"true\" className=\"size-3.5\" />\n        </button>\n      )}\n    </div>\n  );\n}\n"
    }
  ],
  "docs": "Documentation: https://ui.overtrue.xyz/components/data-table. Requires an initialized shadcn/ui project with Tailwind CSS v4."
}
