"use client" import * as React from "react" import { Button } from "@/examples/base/ui-rtl/button" import { Checkbox } from "@/examples/base/ui-rtl/checkbox" import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/examples/base/ui-rtl/dropdown-menu" import { Input } from "@/examples/base/ui-rtl/input" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/examples/base/ui-rtl/table" import { flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, type ColumnDef, type ColumnFiltersState, type SortingState, type VisibilityState, } from "@tanstack/react-table" import { ArrowUpDown, ChevronDown, MoreHorizontal } from "lucide-react" import { useTranslation, type Translations, } from "@/components/language-selector" const translations: Translations = { en: { dir: "ltr", values: { filterEmails: "Filter emails...", columns: "Columns", status: "Status", email: "Email", amount: "Amount", actions: "Actions", copyPaymentId: "Copy payment ID", viewCustomer: "View customer", viewPaymentDetails: "View payment details", selectAll: "Select all", selectRow: "Select row", openMenu: "Open menu", noResults: "No results.", rowsSelected: "of", rowsSelectedSuffix: "row(s) selected.", previous: "Previous", next: "Next", success: "Success", processing: "Processing", failed: "Failed", pending: "Pending", }, }, ar: { dir: "rtl", values: { filterEmails: "تصفية البريد الإلكتروني...", columns: "الأعمدة", status: "الحالة", email: "البريد الإلكتروني", amount: "المبلغ", actions: "الإجراءات", copyPaymentId: "نسخ معرف الدفع", viewCustomer: "عرض العميل", viewPaymentDetails: "عرض تفاصيل الدفع", selectAll: "تحديد الكل", selectRow: "تحديد الصف", openMenu: "فتح القائمة", noResults: "لا توجد نتائج.", rowsSelected: "من", rowsSelectedSuffix: "صف(وف) محدد.", previous: "السابق", next: "التالي", success: "ناجح", processing: "قيد المعالجة", failed: "فشل", pending: "قيد الانتظار", }, }, he: { dir: "rtl", values: { filterEmails: "סנן אימיילים...", columns: "עמודות", status: "סטטוס", email: "אימייל", amount: "סכום", actions: "פעולות", copyPaymentId: "העתק מזהה תשלום", viewCustomer: "צפה בלקוח", viewPaymentDetails: "צפה בפרטי תשלום", selectAll: "בחר הכל", selectRow: "בחר שורה", openMenu: "פתח תפריט", noResults: "אין תוצאות.", rowsSelected: "מתוך", rowsSelectedSuffix: "שורות נבחרו.", previous: "הקודם", next: "הבא", success: "הצליח", processing: "מעבד", failed: "נכשל", pending: "ממתין", }, }, } type Payment = { id: string amount: number status: "pending" | "processing" | "success" | "failed" email: string } const data: Payment[] = [ { id: "m5gr84i9", amount: 316, status: "success", email: "ken99@example.com", }, { id: "3u1reuv4", amount: 242, status: "success", email: "Abe45@example.com", }, { id: "derv1ws0", amount: 837, status: "processing", email: "Monserrat44@example.com", }, { id: "5kma53ae", amount: 874, status: "success", email: "Silas22@example.com", }, { id: "bhqecj4p", amount: 721, status: "failed", email: "carmella@example.com", }, ] export function DataTableRtl() { const { t, dir, language } = useTranslation(translations, "ar") const [sorting, setSorting] = React.useState([]) const [columnFilters, setColumnFilters] = React.useState( [] ) const [columnVisibility, setColumnVisibility] = React.useState({}) const [rowSelection, setRowSelection] = React.useState({}) const columns: ColumnDef[] = React.useMemo( () => [ { id: "select", header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value) } aria-label={t.selectAll} /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label={t.selectRow} /> ), enableSorting: false, enableHiding: false, }, { accessorKey: "status", header: t.status, cell: ({ row }) => { const status = row.getValue("status") as string const statusMap: Record = { success: t.success, processing: t.processing, failed: t.failed, pending: t.pending, } return
{statusMap[status]}
}, }, { accessorKey: "email", header: ({ column }) => { return ( ) }, cell: ({ row }) => (
{row.getValue("email")}
), }, { accessorKey: "amount", header: () =>
{t.amount}
, cell: ({ row }) => { const amount = parseFloat(row.getValue("amount")) const formatted = new Intl.NumberFormat( dir === "rtl" ? "ar-SA" : "en-US", { style: "currency", currency: "USD", } ).format(amount) return
{formatted}
}, }, { id: "actions", enableHiding: false, cell: ({ row }) => { const payment = row.original return ( } > {t.openMenu} {t.actions} navigator.clipboard.writeText(payment.id)} > {t.copyPaymentId} {t.viewCustomer} {t.viewPaymentDetails} ) }, }, ], [t, dir, language] ) const table = useReactTable({ data, columns, onSortingChange: setSorting, onColumnFiltersChange: setColumnFilters, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), onColumnVisibilityChange: setColumnVisibility, onRowSelectionChange: setRowSelection, state: { sorting, columnFilters, columnVisibility, rowSelection, }, }) return (
table.getColumn("email")?.setFilterValue(event.target.value) } className="max-w-sm" /> } > {t.columns} {table .getAllColumns() .filter((column) => column.getCanHide()) .map((column) => { return ( column.toggleVisibility(!!value) } > {column.id} ) })}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ) })} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext() )} ))} )) ) : ( {t.noResults} )}
{table.getFilteredSelectedRowModel().rows.length} {t.rowsSelected}{" "} {table.getFilteredRowModel().rows.length} {t.rowsSelectedSuffix}
) }