410 lines
12 KiB
TypeScript
410 lines
12 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
type Column,
|
|
type ColumnDef,
|
|
flexRender,
|
|
getCoreRowModel,
|
|
getSortedRowModel,
|
|
type SortingState,
|
|
useReactTable,
|
|
} from "@tanstack/react-table";
|
|
import { ArrowDown, ArrowUp, ChevronsUpDown } from "lucide-react";
|
|
import * as React from "react";
|
|
import { z } from "zod";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
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 UseDataTableOptions<TData> = {
|
|
data: Array<TData>;
|
|
columns: Array<ColumnDef<TData, unknown>>;
|
|
getRowId?: (row: TData) => string;
|
|
initialSorting?: SortingState;
|
|
};
|
|
|
|
export function useDataTable<TData>(options: UseDataTableOptions<TData>) {
|
|
const { data, columns, getRowId, initialSorting = [] } = options;
|
|
|
|
const [sorting, setSorting] = React.useState<SortingState>(initialSorting);
|
|
|
|
const table = useReactTable({
|
|
data,
|
|
columns,
|
|
getRowId,
|
|
state: { sorting },
|
|
onSortingChange: setSorting,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getSortedRowModel: getSortedRowModel(),
|
|
});
|
|
|
|
return {
|
|
table,
|
|
sorting,
|
|
setSorting,
|
|
};
|
|
}
|
|
|
|
type DataTableColumnHeaderProps<TData, TValue> = {
|
|
column: Column<TData, TValue>;
|
|
title: string;
|
|
};
|
|
|
|
export const DataTableColumnHeader = <TData, TValue>({
|
|
column,
|
|
title,
|
|
}: DataTableColumnHeaderProps<TData, TValue>) => {
|
|
const canSort = column.getCanSort();
|
|
const sorted = column.getIsSorted();
|
|
|
|
if (!canSort) {
|
|
return (
|
|
<span className="flex h-8 items-center text-xs font-medium text-foreground sm:text-sm">
|
|
{title}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="flex h-8 items-center gap-2 px-0 text-xs font-medium text-foreground sm:text-sm"
|
|
onClick={() => column.toggleSorting(sorted === "asc")}
|
|
>
|
|
<span>{title}</span>
|
|
{sorted === "desc" ? (
|
|
<ArrowDown className="h-3 w-3 sm:h-4 sm:w-4" />
|
|
) : sorted === "asc" ? (
|
|
<ArrowUp className="h-3 w-3 sm:h-4 sm:w-4" />
|
|
) : (
|
|
<ChevronsUpDown className="h-3 w-3 opacity-50 sm:h-4 sm:w-4" />
|
|
)}
|
|
</Button>
|
|
);
|
|
};
|
|
|
|
export const schema = z.object({
|
|
id: z.string(),
|
|
item: z.string(),
|
|
type: z.string(),
|
|
stock: z.boolean(),
|
|
sku: z.string(),
|
|
price: z.number(),
|
|
availability: z.array(z.enum(["فروشگاه", "آنلاین"])),
|
|
});
|
|
|
|
const data = [
|
|
{
|
|
id: "prod-001",
|
|
item: "قاب تبلت",
|
|
type: "الکترونیک",
|
|
stock: true,
|
|
sku: "TC-001",
|
|
price: 83.24,
|
|
availability: ["فروشگاه", "آنلاین"],
|
|
},
|
|
{
|
|
id: "prod-002",
|
|
item: "ساعت هوشمند",
|
|
type: "الکترونیک",
|
|
stock: true,
|
|
sku: "SW-002",
|
|
price: 246.27,
|
|
availability: ["فروشگاه", "آنلاین"],
|
|
},
|
|
{
|
|
id: "prod-003",
|
|
item: "پلیور پشمی",
|
|
type: "لوازم جانبی",
|
|
stock: true,
|
|
sku: "WS-003",
|
|
price: 168.27,
|
|
availability: ["فروشگاه"],
|
|
},
|
|
{
|
|
id: "prod-004",
|
|
item: "هدفون بیسیم",
|
|
type: "الکترونیک",
|
|
stock: true,
|
|
sku: "WE-004",
|
|
price: 107.75,
|
|
availability: ["فروشگاه", "آنلاین"],
|
|
},
|
|
{
|
|
id: "prod-005",
|
|
item: "کاور لپتاپ",
|
|
type: "الکترونیک",
|
|
stock: true,
|
|
sku: "LS-005",
|
|
price: 248.02,
|
|
availability: ["فروشگاه", "آنلاین"],
|
|
},
|
|
{
|
|
id: "prod-006",
|
|
item: "کفش دویدن",
|
|
type: "کفش",
|
|
stock: true,
|
|
sku: "RS-006",
|
|
price: 208.26,
|
|
availability: ["فروشگاه"],
|
|
},
|
|
{
|
|
id: "prod-007",
|
|
item: "کاپشن زمستانی",
|
|
type: "پوشاک",
|
|
stock: true,
|
|
sku: "WJ-007",
|
|
price: 148.06,
|
|
availability: ["فروشگاه"],
|
|
},
|
|
{
|
|
id: "prod-008",
|
|
item: "قاب گوشی",
|
|
type: "لوازم جانبی",
|
|
stock: true,
|
|
sku: "PC-008",
|
|
price: 298.08,
|
|
availability: ["فروشگاه", "آنلاین"],
|
|
},
|
|
{
|
|
id: "prod-009",
|
|
item: "ردیاب تناسب اندام",
|
|
type: "پوشاک",
|
|
stock: true,
|
|
sku: "FT-009",
|
|
price: 222.09,
|
|
availability: ["فروشگاه"],
|
|
},
|
|
{
|
|
id: "prod-010",
|
|
item: "عینک آفتابی",
|
|
type: "لوازم جانبی",
|
|
stock: true,
|
|
sku: "SG-010",
|
|
price: 60.17,
|
|
availability: ["فروشگاه"],
|
|
},
|
|
];
|
|
|
|
export const validatedData = schema.array().parse(data);
|
|
|
|
export const columns: ColumnDef<z.infer<typeof schema>>[] = [
|
|
{
|
|
accessorKey: "sku",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="کد کالا" />
|
|
),
|
|
cell: ({ row }) => {
|
|
const sku = row.getValue("sku") as string;
|
|
return (
|
|
<span className="font-mono text-xs tracking-wide whitespace-nowrap text-muted-foreground uppercase">
|
|
{sku}
|
|
</span>
|
|
);
|
|
},
|
|
enableSorting: true,
|
|
enableHiding: false,
|
|
},
|
|
{
|
|
accessorKey: "item",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="محصول" />
|
|
),
|
|
cell: ({ row }) => (
|
|
<div className="max-w-[180px] truncate font-medium sm:max-w-[220px]">
|
|
{row.getValue("item")}
|
|
</div>
|
|
),
|
|
enableSorting: true,
|
|
enableHiding: false,
|
|
},
|
|
{
|
|
accessorKey: "type",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="دستهبندی" />
|
|
),
|
|
cell: ({ row }) => {
|
|
const type = row.getValue("type") as string;
|
|
return (
|
|
<span className="text-xs whitespace-nowrap text-muted-foreground sm:text-sm">
|
|
{type}
|
|
</span>
|
|
);
|
|
},
|
|
enableSorting: true,
|
|
enableHiding: false,
|
|
},
|
|
{
|
|
accessorKey: "stock",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="موجود در انبار" />
|
|
),
|
|
cell: ({ row }) => {
|
|
const inStock: boolean = row.getValue("stock");
|
|
return (
|
|
<div className="flex justify-start">
|
|
<span className="inline-flex min-w-[3rem] justify-center rounded-full border px-2 py-1 text-xs font-semibold whitespace-nowrap">
|
|
{inStock ? "بله" : "خیر"}
|
|
</span>
|
|
</div>
|
|
);
|
|
},
|
|
enableSorting: false,
|
|
enableHiding: false,
|
|
},
|
|
{
|
|
accessorKey: "price",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="قیمت" />
|
|
),
|
|
cell: ({ row }) => {
|
|
const price = parseFloat(row.getValue("price"));
|
|
return (
|
|
<div className="font-medium whitespace-nowrap tabular-nums">
|
|
{formatPrice(price)}
|
|
</div>
|
|
);
|
|
},
|
|
enableSorting: true,
|
|
enableHiding: false,
|
|
},
|
|
{
|
|
accessorKey: "availability",
|
|
header: ({ column }) => (
|
|
<DataTableColumnHeader column={column} title="دسترسپذیر در" />
|
|
),
|
|
cell: ({ row }) => {
|
|
const availability: ("فروشگاه" | "آنلاین")[] =
|
|
row.getValue("availability");
|
|
return (
|
|
<div className="flex gap-2">
|
|
{availability.map((location) => (
|
|
<Badge
|
|
key={location}
|
|
variant="secondary"
|
|
className="text-xs whitespace-nowrap"
|
|
>
|
|
{location}
|
|
</Badge>
|
|
))}
|
|
</div>
|
|
);
|
|
},
|
|
enableSorting: false,
|
|
enableHiding: false,
|
|
},
|
|
];
|
|
|
|
export const DataTable4 = ({ className }: { className?: string }) => {
|
|
const { table } = useDataTable({
|
|
data: validatedData,
|
|
columns,
|
|
getRowId: (row) => row.id.toString(),
|
|
});
|
|
|
|
return (
|
|
<section dir="rtl" lang="fa" className={cn("py-24", className)}>
|
|
<div className="container px-4 sm:px-6 lg:px-8">
|
|
<div className="w-full overflow-hidden">
|
|
<div className="mb-8 text-start">
|
|
<h2 className="text-xl font-bold tracking-tight">
|
|
جدول واکنشگرای مینیمال
|
|
</h2>
|
|
<p className="mt-2 text-muted-foreground">
|
|
جدول کاملاً واکنشگرا با اسکرول افقی، استایل سفارشی سلولها و
|
|
تایپوگرافی تطبیقی. بهینهشده برای موبایل با ژesture لمسی و
|
|
راهنمای کشیدن.
|
|
</p>
|
|
</div>
|
|
{/* Wrap in ScrollArea for horizontal scroll */}
|
|
<ScrollArea className="w-full whitespace-nowrap">
|
|
<div className="min-w-[800px]">
|
|
<Table className="table-fixed border-separate border-spacing-0 [&_tr:not(:last-child)_td]:border-b">
|
|
<TableHeader>
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
<TableRow
|
|
key={headerGroup.id}
|
|
className="hover:bg-transparent"
|
|
>
|
|
{headerGroup.headers.map((header) => {
|
|
return (
|
|
<TableHead
|
|
key={header.id}
|
|
colSpan={header.colSpan}
|
|
className="relative h-12 border-y border-border bg-muted/50 px-3 text-start text-xs font-medium select-none first:rounded-ss-lg first:border-s first:ps-3 last:rounded-se-lg last:border-e last:pe-3 sm:px-4 sm:text-sm sm:first:ps-5 sm:last:pe-5"
|
|
>
|
|
{header.isPlaceholder
|
|
? null
|
|
: flexRender(
|
|
header.column.columnDef.header,
|
|
header.getContext(),
|
|
)}
|
|
</TableHead>
|
|
);
|
|
})}
|
|
</TableRow>
|
|
))}
|
|
</TableHeader>
|
|
<TableBody>
|
|
{table.getRowModel().rows?.length ? (
|
|
table.getRowModel().rows.map((row) => (
|
|
<TableRow
|
|
key={row.id}
|
|
data-state={row.getIsSelected() && "selected"}
|
|
className="border-0 hover:bg-muted/50 [&:first-child>td:first-child]:rounded-ss-lg [&:first-child>td:last-child]:rounded-se-lg [&:last-child>td:first-child]:rounded-es-lg [&:last-child>td:last-child]:rounded-ee-lg"
|
|
>
|
|
{row.getVisibleCells().map((cell) => (
|
|
<TableCell
|
|
key={cell.id}
|
|
className="px-3 py-2 text-xs first:ps-3 last:pe-3 sm:px-4 sm:py-3 sm:text-sm sm:first:ps-5 sm:last:pe-5"
|
|
>
|
|
{flexRender(
|
|
cell.column.columnDef.cell,
|
|
cell.getContext(),
|
|
)}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
) : (
|
|
<TableRow className="border-0 [&>td:first-child]:rounded-ss-lg [&>td:first-child]:rounded-es-lg [&>td:last-child]:rounded-se-lg [&>td:last-child]:rounded-ee-lg">
|
|
<TableCell
|
|
colSpan={columns.length}
|
|
className="h-24 px-4 text-center text-sm text-muted-foreground"
|
|
>
|
|
نتیجهای یافت نشد.
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
<ScrollBar orientation="horizontal" />
|
|
</ScrollArea>
|
|
</div>
|
|
{/* Optional: Mobile scroll hint */}
|
|
<p className="mt-2 text-center text-xs text-muted-foreground sm:hidden">
|
|
← برای دیدن ستونهای بیشتر بکشید →
|
|
</p>
|
|
</div>
|
|
</section>
|
|
);
|
|
};
|