Skip to main content

Hotel booking

Check-in and check-out on a calendar with each night's price under the day, booked nights that a range can't cross, a two-night minimum, a room type, guests, and a total that adds up every night (weekends cost more).

Loading…
import { useMemo, useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { addDays, Button, DatePicker, formatDate, Heading, HStack, Select, Stepper, Text, toDateKey, useTheme } from 'patine-ui';
import type { DateRange, MarkedDate } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const MIN_NIGHTS = 2;
const ROOMS = [
  { label: 'Double, courtyard', value: 'double', description: 'From €118', extra: 0 },
  { label: 'Double, river view', value: 'river', description: 'From €148', extra: 30 },
  { label: 'Loft suite', value: 'loft', description: 'Sleeps 4 · from €208', extra: 90 },
];
// Fridays and Saturdays cost more.
const nightly = (day: Date) => (day.getDay() === 5 || day.getDay() === 6 ? 156 : 118);
const short = (day: Date) => formatDate(day, undefined, { day: 'numeric', month: 'short' });

export default function HotelBooking() {
  const { colors } = useTheme();
  const today = useMemo(() => new Date(), []);
  const [range, setRange] = useState<DateRange>({ start: null, end: null });
  const [room, setRoom] = useState('river');
  const [adults, setAdults] = useState(2);
  const [children, setChildren] = useState(0);

  // A price under every day for three months, with a few nights already booked.
  const marks = useMemo(() => {
    const out: Record<string, MarkedDate> = {};
    for (let i = 0; i < 90; i++) {
      const day = addDays(today, i);
      out[toDateKey(day)] = i % 11 === 6 || i % 11 === 7 ? { disabled: true, label: '—' } : { label: `€${nightly(day)}` };
    }
    return out;
  }, [today]);

  const extra = ROOMS.find((r) => r.value === room)?.extra ?? 0;
  const nights = range.start && range.end ? Math.round((+range.end - +range.start) / 864e5) : 0;
  let total = 0;
  for (let i = 0; i < nights; i++) total += nightly(addDays(range.start as Date, i)) + extra;

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <ScrollView contentContainerStyle={styles.content}>
        <View style={styles.titles}>
          <Heading level={3}>Casa do Rio</Heading>
          <HStack gap="xs" align="center">
            <Icon name="pin" size="sm" />
            <Text size="sm" color="textSecondary">
              Ribeira, Porto · 4.8 (326)
            </Text>
          </HStack>
        </View>

        <DatePicker
          mode="range"
          value={range}
          onValueChange={setRange}
          minDate={today}
          maxDate={addDays(today, 89)}
          minRangeDays={MIN_NIGHTS}
          markedDates={marks}
          dayHeight={46}
        />

        <Select label="Room" sheetTitle="Choose a room" options={ROOMS} value={room} onValueChange={setRoom} />

        <View style={styles.guests}>
          <HStack justify="space-between" align="center">
            <View>
              <Text weight="medium">Adults</Text>
              <Text size="sm" color="textSecondary">
                13 or older
              </Text>
            </View>
            <Stepper value={adults} onValueChange={setAdults} min={1} max={4} accessibilityLabel="Adults" />
          </HStack>
          <HStack justify="space-between" align="center">
            <View>
              <Text weight="medium">Children</Text>
              <Text size="sm" color="textSecondary">
                Ages 2–12
              </Text>
            </View>
            <Stepper value={children} onValueChange={setChildren} min={0} max={4 - adults} accessibilityLabel="Children" />
          </HStack>
        </View>
      </ScrollView>

      <HStack justify="space-between" align="center" gap="md" style={[styles.footer, { borderColor: colors.border }]}>
        <View style={styles.flex}>
          <Text weight="semibold">{nights ? `€${total} total` : `Min. ${MIN_NIGHTS} nights`}</Text>
          <Text size="sm" color="textSecondary" numberOfLines={1}>
            {range.start && range.end ? `${short(range.start)} → ${short(range.end)} · ${nights} nights` : 'Pick your dates'}
          </Text>
        </View>
        <Button size="lg" disabled={!nights}>
          Reserve
        </Button>
      </HStack>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  content: { gap: 16, padding: 16 },
  titles: { gap: 4 },
  guests: { gap: 16, paddingVertical: 4 },
  footer: { padding: 16, borderTopWidth: StyleSheet.hairlineWidth },
  flex: { flex: 1 },
});

Components used​

How it works​

  • markedDates puts the price under each day and marks booked days disabled. A range never crosses a disabled day: tapping past one starts a new range.
  • minRangeDays ignores a check-out tap that's too close to check-in.
  • dayHeight={46} leaves room for the price.
  • The total adds each night with addDays, which is safe across daylight-saving changes, plus the room's supplement.
  • The steppers share a limit. Children's max is what's left of four once the adults are counted.

As a sheet​

On a busier screen, open the calendar from a summary bar instead. The confirm button stays disabled until both dates are picked, and in Material the headline above the calendar shows the range as it's picked:

<DatePickerSheet
open={open}
onOpenChange={setOpen}
title="Select dates"
confirmLabel="Save dates"
mode="range"
value={range}
onConfirm={setRange}
minDate={today}
minRangeDays={MIN_NIGHTS}
markedDates={marks}
dayHeight={50}
/>