|
| 1 | +import { DataTable } from "@tablecraft/table"; |
| 2 | +import { createTableCraftAdapter } from "@tablecraft/table"; |
| 3 | +import { createOrdersAdapter, type OrdersRow } from "../generated"; |
| 4 | +import { API_BASE_URL } from "../api"; |
| 5 | +import { useMemo } from "react"; |
| 6 | + |
| 7 | +// --- Child Table Component --- |
| 8 | +function OrderItemsTable({ orderId }: { orderId: number }) { |
| 9 | + // Create an adapter for the orderItems table, passing the orderId as a custom filter. |
| 10 | + // We use useMemo so the adapter isn't recreated on every render. |
| 11 | + const childAdapter = useMemo(() => { |
| 12 | + return createTableCraftAdapter({ |
| 13 | + baseUrl: API_BASE_URL, |
| 14 | + table: "orderItems", |
| 15 | + customFilters: { |
| 16 | + orderId: orderId, // Automatically translates to a filter constraint |
| 17 | + }, |
| 18 | + }); |
| 19 | + }, [orderId]); |
| 20 | + |
| 21 | + return ( |
| 22 | + |
| 23 | + <DataTable |
| 24 | + adapter={childAdapter} |
| 25 | + hiddenColumns={["orderId", "id", "tenantId", "deletedAt", "createdAt", "updatedAt"]} |
| 26 | + config={{ |
| 27 | + enableUrlState: false, |
| 28 | + enablePagination: false, |
| 29 | + enableSearch: false, |
| 30 | + enableDateFilter: false, |
| 31 | + enableToolbar: false, |
| 32 | + enableRowSelection: false, |
| 33 | + columnResizingTableId: "order-items-table", |
| 34 | + removeOuterBorder: true, // Strips the card wrapper for a seamless nested look! |
| 35 | + }} |
| 36 | + /> |
| 37 | + |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +// --- Parent Table Component --- |
| 42 | +export function OrdersSubRowPage() { |
| 43 | + const parentAdapter = useMemo(() => { |
| 44 | + return createOrdersAdapter({ |
| 45 | + baseUrl: API_BASE_URL, |
| 46 | + }); |
| 47 | + }, []); |
| 48 | + |
| 49 | + return ( |
| 50 | + <div className="p-8 space-y-4 max-w-7xl mx-auto"> |
| 51 | + <div> |
| 52 | + <h1 className="text-3xl font-bold tracking-tight">Orders (Master-Detail)</h1> |
| 53 | + <p className="text-muted-foreground mt-1"> |
| 54 | + Click the arrow next to an order to see its items. The sub-table loads dynamically |
| 55 | + and manages its own state without affecting the parent URL! |
| 56 | + </p> |
| 57 | + </div> |
| 58 | + |
| 59 | + <DataTable<OrdersRow> |
| 60 | + adapter={parentAdapter} |
| 61 | + renderSubRow={({ row }) => <OrderItemsTable orderId={row.id} />} |
| 62 | + config={{ |
| 63 | + enableSearch: true, |
| 64 | + enableColumnResizing: true, |
| 65 | + defaultPageSize: 10, |
| 66 | + }} |
| 67 | + hiddenColumns={["tenantId", "deletedAt", "updatedAt", "userId", "role"]} |
| 68 | + /> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
0 commit comments