Number 28: Date range with nightly prices
Specification
- Catalogue
- No. 28 of 110
- Section
- Pickers and booking
- Interaction
- Press
- Element
- Picker
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
A month where each night shows its price, sold-out nights are marked, and the total adds up as you pick.
A guest sees what a stay costs before they commit, and why a range will not work. Sold-out nights say Sold and are struck through, so colour is never the only sign.
Arrival, then departure; a range over a sold-out night is refused with a reason. Minimum stay of two nights; the range previews on hover. Arrow keys move a day or a week; each date reads its price or Sold out. Total rolls with NumberFlow; fixed sample month, no dependence on today's date.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useRef, useState, type KeyboardEvent } from "react";
import { Num } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./stay-picker.css";
const gbp0 = { style: "currency", currency: "GBP", maximumFractionDigits: 0 } as const;
const MONTH_START = 1;
// 1 December 2026 is a Tuesday: index 1 in a Monday-first week
const DAYS_IN_MONTH = 31;
const SOLD = new Set([5, 6, 12, 19, 20, 31]);
const MIN_NIGHTS = 2;
const WEEKDAY = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
function rate(d: number): number {
if (d >= 24 && d <= 26) return 150;
const wd = (MONTH_START + d - 1) % 7;
return wd === 4 || wd === 5 ? 120 : 95;
}
function StayPickerDemo() {
const [checkIn, setIn] = useState<number | null>(null);
const [checkOut, setOut] = useState<number | null>(null);
const [hover, setHover] = useState<number | null>(null);
const [focusDay, setFocusDay] = useState(1);
const [msg, setMsg] = useState("Choose your arrival date");
const refs = useRef<(HTMLButtonElement | null)[]>([]);
const nightsOk = (a: number, b: number) => {
for (let d = a; d < b; d++) if (SOLD.has(d)) return false;
return true;
};
const choose = (d: number) => {
if (checkIn === null || checkOut !== null || d <= checkIn) {
if (SOLD.has(d)) {
setMsg(`${d} December is sold out. Choose another arrival date.`);
return;
}
setIn(d);
setOut(null);
setMsg(`Arriving ${d} December. Now choose your departure date.`);
return;
}
if (!nightsOk(checkIn, d)) {
setMsg("A night in that range is sold out. Choose an earlier departure.");
return;
}
if (d - checkIn < MIN_NIGHTS) {
setMsg(`The minimum stay is ${MIN_NIGHTS} nights.`);
return;
}
setOut(d);
setMsg(`${d - checkIn} nights selected.`);
};
const onKey = (e: KeyboardEvent, d: number) => {
const delta: Record<string, number> = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -7, ArrowDown: 7 };
const m = delta[e.key];
if (m === undefined) return;
e.preventDefault();
const n = Math.min(DAYS_IN_MONTH, Math.max(1, d + m));
setFocusDay(n);
refs.current[n]?.focus();
};
const end = checkOut ?? (checkIn !== null && hover !== null && hover > checkIn ? hover : null);
let total = 0;
if (checkIn !== null && checkOut !== null) for (let d = checkIn; d < checkOut; d++) total += rate(d);
const cells: (number | null)[] = [...Array(MONTH_START).fill(null), ...Array.from({ length: DAYS_IN_MONTH }, (_, i) => i + 1)];
while (cells.length % 7) cells.push(null);
const weeks = Array.from({ length: cells.length / 7 }, (_, w) => cells.slice(w * 7, w * 7 + 7));
return (
<div className="dm">
<div className="stay">
<div className="stay-head">
<strong>December 2026</strong>
<span>Prices per night</span>
</div>
<table className="stay-cal" onMouseLeave={() => setHover(null)}>
<thead>
<tr>
{["M", "T", "W", "T", "F", "S", "S"].map((d, i) => (
<th key={i} scope="col" abbr={WEEKDAY[i]}>
{d}
</th>
))}
</tr>
</thead>
<tbody>
{weeks.map((w, wi) => (
<tr key={wi}>
{w.map((d, i) =>
d === null ? (
<td key={i} />
) : (
<td key={i}>
<button
ref={(el) => {
refs.current[d] = el;
}}
type="button"
className="stay-day"
tabIndex={d === focusDay ? 0 : -1}
data-sold={SOLD.has(d) || undefined}
data-edge={d === checkIn || d === checkOut || undefined}
data-in={(checkIn !== null && end !== null && d > checkIn && d < end) || undefined}
data-start={(checkIn !== null && end !== null && d === checkIn && end > d) || undefined}
data-end={(checkIn !== null && end !== null && d === end && end > checkIn) || undefined}
aria-pressed={d === checkIn || d === checkOut}
aria-label={`${WEEKDAY[(MONTH_START + d - 1) % 7]} ${d} December, ${SOLD.has(d) ? "sold out" : `£${rate(d)} a night`}${d === checkIn ? ", arrival" : ""}${d === checkOut ? ", departure" : ""}`}
onClick={() => {
setFocusDay(d);
choose(d);
}}
onMouseEnter={() => setHover(d)}
onKeyDown={(e) => onKey(e, d)}
>
<span className="stay-num">{d}</span>
<span className="stay-rate">{SOLD.has(d) ? "Sold" : `£${rate(d)}`}</span>
</button>
</td>
),
)}
</tr>
))}
</tbody>
</table>
<div className="stay-foot">
<p aria-live="polite">{msg}</p>
<strong>{checkOut !== null ? <Num value={total} format={gbp0} /> : null}</strong>
</div>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: ".stay-day:not([data-sold])", nth: 3, wait: 600 },
{ hover: ".stay-day:not([data-sold])", nth: 4, wait: 250 },
{ hover: ".stay-day:not([data-sold])", nth: 5, wait: 250 },
{ hover: ".stay-day:not([data-sold])", nth: 6, wait: 400 },
{ click: ".stay-day:not([data-sold])", nth: 6, wait: 1800 },
];
export default function StayPickerEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Date range with nightly prices" sample tour={TOUR}>
<StayPickerDemo />
</PieceStage>
);
}