
A basic data table section with sortable columns, currency formatting, and availability badges. Built with TanStack Table for sorting functionality.
block/data-table1/data-table1.tsx
"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 { cn } from "cn";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
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-sm font-medium text-foreground">
{title}
</span>
);
}
return (
<Button
variant="ghost"
size="sm"
className="flex h-8 items-center gap-2 px-0 text-sm font-medium text-foreground"
onClick={() => column.toggleSorting(sorted === "asc")}
>
<span>{title}</span>
{sorted === "desc" ? (
<ArrowDown className="h-4 w-4" />
) : sorted === "asc" ? (
<ArrowUp className="h-4 w-4" />
) : (
<ChevronsUpDown className="h-4 w-4 opacity-50" />
)}
</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(["In store", "Online"])),
});
const data = [
{
id: "prod-001",
item: "Tablet Case",
type: "Electronics",
stock: true,
sku: "TC-001",
price: 83.24,
availability: ["In store", "Online"],
},
{
id: "prod-002",
item: "Smart Watch",
type: "Electronics",
stock: true,
sku: "SW-002",
price: 246.27,
availability: ["In store", "Online"],
},
{
id: "prod-003",
item: "Wool Sweater",
type: "Accessories",
stock: true,
sku: "WS-003",
price: 168.27,
availability: ["In store"],
},
{
id: "prod-004",
item: "Wireless Earbuds",
type: "Electronics",
stock: true,
sku: "WE-004",
price: 107.75,
availability: ["In store", "Online"],
},
{
id: "prod-005",
item: "Laptop Sleeve",
type: "Electronics",
stock: true,
sku: "LS-005",
price: 248.02,
availability: ["In store", "Online"],
},
{
id: "prod-006",
item: "Running Shoes",
type: "Footwear",
stock: true,
sku: "RS-006",
price: 208.26,
availability: ["In store"],
},
{
id: "prod-007",
item: "Winter Jacket",
type: "Clothing",
stock: true,
sku: "WJ-007",
price: 148.06,
availability: ["In store"],
},
{
id: "prod-008",
item: "Phone Case",
type: "Accessories",
stock: true,
sku: "PC-008",
price: 298.08,
availability: ["In store", "Online"],
},
{
id: "prod-009",
item: "Fitness Tracker",
type: "Clothing",
stock: true,
sku: "FT-009",
price: 222.09,
availability: ["In store"],
},
{
id: "prod-010",
item: "Sunglasses",
type: "Accessories",
stock: true,
sku: "SG-010",
price: 60.17,
availability: ["In store"],
},
];
export const validatedData = schema.array().parse(data);
export const columns: ColumnDef<z.infer<typeof schema>>[] = [
{
accessorKey: "sku",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="SKU" />
),
enableSorting: true,
enableHiding: false,
},
{
accessorKey: "item",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Item" />
),
cell: ({ row }) => (
<div className="font-medium">{row.getValue("item")}</div>
),
enableSorting: true,
enableHiding: false,
},
{
accessorKey: "type",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Type" />
),
enableSorting: true,
enableHiding: false,
},
{
accessorKey: "stock",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="In Stock" />
),
cell: ({ row }) => {
const inStock: boolean = row.getValue("stock");
return inStock ? "Yes" : "No";
},
enableSorting: false,
enableHiding: false,
},
{
accessorKey: "price",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Price" />
),
cell: ({ row }) => {
const price = parseFloat(row.getValue("price"));
const formatted = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(price);
return <div className="font-medium">{formatted}</div>;
},
enableSorting: true,
enableHiding: false,
},
{
accessorKey: "availability",
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Available In" />
),
cell: ({ row }) => {
const availability: ("In store" | "Online")[] =
row.getValue("availability");
return (
<div className="flex space-x-2">
{availability.map((location) => (
<Badge key={location} variant="secondary">
{location}
</Badge>
))}
</div>
);
},
enableSorting: false,
enableHiding: false,
},
];
export const DataTable1 = ({ className }: { className?: string }) => {
const { table } = useDataTable({
data: validatedData,
columns,
getRowId: (row) => row.id.toString(),
});
return (
<section className={cn("py-32", className)}>
<div className="container">
<div className="w-full">
<div className="mb-8 text-left">
<h2 className="text-2xl font-bold tracking-tight">
Basic Data Table
</h2>
<p className="mt-2 text-muted-foreground">
A clean and simple data table with sorting capabilities and
responsive design. Perfect for displaying structured data with
minimal styling.
</p>
</div>
<div className="overflow-hidden rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id} className="px-0">
{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"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
</div>
</div>
</section>
);
};