|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import DataTable, { type ExpanderComponentProps, type TableColumn } from 'react-data-table-component'; |
| 3 | + |
| 4 | +interface Order { |
| 5 | + id: number; |
| 6 | + region: string; |
| 7 | + customer: string; |
| 8 | + amount: number; |
| 9 | +} |
| 10 | + |
| 11 | +interface RegionGroup { |
| 12 | + region: string; |
| 13 | + count: number; |
| 14 | + total: number; |
| 15 | + orders: Order[]; |
| 16 | +} |
| 17 | + |
| 18 | +const ORDERS: Order[] = [ |
| 19 | + { id: 1, region: 'West', customer: 'Acme Corp', amount: 4200 }, |
| 20 | + { id: 2, region: 'West', customer: 'Blue Harbor Inc', amount: 1850 }, |
| 21 | + { id: 3, region: 'West', customer: 'Candle Systems', amount: 3100 }, |
| 22 | + { id: 4, region: 'East', customer: 'Dynamo Energy', amount: 2600 }, |
| 23 | + { id: 5, region: 'East', customer: 'Evergreen Foods', amount: 5400 }, |
| 24 | + { id: 6, region: 'Central', customer: 'Frontier Retail', amount: 990 }, |
| 25 | + { id: 7, region: 'Central', customer: 'Granite Works', amount: 2200 }, |
| 26 | + { id: 8, region: 'Central', customer: 'Harbor Logistics', amount: 1475 }, |
| 27 | + { id: 9, region: 'Central', customer: 'Ironclad Freight', amount: 3050 }, |
| 28 | +]; |
| 29 | + |
| 30 | +// Group flat rows into per-region summary rows — a plain reduce, no groupBy API required. |
| 31 | +const groups: RegionGroup[] = Object.values( |
| 32 | + ORDERS.reduce<Record<string, RegionGroup>>((acc, order) => { |
| 33 | + const g = (acc[order.region] ??= { region: order.region, count: 0, total: 0, orders: [] }); |
| 34 | + g.count += 1; |
| 35 | + g.total += order.amount; |
| 36 | + g.orders.push(order); |
| 37 | + return acc; |
| 38 | + }, {}), |
| 39 | +); |
| 40 | + |
| 41 | +const columns: TableColumn<RegionGroup>[] = [ |
| 42 | + { id: 'region', name: 'Region', selector: r => r.region, sortable: true, style: { fontWeight: 600 } }, |
| 43 | + { id: 'count', name: 'Orders', selector: r => r.count, sortable: true, right: true, width: '110px' }, |
| 44 | + { |
| 45 | + id: 'total', |
| 46 | + name: 'Total', |
| 47 | + selector: r => r.total, |
| 48 | + sortable: true, |
| 49 | + right: true, |
| 50 | + width: '130px', |
| 51 | + format: r => `$${r.total.toLocaleString()}`, |
| 52 | + footer: (rows: RegionGroup[]) => `$${rows.reduce((sum, r) => sum + r.total, 0).toLocaleString()}`, |
| 53 | + }, |
| 54 | +]; |
| 55 | + |
| 56 | +function RegionOrders({ data }: ExpanderComponentProps<RegionGroup>) { |
| 57 | + return ( |
| 58 | + <div style={{ padding: '10px 32px' }} className="bg-gray-50"> |
| 59 | + <div className="space-y-1.5"> |
| 60 | + {data.orders.map(order => ( |
| 61 | + <div |
| 62 | + key={order.id} |
| 63 | + className="flex items-center justify-between text-sm bg-white border border-gray-200 rounded-md px-3 py-2" |
| 64 | + > |
| 65 | + <span className="text-gray-700">{order.customer}</span> |
| 66 | + <span className="font-medium text-gray-900">${order.amount.toLocaleString()}</span> |
| 67 | + </div> |
| 68 | + ))} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export default function RowGroupingDemo() { |
| 75 | + const [allExpanded, setAllExpanded] = useState(false); |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className="space-y-2"> |
| 79 | + <div className="flex justify-end"> |
| 80 | + <button |
| 81 | + onClick={() => setAllExpanded(v => !v)} |
| 82 | + className="px-2.5 py-1 text-xs font-medium rounded-md border border-gray-200 text-gray-600 hover:border-gray-300" |
| 83 | + > |
| 84 | + {allExpanded ? 'Collapse all' : 'Expand all'} |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + <div className="rounded-xl border border-gray-200 overflow-hidden"> |
| 88 | + <DataTable |
| 89 | + columns={columns} |
| 90 | + data={groups} |
| 91 | + keyField="region" |
| 92 | + expandableRows |
| 93 | + expandableRowsComponent={RegionOrders} |
| 94 | + expandableRowExpanded={() => allExpanded} |
| 95 | + highlightOnHover |
| 96 | + dense |
| 97 | + /> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments