"use client"; import type { ColumnDef, Row, SortingState } from "@tanstack/react-table"; import { flexRender, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import * as React from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; const formatPrice = (value: number) => new Intl.NumberFormat("fa-IR", { style: "currency", currency: "IRR", }).format(value); type ColumnMeta = { cellClassName?: string; }; type Transaction = { id: number; invoiceNumber: string; href: string; amount: number; tax?: number; client: string; description: string; }; type DayGroup = { date: string; dateTime: string; transactions: Transaction[]; }; const days: DayGroup[] = [ { date: "امروز", dateTime: "2024-05-20", transactions: [ { id: 101, invoiceNumber: "ORD-48392", href: "/orders/ORD-48392", amount: 1248.5, tax: 98.5, client: "خانه آرورا", description: "۳ ست ملحفه کتان + بسته‌بندی هدیه", }, { id: 102, invoiceNumber: "PAYOUT-2298", href: "/payouts/PAYOUT-2298", amount: 4830, client: "بازارچه مسیرهای وحشی", description: "پرداخت هفتگی فروشنده", }, { id: 103, invoiceNumber: "ORD-48310", href: "/orders/ORD-48310", amount: 312, tax: 23.8, client: "نیلا گرین", description: "بازپرداخت در حال بررسی", }, ], }, { date: "دیروز", dateTime: "2024-05-19", transactions: [ { id: 104, invoiceNumber: "ORD-48277", href: "/orders/ORD-48277", amount: 2940, tax: 201.3, client: "شرکت ترک شهری", description: "۱۲ عدد عصای کوهنوردی کربنی", }, { id: 105, invoiceNumber: "ORD-48261", href: "/orders/ORD-48261", amount: 864, client: "مایا پاتل", description: "پیش‌سفارش کفش اسپرت نسخه محدود", }, { id: 106, invoiceNumber: "PAYOUT-2297", href: "/payouts/PAYOUT-2297", amount: 1980, client: "لوازم خانگی لومن", description: "درخواست پرداخت فوری", }, ], }, { date: "یکشنبه", dateTime: "2024-05-18", transactions: [ { id: 107, invoiceNumber: "ORD-48190", href: "/orders/ORD-48190", amount: 490, tax: 34.3, client: "تأمین اطلس", description: "سفارش نمونه فروش سازمانی", }, { id: 108, invoiceNumber: "ORD-48175", href: "/orders/ORD-48175", amount: 156, client: "جیمی لی", description: "پرداخت تقسیمی انجام نشده", }, ], }, ]; type FlatTransaction = Transaction & { dayDate: string; dayDateTime: string }; function flattenDays(src: DayGroup[]): FlatTransaction[] { const out: FlatTransaction[] = []; for (const d of src) { for (const t of d.transactions) { out.push({ ...t, dayDate: d.date, dayDateTime: d.dateTime }); } } return out; } export function DataTable24({ className }: { className?: string }) { const [sorting, setSorting] = React.useState([]); const data = React.useMemo(() => flattenDays(days), []); const columns = React.useMemo[]>( () => [ { id: "invoice", header: "فاکتور", cell: ({ row }) => { const t = row.original; return (
{t.invoiceNumber}
); }, meta: { cellClassName: "py-5 pe-6", }, }, { id: "amount-status", header: "مبلغ معامله", cell: ({ row }) => { const t = row.original; return ( <>
{formatPrice(t.amount)}
{t.tax ? (
+ {formatPrice(t.tax)} مالیات
) : null}
); }, meta: { cellClassName: "relative py-5 pe-6", }, }, { id: "client-desc", header: "مشتری", cell: ({ row }) => { const t = row.original; return ( <>
{t.client}
); }, meta: { cellClassName: "hidden py-5 pe-6 sm:table-cell", }, }, { id: "description", header: "توضیحات", cell: ({ row }) => { const t = row.original; return
{t.description}
; }, meta: { cellClassName: "py-5 pe-6", }, }, ], [], ); const table = useReactTable({ data, columns, state: { sorting }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), getRowId: (r) => String(r.id), }); const rowModelRows = table.getRowModel().rows; const grouped = React.useMemo(() => { const byDay = new Map< string, { date: string; dateTime: string; rows: Row[] } >(); for (const r of rowModelRows) { const key = r.original.dayDateTime; if (!byDay.has(key)) { byDay.set(key, { date: r.original.dayDate, dateTime: r.original.dayDateTime, rows: [], }); } byDay.get(key)!.rows.push(r); } return days.map((d) => byDay.get(d.dateTime)).filter(Boolean) as Array<{ date: string; dateTime: string; rows: Row[]; }>; }, [rowModelRows]); const columnCount = columns.length; return (

جدول با سرستون‌های مخفی

جدول تراکنش‌ها با سرستون‌های مخفی و رکورد‌های گروه‌بندی‌شده بر اساس تاریخ به‌روزرسانی.

{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ))} ))} {grouped.map((group) => (
{group.rows.map((row) => ( {row.getVisibleCells().map((cell) => { const meta = (cell.column.columnDef.meta as ColumnMeta) ?? {}; return ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ); })} ))} ))}
); }