{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shadcn-studio/data-table-07","type":"registry:component","title":"Data Table 7","description":"Data table with pinnable columns sticking to the left side or right side for easy access","dependencies":["lucide-react","@tanstack/react-table"],"registryDependencies":["button","dropdown-menu","table"],"files":[{"path":"src/registry/new-york/components/data-table/data-table-07.tsx","content":"'use client'\n\nimport type { CSSProperties } from 'react'\nimport { useState } from 'react'\n\nimport { ArrowLeftFromLineIcon, ArrowRightFromLineIcon, EllipsisIcon, PinOffIcon } from 'lucide-react'\n\nimport type { Column, ColumnDef, SortingState } from '@tanstack/react-table'\nimport { flexRender, getCoreRowModel, getSortedRowModel, useReactTable } from '@tanstack/react-table'\n\nimport { Button } from '@/registry/new-york/ui/button'\nimport {\n  DropdownMenu,\n  DropdownMenuContent,\n  DropdownMenuItem,\n  DropdownMenuTrigger\n} from '@/registry/new-york/ui/dropdown-menu'\nimport { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/registry/new-york/ui/table'\n\ntype Product = {\n  productId: number\n  productName: string\n  category: string\n  stockQuantity: number\n  price: number\n  supplier: string\n  discontinued: 'no' | 'yes'\n}\n\nconst data: Product[] = [\n  {\n    productId: 1,\n    productName: 'Apple iPhone 14',\n    category: 'Smartphones',\n    stockQuantity: 4550,\n    price: 1500,\n    supplier: 'Dixon Electronics',\n    discontinued: 'no'\n  },\n  {\n    productId: 2,\n    productName: 'Metal frame table',\n    category: 'Furniture',\n    stockQuantity: 150,\n    price: 540,\n    supplier: 'Milton Furniture',\n    discontinued: 'no'\n  },\n  {\n    productId: 3,\n    productName: 'Xiaomi A series',\n    category: 'electronics',\n    stockQuantity: 1500,\n    price: 2200,\n    supplier: 'Xiaomi Electronics',\n    discontinued: 'yes'\n  },\n  {\n    productId: 4,\n    productName: 'RC Monster Truck',\n    category: 'Toys',\n    stockQuantity: 10500,\n    price: 250,\n    supplier: 'Lego Toys',\n    discontinued: 'no'\n  },\n  {\n    productId: 5,\n    productName: 'Glass Water Bottle',\n    category: 'Kitchenware',\n    stockQuantity: 5503,\n    price: 69,\n    supplier: 'Kitchen Essentials',\n    discontinued: 'no'\n  },\n  {\n    productId: 6,\n    productName: 'BenQ Monitor 24',\n    category: 'Electronics',\n    stockQuantity: 600,\n    price: 1000,\n    supplier: 'BenQ Electronics',\n    discontinued: 'yes'\n  }\n]\n\nconst getPinningStyles = (column: Column<Product>): CSSProperties => {\n  const isPinned = column.getIsPinned()\n\n  return {\n    left: isPinned === 'left' ? `${column.getStart('left')}px` : undefined,\n    right: isPinned === 'right' ? `${column.getAfter('right')}px` : undefined,\n    position: isPinned ? 'sticky' : 'relative',\n    zIndex: isPinned ? 1 : 0\n  }\n}\n\nconst columns: ColumnDef<Product>[] = [\n  {\n    header: 'Product Name',\n    accessorKey: 'productName',\n    cell: ({ row }) => <div className='font-medium'>{row.getValue('productName')}</div>\n  },\n  {\n    header: 'Category',\n    accessorKey: 'category',\n    cell: ({ row }) => <div>{row.getValue('category')}</div>\n  },\n  {\n    header: 'Stock Quantity',\n    accessorKey: 'stockQuantity',\n    cell: ({ row }) => <div>{row.getValue('stockQuantity')}</div>\n  },\n  {\n    header: 'Price',\n    accessorKey: 'price',\n    cell: ({ row }) => {\n      const price = parseFloat(row.getValue('price'))\n\n      const formatted = new Intl.NumberFormat('en-US', {\n        style: 'currency',\n        currency: 'USD'\n      }).format(price)\n\n      return <div>{formatted}</div>\n    }\n  },\n  {\n    header: 'Supplier',\n    accessorKey: 'supplier',\n    cell: ({ row }) => <div>{row.getValue('supplier')}</div>\n  },\n  {\n    header: 'Discontinued',\n    accessorKey: 'discontinued',\n    cell: ({ row }) => <div className='capitalize'>{row.getValue('discontinued')}</div>\n  }\n]\n\nconst DataTablePinnableColumnDemo = () => {\n  const [sorting, setSorting] = useState<SortingState>([])\n\n  const table = useReactTable({\n    data,\n    columns,\n    getCoreRowModel: getCoreRowModel(),\n    getSortedRowModel: getSortedRowModel(),\n    onSortingChange: setSorting,\n    state: {\n      sorting\n    },\n    enableSortingRemoval: false\n  })\n\n  return (\n    <div className='w-full'>\n      <div className='rounded-md border'>\n        <Table className='[&_td]:border-border [&_th]:border-border border-separate border-spacing-0 [&_tfoot_td]:border-t [&_th]:border-b [&_tr]:border-none [&_tr:not(:last-child)_td]:border-b'>\n          <TableHeader>\n            {table.getHeaderGroups().map(headerGroup => (\n              <TableRow key={headerGroup.id}>\n                {headerGroup.headers.map(header => {\n                  const { column } = header\n                  const isPinned = column.getIsPinned()\n                  const isLastLeftPinned = isPinned === 'left' && column.getIsLastColumn('left')\n                  const isFirstRightPinned = isPinned === 'right' && column.getIsFirstColumn('right')\n\n                  return (\n                    <TableHead\n                      key={header.id}\n                      className='data-pinned:bg-muted/90 relative h-10 truncate data-pinned:backdrop-blur-xs [&:not([data-pinned]):has(+[data-pinned])_div.cursor-col-resize:last-child]:opacity-0 [&[data-last-col=left]_div.cursor-col-resize:last-child]:opacity-0 [&[data-pinned=right]:last-child_div.cursor-col-resize:last-child]:opacity-0'\n                      colSpan={header.colSpan}\n                      style={{ ...getPinningStyles(column) }}\n                      data-pinned={isPinned || undefined}\n                      data-last-col={isLastLeftPinned ? 'left' : isFirstRightPinned ? 'right' : undefined}\n                    >\n                      <div className='flex items-center justify-between gap-2'>\n                        <span className='truncate'>\n                          {header.isPlaceholder\n                            ? null\n                            : flexRender(header.column.columnDef.header, header.getContext())}\n                        </span>\n\n                        {!header.isPlaceholder &&\n                          header.column.getCanPin() &&\n                          (header.column.getIsPinned() ? (\n                            <Button\n                              size='icon'\n                              variant='ghost'\n                              className='-mr-1 size-7'\n                              onClick={() => header.column.pin(false)}\n                              aria-label={`Unpin ${header.column.columnDef.header as string} column`}\n                              title={`Unpin ${header.column.columnDef.header as string} column`}\n                            >\n                              <PinOffIcon className='opacity-60' aria-hidden='true' />\n                            </Button>\n                          ) : (\n                            <DropdownMenu>\n                              <DropdownMenuTrigger asChild>\n                                <Button\n                                  size='icon'\n                                  variant='ghost'\n                                  className='-mr-1 size-7'\n                                  aria-label={`Pin options for ${header.column.columnDef.header as string} column`}\n                                  title={`Pin options for ${header.column.columnDef.header as string} column`}\n                                >\n                                  <EllipsisIcon className='opacity-60' aria-hidden='true' />\n                                </Button>\n                              </DropdownMenuTrigger>\n                              <DropdownMenuContent align='end'>\n                                <DropdownMenuItem onClick={() => header.column.pin('left')}>\n                                  <ArrowLeftFromLineIcon size={16} className='opacity-60' aria-hidden='true' />\n                                  Stick to left\n                                </DropdownMenuItem>\n                                <DropdownMenuItem onClick={() => header.column.pin('right')}>\n                                  <ArrowRightFromLineIcon size={16} className='opacity-60' aria-hidden='true' />\n                                  Stick to right\n                                </DropdownMenuItem>\n                              </DropdownMenuContent>\n                            </DropdownMenu>\n                          ))}\n                      </div>\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                    const { column } = cell\n                    const isPinned = column.getIsPinned()\n                    const isLastLeftPinned = isPinned === 'left' && column.getIsLastColumn('left')\n                    const isFirstRightPinned = isPinned === 'right' && column.getIsFirstColumn('right')\n\n                    return (\n                      <TableCell\n                        key={cell.id}\n                        className='data-pinned:bg-background/90 truncate data-pinned:backdrop-blur-xs'\n                        style={{ ...getPinningStyles(column) }}\n                        data-pinned={isPinned || undefined}\n                        data-last-col={isLastLeftPinned ? 'left' : isFirstRightPinned ? 'right' : undefined}\n                      >\n                        {flexRender(cell.column.columnDef.cell, cell.getContext())}\n                      </TableCell>\n                    )\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      <p className='text-muted-foreground mt-4 text-center text-sm'>Data table with pinnable columns</p>\n    </div>\n  )\n}\n\nexport default DataTablePinnableColumnDemo\n","type":"registry:component","target":"components/shadcn-studio/data-table/data-table-07.tsx"}]}