Booking Date
Appointment booking card: an open calendar with weekends and past days off, times that follow the chosen day, validation and a confirmation.
"use client"
import { useId, useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { addDays, format, isBefore, isWeekend, startOfDay } from "date-fns"
import { Controller, useForm, useWatch } from "react-hook-form"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import { Calendar } from "@/components/ui/calendar"
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldLabel,
FieldLegend,
FieldSet,
FieldTitle,
} from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
// The sample "today". It is a fixed date so the server and the browser
// render the same days; every other date here counts from it. In your app,
// read the real date where you load the data.
const TODAY = new Date(2026, 10, 17)
// The copy: change text here, not in the markup.
const COPY = {
title: "Book a studio session",
description: "Choose a day and a time to talk through your project.",
date: "Choose a day",
time: "Available times",
timezone: "30 minutes · Times shown in UTC",
submit: "Confirm time",
reset: "Start over",
unavailable: "Unavailable",
dateError: "Choose a weekday from November 17 onward.",
timeError: "Choose an available time.",
choose: "Choose a time to continue.",
picked: (day: string, time: string) =>
`${day} at ${time} UTC. Confirm to book it.`,
confirmed: (day: string, time: string) => `${day} at ${time} UTC`,
} as const
const SLOTS = ["10:00", "11:30", "14:00", "15:30"] as const
const INITIAL_DAY = addDays(TODAY, 2)
// Sample availability: Mondays lose the first slot, Thursdays the last.
function availableTimes(day: Date) {
return SLOTS.filter(
(slot) =>
!(day.getDay() === 1 && slot === "10:00") &&
!(day.getDay() === 4 && slot === "15:30")
)
}
const schema = z
.object({
day: z
.date({ error: COPY.dateError })
.refine(
(day) => !isWeekend(day) && !isBefore(startOfDay(day), TODAY),
COPY.dateError
),
time: z.string().min(1, COPY.timeError),
})
.superRefine(({ day, time }, context) => {
if (time && !availableTimes(day).some((slot) => slot === time)) {
context.addIssue({
code: "custom",
path: ["time"],
message: COPY.timeError,
})
}
})
type BookingValues = z.infer<typeof schema>
/**
* A booking card: an open calendar beside the times for the chosen day.
* Weekends and past days are off, a time that the new day does not offer is
* dropped, and the form validates before it confirms.
*
* The layout follows the card's own width, not the screen's (a container
* query on the Card), so it also fits a sidebar, a dialog or half a page.
*
* The words live in `COPY`, the times in `SLOTS` and the sample availability
* in `availableTimes`.
*/
export default function BookingDate01() {
const id = useId()
const [month, setMonth] = useState(INITIAL_DAY)
const [confirmed, setConfirmed] = useState<string | null>(null)
const form = useForm<BookingValues>({
resolver: zodResolver(schema),
defaultValues: { day: INITIAL_DAY, time: "" },
})
const day = useWatch({ control: form.control, name: "day" })
const selectedTime = useWatch({ control: form.control, name: "time" })
const times = availableTimes(day)
return (
<Card
role="region"
aria-labelledby={`${id}-title`}
className="@container/booking mx-auto w-full max-w-2xl"
>
<CardHeader>
<CardTitle>
<h2 id={`${id}-title`}>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent>
<form
id={`${id}-form`}
className="grid gap-6 @xl/booking:grid-cols-[auto_minmax(0,1fr)]"
onSubmit={form.handleSubmit(({ day: selectedDay, time }) => {
setConfirmed(
COPY.confirmed(format(selectedDay, "EEE, MMM d"), time)
)
})}
>
<Controller
control={form.control}
name="day"
render={({ field, fieldState }) => (
<Field data-invalid={fieldState.invalid}>
<FieldLabel id={`${id}-date-label`}>{COPY.date}</FieldLabel>
<Calendar
mode="single"
required
selected={field.value}
month={month}
onMonthChange={setMonth}
onSelect={(next) => {
field.onChange(next)
// A time the new day does not offer is dropped.
const currentTime = form.getValues("time")
const nextTime = availableTimes(next).some(
(slot) => slot === currentTime
)
? currentTime
: ""
form.setValue("time", nextTime, {
shouldDirty: true,
shouldValidate: form.formState.isSubmitted,
})
setConfirmed(null)
}}
disabled={[{ before: TODAY }, { dayOfWeek: [0, 6] }]}
aria-labelledby={`${id}-date-label`}
aria-describedby={
fieldState.invalid ? `${id}-date-error` : undefined
}
className="p-0"
/>
<FieldError
id={`${id}-date-error`}
errors={[fieldState.error]}
/>
</Field>
)}
/>
<Controller
control={form.control}
name="time"
render={({ field, fieldState }) => (
<FieldSet data-invalid={fieldState.invalid}>
<FieldLegend variant="label">{COPY.time}</FieldLegend>
<FieldDescription>
{format(day, "EEEE, MMMM d")}
</FieldDescription>
<RadioGroup
name={field.name}
value={field.value}
onValueChange={(next) => {
field.onChange(next)
setConfirmed(null)
}}
onBlur={field.onBlur}
aria-invalid={fieldState.invalid}
aria-describedby={
fieldState.invalid ? `${id}-time-error` : undefined
}
className="@xs/booking:grid-cols-2 @xl/booking:grid-cols-1"
>
{SLOTS.map((slot) => {
const available = times.includes(slot)
return (
<FieldLabel key={slot} htmlFor={`${id}-${slot}`}>
<Field
orientation="horizontal"
data-disabled={!available}
>
<FieldContent>
<FieldTitle className="tabular-nums">
{slot}
</FieldTitle>
{available ? null : (
<FieldDescription>
{COPY.unavailable}
</FieldDescription>
)}
</FieldContent>
<RadioGroupItem
id={`${id}-${slot}`}
value={slot}
disabled={!available}
aria-invalid={fieldState.invalid}
/>
</Field>
</FieldLabel>
)
})}
</RadioGroup>
<FieldError
id={`${id}-time-error`}
errors={[fieldState.error]}
/>
</FieldSet>
)}
/>
</form>
</CardContent>
<CardFooter className="flex-wrap justify-between gap-4 border-t">
<div className="grid gap-1">
<span className="text-xs text-muted-foreground">{COPY.timezone}</span>
<span role="status">
{confirmed ??
(selectedTime
? COPY.picked(format(day, "EEE, MMM d"), selectedTime)
: COPY.choose)}
</span>
</div>
<div className="flex items-center gap-2">
<Button
type="button"
variant="ghost"
onClick={() => {
form.reset({ day: INITIAL_DAY, time: "" })
setMonth(INITIAL_DAY)
setConfirmed(null)
}}
>
{COPY.reset}
</Button>
<Button type="submit" form={`${id}-form`}>
{COPY.submit}
</Button>
</div>
</CardFooter>
</Card>
)
}
Installation
npx shadcn@latest add @01orbital/booking-date-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/controls/booking-date-01/index.tsx: change them there, not in the markup. TODAY,SLOTSandINITIAL_DAY, also at the top of the file, hold its lists and settings.
Built from
npm: @hookform/resolvers, date-fns, react-hook-form, zod. The CLI installs them for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on