⌘K
Search
Search pages, pieces and blocks.
Table with filters/A list page in the App Shell (Strip): the top row, the controls row, the framed table with status tags; a row opens the right panel, New booking the takeover.
components/blocks/table-with-filters.tsx
"use client"
import * as React from "react"
import { AsteriskIcon, CalendarIcon, HomeIcon, LogOutIcon, PlusIcon, SchoolIcon, SearchIcon, SettingsIcon, UserIcon, UsersIcon, WalletIcon, WavesIcon,} from "@/components/icons"import { AppShell, type AppShellNavItem, type AppShellPhoneLayout } from "@/components/ui/app-shell"import { Badge } from "@/components/ui/badge"import { Breadcrumb, BreadcrumbLink, BreadcrumbList, BreadcrumbSection, BreadcrumbSwitcher } from "@/components/ui/breadcrumb"import { Button } from "@/components/ui/button"import { DataTable, createDataTableColumns } from "@/components/ui/data-table"import { DescriptionDetails, DescriptionItem, DescriptionList, DescriptionTerm } from "@/components/ui/description-list"import { DropdownMenuGroup, DropdownMenuItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator,} from "@/components/ui/dropdown-menu"import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "@/components/ui/empty"import { Field, FieldDescription, FieldGroup } from "@/components/ui/field"import { FilterAdd, FilterBar, FilterChip, FilterClear, FilterOptions } from "@/components/ui/filter-bar"import { InputGroup, InputGroupInput } from "@/components/ui/input-group"import { PageHeader, PageHeaderActions } from "@/components/ui/page-header"import { SearchInput } from "@/components/ui/search-input"
// Table with filters: the Maré bookings in the App Shell (Strip). The top// row: where the page sits ("Bookings / Overview", the second part a// switcher of the views: Overview, Upcoming, Past and Cancelled, each// filtering the rows), Search (by student or booking) and New booking.// The controls row: Columns, then the filters as chips, Add filter and// Clear all. Then the framed Data Table, paging its rows. A row, clicked or// Enter, opens the booking in the right panel; Esc closes it and focus goes// back to the row. New booking opens the takeover, a focused form over the// whole shell. On a phone the controls are Columns and Filters, and the// rows a list.
type Status = "Confirmed" | "Pending" | "Cancelled"type Level = "Beginner" | "Improver" | "Private"
type Booking = { id: string student: string school: string status: Status instructor: string level: Level // Lisbon time, yyyy-mm-ddThh:mm, so it sorts as text. date: string price: number}
const bookings: Booking[] = [ { id: "MR-2041", student: "Marta Silva", school: "Ericeira", status: "Confirmed", instructor: "Miguel Costa", level: "Beginner", date: "2026-11-30T09:00", price: 45 }, { id: "MR-2042", student: "Duarte Pereira", school: "Peniche", status: "Pending", instructor: "Pedro Matos", level: "Improver", date: "2026-11-30T11:30", price: 55 }, { id: "MR-2043", student: "Leonor Santos", school: "Costa da Caparica", status: "Confirmed", instructor: "Ana Vieira", level: "Private", date: "2026-12-01T08:30", price: 90 }, { id: "MR-2044", student: "Gonçalo Martins", school: "Carcavelos", status: "Cancelled", instructor: "João Freitas", level: "Beginner", date: "2026-12-01T10:00", price: 45 }, { id: "MR-2045", student: "Carolina Rocha", school: "Ericeira", status: "Confirmed", instructor: "Miguel Costa", level: "Improver", date: "2026-12-02T09:00", price: 55 }, { id: "MR-2046", student: "Francisco Almeida", school: "Nazaré", status: "Confirmed", instructor: "Vasco Reis", level: "Private", date: "2026-12-02T14:00", price: 120 }, { id: "MR-2047", student: "Sofia Nunes", school: "Sagres", status: "Pending", instructor: "Luísa Brito", level: "Beginner", date: "2026-12-03T09:30", price: 45 }, { id: "MR-2048", student: "Henrique Gomes", school: "Carcavelos", status: "Confirmed", instructor: "João Freitas", level: "Improver", date: "2026-12-03T16:00", price: 55 }, { id: "MR-2049", student: "Matilde Teixeira", school: "Peniche", status: "Confirmed", instructor: "Pedro Matos", level: "Beginner", date: "2026-12-04T10:30", price: 45 }, { id: "MR-2050", student: "Rodrigo Pinto", school: "Ericeira", status: "Cancelled", instructor: "Miguel Costa", level: "Private", date: "2026-12-04T15:00", price: 90 }, { id: "MR-2051", student: "Mariana Cardoso", school: "Costa da Caparica", status: "Confirmed", instructor: "Ana Vieira", level: "Improver", date: "2026-12-05T09:00", price: 55 }, { id: "MR-2052", student: "Afonso Moreira", school: "Sagres", status: "Confirmed", instructor: "Luísa Brito", level: "Improver", date: "2026-12-05T11:00", price: 55 }, { id: "MR-2053", student: "Catarina Correia", school: "Nazaré", status: "Pending", instructor: "Vasco Reis", level: "Beginner", date: "2026-12-06T10:00", price: 45 }, { id: "MR-2054", student: "Tomás Amaro", school: "Ericeira", status: "Confirmed", instructor: "Miguel Costa", level: "Beginner", date: "2026-12-06T12:30", price: 45 }, { id: "MR-2055", student: "Rita Fonseca", school: "Carcavelos", status: "Confirmed", instructor: "João Freitas", level: "Private", date: "2026-12-07T08:00", price: 90 }, { id: "MR-2056", student: "Inês Lopes", school: "Ericeira", status: "Confirmed", instructor: "Miguel Costa", level: "Improver", date: "2026-12-07T10:30", price: 55 }, { id: "MR-2057", student: "Diogo Ribeiro", school: "Peniche", status: "Confirmed", instructor: "Pedro Matos", level: "Beginner", date: "2026-12-08T09:00", price: 45 }, { id: "MR-2058", student: "Beatriz Carvalho", school: "Sagres", status: "Cancelled", instructor: "Luísa Brito", level: "Private", date: "2026-12-08T13:00", price: 90 },]
const tones: Record<Status, "success" | "warning" | "destructive"> = { Confirmed: "success", Pending: "warning", Cancelled: "destructive",}
const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
// "Mon 30 Nov, 09:00", read from the text so the time zone never shifts it.function formatDate(value: string) { const [day, time] = value.split("T") const [year, month, date] = day.split("-").map(Number) const weekday = weekdays[new Date(Date.UTC(year, month - 1, date)).getUTCDay()] return `${weekday} ${date} ${months[month - 1]}, ${time}`}
const euros = new Intl.NumberFormat("en-GB", { style: "currency", currency: "EUR" })
const helper = createDataTableColumns<Booking>()
const columns = helper.columns([ helper.accessor("student", { header: "Student", meta: { kind: "person", emphasis: "strong" }, }), helper.accessor("status", { header: "Status", meta: { kind: "status" }, cell: ({ getValue }) => ( <Badge look="status" tone={tones[getValue()]}> {getValue()} </Badge> ), }), helper.accessor("instructor", { header: "Instructor", meta: { kind: "person", emphasis: "muted" }, }), helper.accessor("level", { header: "Level", meta: { kind: "text" }, cell: ({ getValue }) => <Badge variant="secondary">{getValue()}</Badge>, }), helper.accessor("date", { header: "Date", meta: { kind: "date", numeric: true }, cell: ({ getValue }) => formatDate(getValue()), }), helper.accessor("price", { header: "Price", meta: { kind: "number", align: "end", numeric: true }, cell: ({ getValue }) => euros.format(getValue()), }),])
const filterFields = [ { field: "Status", options: ["Confirmed", "Pending", "Cancelled"] }, { field: "Level", options: ["Beginner", "Improver", "Private"] }, { field: "School", options: ["Ericeira", "Peniche", "Costa da Caparica", "Carcavelos", "Nazaré", "Sagres"] },] as const
type Filter = { field: (typeof filterFields)[number]["field"]; value: string }
// The views in the top row's switcher. Now is Mon 30 Nov, 12:00 in Lisbon.type View = "overview" | "upcoming" | "past" | "cancelled"
const now = "2026-11-30T12:00"
const views: { value: View; label: string; shows: (booking: Booking) => boolean }[] = [ { value: "overview", label: "Overview", shows: () => true }, { value: "upcoming", label: "Upcoming", shows: (booking) => booking.status !== "Cancelled" && booking.date >= now }, { value: "past", label: "Past", shows: (booking) => booking.status !== "Cancelled" && booking.date < now }, { value: "cancelled", label: "Cancelled", shows: (booking) => booking.status === "Cancelled" },]
// The Strip sidebar's rows; Settings is pinned at its foot.const stripNav = ["Overview", "Bookings", "Calendar", "Schools", "Instructors", "Students", "Lessons", "Payouts", "Reviews", "Messages", "Reports"]
// Rail's rows, for App Shell's Phone section, which shows Rail's phone layouts.const sections = [ { label: "Overview", icon: HomeIcon }, { label: "Bookings", icon: CalendarIcon, count: "104" }, { label: "Schools", icon: SchoolIcon }, { label: "Instructors", icon: UsersIcon }, { label: "Payouts", icon: WalletIcon },]
const account = { name: "António Ferreira", detail: "Head of Product", initials: "AF", menu: ( <> <DropdownMenuGroup> <DropdownMenuItem> <UserIcon /> Profile </DropdownMenuItem> <DropdownMenuItem> <SettingsIcon /> Settings </DropdownMenuItem> </DropdownMenuGroup> <DropdownMenuSeparator /> <DropdownMenuItem> <LogOutIcon /> Sign out </DropdownMenuItem> </> ),}
function matches(booking: Booking, filters: Filter[], search: string) { const text = search.trim().toLowerCase() if (text && !`${booking.student} ${booking.id}`.toLowerCase().includes(text)) return false // Values of one field are either/or; fields are both. return filterFields.every(({ field }) => { const values = filters.filter((filter) => filter.field === field).map((filter) => filter.value) if (values.length === 0) return true const value = field === "Status" ? booking.status : field === "Level" ? booking.level : booking.school return values.includes(value) })}
// phoneLayout is for App Shell's Phone section only (the layout, with the// page's action in the bar); unset, the phone gets the shell's default.function TableWithFiltersBlock({ phoneLayout }: { phoneLayout?: AppShellPhoneLayout["layout"] } = {}) { const [filters, setFilters] = React.useState<Filter[]>([{ field: "Level", value: "Beginner" }]) const [search, setSearch] = React.useState("") const [view, setView] = React.useState<View>("overview") const currentView = views.find((each) => each.value === view) ?? views[0] const [openId, setOpenId] = React.useState<string | null>(null) const [booking, setBooking] = React.useState(false) const addFilter = React.useRef<HTMLButtonElement>(null) const shown = bookings.filter((booking) => currentView.shows(booking) && matches(booking, filters, search)) const open = bookings.find((booking) => booking.id === openId)
const toggle = (field: Filter["field"], value: string) => setFilters((current) => current.some((each) => each.field === field && each.value === value) ? current.filter((each) => !(each.field === field && each.value === value)) : [...current, { field, value }] )
const rail = Boolean(phoneLayout) const nav: AppShellNavItem[] = rail ? sections.map((section) => ({ ...section, active: section.label === "Bookings" })) : stripNav.map((label) => ({ label, count: label === "Bookings" ? "104" : undefined, active: label === "Bookings" }))
const filterChoices = filterFields.map(({ field, options }) => ( <FilterOptions key={field} field={field} options={[...options]} selected={filters.filter((filter) => filter.field === field).map((filter) => filter.value)} onToggle={(value) => toggle(field, value)} /> ))
return ( <AppShell id="mare-bookings" look={rail ? "rail" : "strip"} contentAlign="top" brand={rail ? { name: "Maré", detail: "Admin", logo: <WavesIcon /> } : { name: "Maré", logo: <AsteriskIcon /> }} nav={nav} settings={{ label: "Settings" }} account={account} title="Bookings" phoneLayout={ phoneLayout && { layout: phoneLayout, action: { label: "New booking", icon: PlusIcon, onClick: () => setBooking(true) }, } } header={ <PageHeader look="toprow"> <Breadcrumb> <BreadcrumbList> <BreadcrumbSection> <BreadcrumbLink href="#">Bookings</BreadcrumbLink> </BreadcrumbSection> <BreadcrumbSwitcher label={currentView.label} aria-label={`Bookings view: ${currentView.label}`}> <DropdownMenuRadioGroup value={view} onValueChange={(value) => { const next = views.find((each) => each.value === value) if (next) setView(next.value) }} > {views.map((each) => ( <DropdownMenuRadioItem key={each.value} value={each.value} closeOnClick> {each.label} </DropdownMenuRadioItem> ))} </DropdownMenuRadioGroup> </BreadcrumbSwitcher> </BreadcrumbList> </Breadcrumb> <SearchInput aria-label="Search bookings" placeholder="Search" value={search} onValueChange={setSearch} /> <PageHeaderActions primary={{ label: "New booking", onClick: () => setBooking(true) }} /> </PageHeader> } takeover={{ open: booking, onOpenChange: setBooking, title: "New booking", actions: <Button onClick={() => setBooking(false)}>Book lesson</Button>, children: ( <FieldGroup> {[ { id: "new-student", label: "Student", value: "Tomás Amaro" }, { id: "new-school", label: "School", value: "Ericeira" }, { id: "new-when", label: "When", value: "Sat 12 Dec, 09:00" }, { id: "new-instructor", label: "Instructor", value: "Miguel Costa" }, ].map(({ id, label, value }) => ( <Field key={id}> <InputGroup> <InputGroupInput id={id} label={label} defaultValue={value} /> </InputGroup> </Field> ))} <FieldDescription>The student gets a confirmation by email once the lesson is booked.</FieldDescription> </FieldGroup> ), }} panel={ open && { open: true, onOpenChange: (next) => !next && setOpenId(null), title: open.student, description: `${open.id}, ${open.school}`, children: ( <div className="flex flex-col gap-6"> <DescriptionList> <DescriptionItem> <DescriptionTerm>Status</DescriptionTerm> <DescriptionDetails> <Badge look="status" tone={tones[open.status]}> {open.status} </Badge> </DescriptionDetails> </DescriptionItem> <DescriptionItem> <DescriptionTerm>When</DescriptionTerm> <DescriptionDetails numeric>{formatDate(open.date)}</DescriptionDetails> </DescriptionItem> <DescriptionItem> <DescriptionTerm>Level</DescriptionTerm> <DescriptionDetails> <Badge variant="secondary">{open.level}</Badge> </DescriptionDetails> </DescriptionItem> <DescriptionItem> <DescriptionTerm>Instructor</DescriptionTerm> <DescriptionDetails>{open.instructor}</DescriptionDetails> </DescriptionItem> <DescriptionItem> <DescriptionTerm>Price</DescriptionTerm> <DescriptionDetails numeric>{euros.format(open.price)}</DescriptionDetails> </DescriptionItem> </DescriptionList> <div className="flex gap-2"> <Button variant="outline">Reschedule</Button> <Button variant="ghost">Message student</Button> </div> </div> ), } } > <DataTable id="mare-bookings-table" columns={columns} data={shown} getRowId={(booking) => booking.id} pageSize={8} onRowClick={(booking) => setOpenId(booking.id)} toolbar="columns" controls={ <> <FilterBar size="default" aria-label="Filters for bookings" className="hidden justify-end md:flex"> {filters.map((filter) => ( <FilterChip key={`${filter.field}-${filter.value}`} field={filter.field} value={filter.value} onRemove={() => { toggle(filter.field, filter.value) addFilter.current?.focus() }} /> ))} <FilterAdd triggerRef={addFilter}>{filterChoices}</FilterAdd> {filters.length > 0 && ( <FilterClear onClick={() => { setFilters([]) addFilter.current?.focus() }} /> )} </FilterBar> <FilterBar size="default" aria-label="Filters for bookings" className="md:hidden"> <FilterAdd label={filters.length ? `Filters, ${filters.length}` : "Filters"}>{filterChoices}</FilterAdd> </FilterBar> </> } phoneRow={(booking) => ({ title: booking.student, description: formatDate(booking.date), end: ( <Badge look="status" tone={tones[booking.status]}> {booking.status} </Badge> ), })} empty={ <Empty className="py-10"> <EmptyHeader> <EmptyMedia> <SearchIcon /> </EmptyMedia> <EmptyTitle>No booking matches</EmptyTitle> <EmptyDescription>Try another name, or clear the filters.</EmptyDescription> </EmptyHeader> </Empty> } /> </AppShell> )}
export { TableWithFiltersBlock }