BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/shadcn-table-library/pivot-table

Pivot Table

shadcn-table-library/pivot-table
FreeBlock

Dashboard-style analytics: pick which dimension becomes rows, which becomes columns, and how to aggregate (sum, average, count).

Install it

npx shadcn@latest add https://shad-table.dev/r/pivot-table.json

Source

src/components/pivot/data-table.tsxshad-table.dev/r/pivot-table.json · first 6 KB
1'use client'
2
3import { useMemo, useState } from 'react'
4import {
5 flexRender,
6 getCoreRowModel,
7 useReactTable,
8} from '@tanstack/react-table'
9import type { ColumnDef } from '@tanstack/react-table'
10
11import {
12 Table,
13 TableBody,
14 TableCell,
15 TableFooter,
16 TableHead,
17 TableHeader,
18 TableRow,
19} from '#/components/ui/table'
20import {
21 Select,
22 SelectContent,
23 SelectItem,
24 SelectTrigger,
25 SelectValue,
26} from '#/components/ui/select'
27import type { AggregationType, PivotRow, SalesRecord } from './pivot'
28import { pivotData } from './pivot'
29
30const DIMENSIONS: { value: keyof SalesRecord; label: string }[] = [
31 { value: 'region', label: 'Region' },
32 { value: 'quarter', label: 'Quarter' },
33 { value: 'channel', label: 'Channel' },
34]
35
36const AGGREGATIONS: { value: AggregationType; label: string }[] = [
37 { value: 'sum', label: 'Sum' },
38 { value: 'avg', label: 'Average' },
39 { value: 'count', label: 'Count' },
40]
41
42function formatValue(value: number, aggFn: AggregationType) {
43 if (aggFn === 'count') return value.toLocaleString('en-US')
44 return value.toLocaleString('en-US', {
45 style: 'currency',
46 currency: 'USD',
47 maximumFractionDigits: 0,
48 })
49}
50
51interface PivotTableProps {
52 data: SalesRecord[]
53}
54
55export function PivotTable({ data }: PivotTableProps) {
56 const [rowDim, setRowDim] = useState<keyof SalesRecord>('region')
57 const [colDim, setColDim] = useState<keyof SalesRecord>('quarter')
58 const [aggFn, setAggFn] = useState<AggregationType>('sum')
59
60 const { colValues, rows, columnTotals, grandTotal } = useMemo(
61 () => pivotData(data, rowDim, colDim, aggFn),
62 [data, rowDim, colDim, aggFn],
63 )
64
65 const rowLabel = DIMENSIONS.find((d) => d.value === rowDim)!.label
66
67 const columns = useMemo<ColumnDef<PivotRow>[]>(
68 () => [
69 {
70 id: 'rowValue',
71 header: rowLabel,
72 accessorKey: 'rowValue',
73 cell: ({ getValue }) => (
74 <span className="font-medium">{getValue() as string}</span>
75 ),
76 },
77 ...colValues.map(
78 (colValue): ColumnDef<PivotRow> => ({
79 id: colValue,
80 header: colValue,
81 accessorFn: (row) => row.cells[colValue],
82 cell: ({ getValue }) => formatValue(getValue() as number, aggFn),
83 }),
84 ),
85 {
86 id: 'total',
87 header: 'Total',
88 accessorKey: 'total',
89 cell: ({ getValue }) => (
90 <span className="font-medium">
91 {formatValue(getValue() as number, aggFn)}
92 </span>
93 ),
94// … truncated

What it pulls in

npm packages

  • @tanstack/react-table

Other registry items

  • table
  • select

Files it writes

  • src/components/pivot/pivot.ts
  • src/components/pivot/data.ts
  • src/components/pivot/data-table.tsx
  • src/components/pivot/index.tsx

Facts

Registry
shadcn-table-library
Type
registry:block
Access
Free
Files written
4
Licence
the repository states none
First indexed
2026-08-03
Last confirmed
today

Go to the source

shad-table.dev coros-hq/shadcn-table-library on GitHub Raw registry item This item as JSON

More from shadcn-table-library

All 16 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Comparison Tablecomparison-tableshadcn-table-libraryBlocksFree1 dep · 4 files
Data Tabledata-tableshadcn-table-libraryBlocksFree1 dep · 4 files
Density & Exportdensity-export-tableshadcn-table-libraryBlocksFree1 dep · 4 files
Editable Tableeditable-tableshadcn-table-libraryBlocksFree1 dep · 3 files
Grouped Tablegrouped-tableshadcn-table-libraryBlocksFree1 dep · 3 files
Heatmap Tableheatmap-tableshadcn-table-libraryBlocksFree1 dep · 4 files
Summary / KPI Tablekpi-tableshadcn-table-libraryBlocksFree1 dep · 5 files
Master-Detail Tablemaster-detail-tableshadcn-table-libraryBlocksFree1 dep · 4 files
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex