{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shadcn-studio/data-table-10","type":"registry:component","title":"Data Table 10","description":"Data table with pagination, rows selection, and sorting system","dependencies":["lucide-react","@tanstack/react-table"],"registryDependencies":["badge","button","checkbox","label","pagination","select","table","utils"],"files":[{"path":"src/registry/new-york/components/data-table/data-table-10.tsx","content":"'use client'\n\nimport { useEffect, useId, useState } from 'react'\n\nimport {\n  ChevronDownIcon,\n  ChevronFirstIcon,\n  ChevronLastIcon,\n  ChevronLeftIcon,\n  ChevronRightIcon,\n  ChevronUpIcon\n} from 'lucide-react'\n\nimport type { ColumnDef, PaginationState, SortingState } from '@tanstack/react-table'\nimport {\n  flexRender,\n  getCoreRowModel,\n  getPaginationRowModel,\n  getSortedRowModel,\n  useReactTable\n} from '@tanstack/react-table'\n\nimport { Badge } from '@/registry/new-york/ui/badge'\nimport { Button } from '@/registry/new-york/ui/button'\nimport { Checkbox } from '@/registry/new-york/ui/checkbox'\nimport { Label } from '@/registry/new-york/ui/label'\nimport { Pagination, PaginationContent, PaginationItem } from '@/registry/new-york/ui/pagination'\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\nimport { cn } from '@/registry/new-york/lib/utils'\n\ntype Item = {\n  product_name: string\n  price: string\n  availability: 'In Stock' | 'Out of Stock' | 'Limited'\n}\n\nconst columns: ColumnDef<Item>[] = [\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    size: 28,\n    enableSorting: false\n  },\n  {\n    header: 'Product Name',\n    accessorKey: 'product_name',\n    cell: ({ row }) => <div className='font-medium'>{row.getValue('product_name')}</div>\n  },\n  {\n    header: 'Price',\n    accessorKey: 'price',\n    cell: ({ row }) => <div className='font-medium'>{row.getValue('price')}</div>\n  },\n  {\n    header: 'Availability',\n    accessorKey: 'availability',\n    cell: ({ row }) => {\n      const availability = row.getValue('availability') as string\n\n      const styles = {\n        'In Stock':\n          'bg-green-600/10 text-green-600 focus-visible:ring-green-600/20 dark:bg-green-400/10 dark:text-green-400 dark:focus-visible:ring-green-400/40 [a&]:hover:bg-green-600/5 dark:[a&]:hover:bg-green-400/5',\n        'Out of Stock':\n          'bg-destructive/10 [a&]:hover:bg-destructive/5 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 text-destructive',\n        Limited:\n          'bg-amber-600/10 text-amber-600 focus-visible:ring-amber-600/20 dark:bg-amber-400/10 dark:text-amber-400 dark:focus-visible:ring-amber-400/40 [a&]:hover:bg-amber-600/5 dark:[a&]:hover:bg-amber-400/5'\n      }[availability]\n\n      return <Badge className={styles}>{row.getValue('availability')}</Badge>\n    }\n  }\n]\n\nconst PaginatedDataTableDemo = () => {\n  const id = useId()\n\n  const [pagination, setPagination] = useState<PaginationState>({\n    pageIndex: 0,\n    pageSize: 5\n  })\n\n  const [sorting, setSorting] = useState<SortingState>([\n    {\n      id: 'product_name',\n      desc: false\n    }\n  ])\n\n  const [data, setData] = useState<Item[]>([])\n\n  useEffect(() => {\n    async function fetchPosts() {\n      const res = await fetch('https://cdn.jsdelivr.net/gh/themeselection/fy-assets/assets/json/mobile-stock.json')\n\n      if (!res.ok) {\n        throw new Error('Failed to fetch data')\n      }\n\n      const items = await res.json()\n\n      const data = await items.data\n\n      setData([...data, ...data])\n    }\n\n    fetchPosts()\n  }, [])\n\n  const table = useReactTable({\n    data,\n    columns,\n    getCoreRowModel: getCoreRowModel(),\n    getSortedRowModel: getSortedRowModel(),\n    onSortingChange: setSorting,\n    enableSortingRemoval: false,\n    getPaginationRowModel: getPaginationRowModel(),\n    onPaginationChange: setPagination,\n    state: {\n      sorting,\n      pagination\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} className='hover:bg-transparent'>\n                {headerGroup.headers.map(header => {\n                  return (\n                    <TableHead key={header.id} style={{ width: `${header.getSize()}px` }} className='h-11'>\n                      {header.isPlaceholder ? null : header.column.getCanSort() ? (\n                        <div\n                          className={cn(\n                            header.column.getCanSort() &&\n                              'flex h-full cursor-pointer items-center justify-between gap-2 select-none'\n                          )}\n                          onClick={header.column.getToggleSortingHandler()}\n                          onKeyDown={e => {\n                            if (header.column.getCanSort() && (e.key === 'Enter' || e.key === ' ')) {\n                              e.preventDefault()\n                              header.column.getToggleSortingHandler()?.(e)\n                            }\n                          }}\n                          tabIndex={header.column.getCanSort() ? 0 : undefined}\n                        >\n                          {flexRender(header.column.columnDef.header, header.getContext())}\n                          {{\n                            asc: <ChevronUpIcon className='shrink-0 opacity-60' size={16} aria-hidden='true' />,\n                            desc: <ChevronDownIcon className='shrink-0 opacity-60' size={16} aria-hidden='true' />\n                          }[header.column.getIsSorted() as string] ?? null}\n                        </div>\n                      ) : (\n                        flexRender(header.column.columnDef.header, header.getContext())\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-8'>\n        <div className='flex items-center gap-3'>\n          <Label htmlFor={id} className='max-sm:sr-only'>\n            Rows per page\n          </Label>\n          <Select\n            value={table.getState().pagination.pageSize.toString()}\n            onValueChange={value => {\n              table.setPageSize(Number(value))\n            }}\n          >\n            <SelectTrigger id={id} className='w-fit whitespace-nowrap'>\n              <SelectValue placeholder='Select number of results' />\n            </SelectTrigger>\n            <SelectContent className='[&_*[role=option]]:pr-8 [&_*[role=option]]:pl-2 [&_*[role=option]>span]:right-2 [&_*[role=option]>span]:left-auto'>\n              {[5, 10, 25, 50].map(pageSize => (\n                <SelectItem key={pageSize} value={pageSize.toString()}>\n                  {pageSize}\n                </SelectItem>\n              ))}\n            </SelectContent>\n          </Select>\n        </div>\n\n        <div className='text-muted-foreground flex grow justify-end text-sm whitespace-nowrap'>\n          <p className='text-muted-foreground text-sm whitespace-nowrap' aria-live='polite'>\n            <span className='text-foreground'>\n              {table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1}-\n              {Math.min(\n                Math.max(\n                  table.getState().pagination.pageIndex * table.getState().pagination.pageSize +\n                    table.getState().pagination.pageSize,\n                  0\n                ),\n                table.getRowCount()\n              )}\n            </span>{' '}\n            of <span className='text-foreground'>{table.getRowCount().toString()}</span>\n          </p>\n        </div>\n\n        <div>\n          <Pagination>\n            <PaginationContent>\n              <PaginationItem>\n                <Button\n                  size='icon'\n                  variant='outline'\n                  className='disabled:pointer-events-none disabled:opacity-50'\n                  onClick={() => table.firstPage()}\n                  disabled={!table.getCanPreviousPage()}\n                  aria-label='Go to first page'\n                >\n                  <ChevronFirstIcon aria-hidden='true' />\n                </Button>\n              </PaginationItem>\n\n              <PaginationItem>\n                <Button\n                  size='icon'\n                  variant='outline'\n                  className='disabled:pointer-events-none disabled:opacity-50'\n                  onClick={() => table.previousPage()}\n                  disabled={!table.getCanPreviousPage()}\n                  aria-label='Go to previous page'\n                >\n                  <ChevronLeftIcon aria-hidden='true' />\n                </Button>\n              </PaginationItem>\n\n              <PaginationItem>\n                <Button\n                  size='icon'\n                  variant='outline'\n                  className='disabled:pointer-events-none disabled:opacity-50'\n                  onClick={() => table.nextPage()}\n                  disabled={!table.getCanNextPage()}\n                  aria-label='Go to next page'\n                >\n                  <ChevronRightIcon aria-hidden='true' />\n                </Button>\n              </PaginationItem>\n\n              <PaginationItem>\n                <Button\n                  size='icon'\n                  variant='outline'\n                  className='disabled:pointer-events-none disabled:opacity-50'\n                  onClick={() => table.lastPage()}\n                  disabled={!table.getCanNextPage()}\n                  aria-label='Go to last page'\n                >\n                  <ChevronLastIcon aria-hidden='true' />\n                </Button>\n              </PaginationItem>\n            </PaginationContent>\n          </Pagination>\n        </div>\n      </div>\n      <p className='text-muted-foreground mt-4 text-center text-sm'>Paginated data table </p>\n    </div>\n  )\n}\n\nexport default PaginatedDataTableDemo\n","type":"registry:component","target":"components/shadcn-studio/data-table/data-table-10.tsx"}]}