{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shadcn-studio/data-table-13","type":"registry:component","title":"Data Table 13","description":"editable data table with inline cell editing for text, status, and progress - supports row selection and live updates.","dependencies":["@tanstack/react-table"],"registryDependencies":["button","checkbox","input","select","table"],"files":[{"path":"src/registry/new-york/components/data-table/data-table-13.tsx","content":"'use client'\n\nimport { useEffect, useState } from 'react'\n\nimport type {\n  ColumnDef,\n  ColumnFiltersState,\n  RowData,\n  SortingState,\n  VisibilityState,\n  CellContext\n} from '@tanstack/react-table'\nimport {\n  flexRender,\n  getCoreRowModel,\n  getFilteredRowModel,\n  getSortedRowModel,\n  useReactTable\n} from '@tanstack/react-table'\n\nimport { Button } from '@/registry/new-york/ui/button'\nimport { Checkbox } from '@/registry/new-york/ui/checkbox'\nimport { Input } from '@/registry/new-york/ui/input'\nimport { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/registry/new-york/ui/select'\nimport { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/registry/new-york/ui/table'\n\n// Extend TanStack Table's meta interface\ndeclare module '@tanstack/react-table' {\n  // eslint-disable-next-line @typescript-eslint/no-unused-vars\n  interface TableMeta<TData extends RowData> {\n    updateData: (rowIndex: number, columnId: string, value: string | number) => void\n  }\n}\n\n// Sample data\nconst initialData: Person[] = [\n  {\n    id: '1',\n    firstName: 'John',\n    lastName: 'Doe',\n    email: 'john.doe@example.com',\n    status: 'active',\n    progress: 75\n  },\n  {\n    id: '2',\n    firstName: 'Jane',\n    lastName: 'Smith',\n    email: 'jane.smith@example.com',\n    status: 'inactive',\n    progress: 45\n  },\n  {\n    id: '3',\n    firstName: 'Bob',\n    lastName: 'Johnson',\n    email: 'bob.johnson@example.com',\n    status: 'active',\n    progress: 90\n  },\n  {\n    id: '4',\n    firstName: 'Alice',\n    lastName: 'Brown',\n    email: 'alice.brown@example.com',\n    status: 'pending',\n    progress: 60\n  },\n  {\n    id: '5',\n    firstName: 'Charlie',\n    lastName: 'Wilson',\n    email: 'charlie.wilson@example.com',\n    status: 'active',\n    progress: 80\n  }\n]\n\nexport type Person = {\n  id: string\n  firstName: string\n  lastName: string\n  email: string\n  status: 'active' | 'inactive' | 'pending'\n  progress: number\n}\n\n// Editable cell component for text inputs\nconst EditableTextCell = ({ getValue, row: { index }, column: { id }, table }: CellContext<Person, unknown>) => {\n  const initialValue = getValue() as string\n  const [value, setValue] = useState(initialValue)\n\n  const onBlur = () => {\n    table.options.meta?.updateData(index, id, value)\n  }\n\n  useEffect(() => {\n    setValue(initialValue)\n  }, [initialValue])\n\n  return (\n    <Input\n      value={value}\n      onChange={e => setValue(e.target.value)}\n      onBlur={onBlur}\n      className='focus-visible:ring-ring h-8 w-full bg-transparent px-2 focus-visible:ring-1'\n      aria-label='editable-text-input'\n    />\n  )\n}\n\n// Editable cell component for select inputs\nconst EditableSelectCell = ({ getValue, row: { index }, column: { id }, table }: CellContext<Person, unknown>) => {\n  const initialValue = getValue() as string\n\n  const handleValueChange = (newValue: string) => {\n    table.options.meta?.updateData(index, id, newValue)\n  }\n\n  return (\n    <Select value={initialValue} onValueChange={handleValueChange}>\n      <SelectTrigger\n        className='focus:ring-ring h-8 w-full bg-transparent px-2 focus:ring-1'\n        aria-label={`select-status-${id}`}\n      >\n        <SelectValue />\n      </SelectTrigger>\n      <SelectContent>\n        <SelectItem value='active'>Active</SelectItem>\n        <SelectItem value='inactive'>Inactive</SelectItem>\n        <SelectItem value='pending'>Pending</SelectItem>\n      </SelectContent>\n    </Select>\n  )\n}\n\n// Editable cell component for progress (0-100%)\nconst EditableProgressCell = ({ getValue, row: { index }, column: { id }, table }: CellContext<Person, unknown>) => {\n  const initialValue = getValue() as number\n  const [value, setValue] = useState(initialValue.toString())\n\n  const onBlur = () => {\n    const numValue = parseFloat(value)\n    const clampedValue = Math.max(0, Math.min(100, isNaN(numValue) ? initialValue : numValue))\n\n    table.options.meta?.updateData(index, id, clampedValue)\n  }\n\n  useEffect(() => {\n    setValue(initialValue.toString())\n  }, [initialValue])\n\n  return (\n    <div className='flex items-center space-x-2'>\n      <Input\n        type='number'\n        min='0'\n        max='100'\n        value={value}\n        onChange={e => setValue(e.target.value)}\n        onBlur={onBlur}\n        className='focus-visible:ring-ring h-8 w-20 bg-transparent px-2 focus-visible:ring-1'\n        aria-label='editable-progress-input'\n      />\n      <span className='text-muted-foreground text-sm'>%</span>\n    </div>\n  )\n}\n\n// Column definitions with editable cells\nexport const columns: ColumnDef<Person>[] = [\n  {\n    id: 'select',\n    header: ({ table }) => (\n      <Checkbox\n        checked={table.getIsAllPageRowsSelected() || (table.getIsSomePageRowsSelected() && 'indeterminate')}\n        onCheckedChange={value => table.toggleAllPageRowsSelected(!!value)}\n        aria-label='Select all'\n      />\n    ),\n    cell: ({ row }) => (\n      <Checkbox\n        checked={row.getIsSelected()}\n        onCheckedChange={value => row.toggleSelected(!!value)}\n        aria-label='Select row'\n      />\n    ),\n    enableSorting: false,\n    enableHiding: false\n  },\n  {\n    accessorKey: 'firstName',\n    header: 'First Name',\n    cell: EditableTextCell\n  },\n  {\n    accessorKey: 'lastName',\n    header: 'Last Name',\n    cell: EditableTextCell\n  },\n  {\n    accessorKey: 'email',\n    header: 'Email',\n    cell: EditableTextCell\n  },\n  {\n    accessorKey: 'status',\n    header: 'Status',\n    cell: EditableSelectCell\n  },\n  {\n    accessorKey: 'progress',\n    header: 'Progress',\n    cell: EditableProgressCell\n  }\n]\n\nconst EditableDataTableDemo = () => {\n  const [data, setData] = useState(() => [...initialData])\n  const [sorting, setSorting] = useState<SortingState>([])\n  const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([])\n  const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({})\n  const [rowSelection, setRowSelection] = useState({})\n\n  const refreshData = () => setData(() => [...initialData])\n\n  const table = useReactTable({\n    data,\n    columns,\n    onSortingChange: setSorting,\n    onColumnFiltersChange: setColumnFilters,\n    getCoreRowModel: getCoreRowModel(),\n    getSortedRowModel: getSortedRowModel(),\n    getFilteredRowModel: getFilteredRowModel(),\n    onColumnVisibilityChange: setColumnVisibility,\n    onRowSelectionChange: setRowSelection,\n    meta: {\n      updateData: (rowIndex, columnId, value) => {\n        setData(old =>\n          old.map((row, index) => {\n            if (index === rowIndex) {\n              return {\n                ...old[rowIndex]!,\n                [columnId]: value\n              }\n            }\n\n            return row\n          })\n        )\n      }\n    },\n    state: {\n      sorting,\n      columnFilters,\n      columnVisibility,\n      rowSelection\n    }\n  })\n\n  return (\n    <div className='w-full space-y-4'>\n      <div className='rounded-md border'>\n        <Table>\n          <TableHeader>\n            {table.getHeaderGroups().map(headerGroup => (\n              <TableRow key={headerGroup.id}>\n                {headerGroup.headers.map(header => {\n                  return (\n                    <TableHead key={header.id} colSpan={header.colSpan}>\n                      {header.isPlaceholder ? null : (\n                        <div>{flexRender(header.column.columnDef.header, header.getContext())}</div>\n                      )}\n                    </TableHead>\n                  )\n                })}\n              </TableRow>\n            ))}\n          </TableHeader>\n          <TableBody>\n            {table.getRowModel().rows?.length ? (\n              table.getRowModel().rows.map(row => (\n                <TableRow key={row.id} data-state={row.getIsSelected() && 'selected'}>\n                  {row.getVisibleCells().map(cell => (\n                    <TableCell key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell>\n                  ))}\n                </TableRow>\n              ))\n            ) : (\n              <TableRow>\n                <TableCell colSpan={columns.length} className='h-24 text-center'>\n                  No results.\n                </TableCell>\n              </TableRow>\n            )}\n          </TableBody>\n        </Table>\n      </div>\n\n      <div className='flex items-center justify-between gap-2 text-sm max-md:flex-col'>\n        <span className='text-muted-foreground'>{table.getRowModel().rows.length} rows total</span>\n        <Button variant='outline' size='sm' onClick={refreshData}>\n          Refresh Data\n        </Button>\n      </div>\n\n      <p className='text-muted-foreground mt-4 text-center text-sm'>\n        Editable data table - Click on cells to edit values\n      </p>\n    </div>\n  )\n}\n\nexport default EditableDataTableDemo\n","type":"registry:component","target":"components/shadcn-studio/data-table/data-table-13.tsx"}]}