Skip to main content

Checkout

One screen from bag to paid: the order in a card, the shipping address, the delivery speed (its price flows into the total), the card to charge, and a Pay button that spins while the payment runs and ends in a toast with the order number. Tap Pay with an empty field to see the errors.

Loading…
import { useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import {
  Button,
  Card,
  CardContent,
  CardHeader,
  CardTitle,
  Header,
  HStack,
  RadioGroup,
  Select,
  Separator,
  Text,
  TextField,
  Toaster,
  toast,
  useTheme,
} from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const ITEMS = [
  { name: 'Linen overshirt · M', price: 89 },
  { name: 'Merino crew socks × 2', price: 24 },
];
const DELIVERY = [
  { value: 'standard', label: 'Standard', description: 'Thu 21 – Mon 25 Nov', price: 0 },
  { value: 'express', label: 'Express', description: 'Tomorrow by 18:00', price: 9 },
  { value: 'pickup', label: 'Pick up in store', description: 'Rua Garrett 42, Lisbon · today', price: 0 },
];
const CARDS = [
  { label: 'Visa ending 4242', value: 'visa', description: 'Expires 08/28' },
  { label: 'Mastercard ending 5100', value: 'mc', description: 'Expires 02/27' },
  { label: 'Add a new card', value: 'new' },
];
const money = (amount: number) => (amount ? `€${amount.toFixed(2)}` : 'Free');

export default function Checkout() {
  const { colors } = useTheme();
  const [address, setAddress] = useState({ name: 'Inês Costa', street: '', city: 'Lisbon', postcode: '' });
  const [delivery, setDelivery] = useState('standard');
  const [card, setCard] = useState<string | null>('visa');
  const [submitted, setSubmitted] = useState(false);
  const [paying, setPaying] = useState(false);

  const shipping = DELIVERY.find((d) => d.value === delivery)?.price ?? 0;
  const total = ITEMS.reduce((sum, item) => sum + item.price, 0) + shipping;
  const needsAddress = delivery !== 'pickup';
  const errors = {
    street: needsAddress && !address.street.trim() ? 'Enter a street and number' : undefined,
    postcode: needsAddress && !/^\d{4}-\d{3}$/.test(address.postcode) ? 'Use the format 1200-195' : undefined,
  };
  const show = (error?: string) => (submitted ? error : undefined);

  const pay = () => {
    setSubmitted(true);
    if (errors.street || errors.postcode) return;
    setPaying(true);
    setTimeout(() => {
      setPaying(false);
      toast.success('Order placed', { description: `#LX-20418 · ${money(total)} charged to your card` });
    }, 1400);
  };

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <Header title="Checkout" onBack={() => {}} />
      <ScrollView contentContainerStyle={styles.content} keyboardShouldPersistTaps="handled">
        <Card>
          <CardHeader>
            <CardTitle>Order summary</CardTitle>
          </CardHeader>
          <CardContent style={styles.rows}>
            {ITEMS.map((item) => (
              <HStack key={item.name} justify="space-between" gap="md">
                <Text color="textSecondary" style={styles.flex}>
                  {item.name}
                </Text>
                <Text>{money(item.price)}</Text>
              </HStack>
            ))}
            <HStack justify="space-between">
              <Text color="textSecondary">Delivery</Text>
              <Text>{money(shipping)}</Text>
            </HStack>
            <Separator />
            <HStack justify="space-between">
              <Text weight="semibold">Total</Text>
              <Text weight="semibold">{money(total)}</Text>
            </HStack>
          </CardContent>
        </Card>

        <Text weight="semibold" size="lg">
          Delivery
        </Text>
        <RadioGroup
          aria-label="Delivery"
          value={delivery}
          onValueChange={setDelivery}
          options={DELIVERY.map(({ value, label, description, price }) => ({
            value,
            label: `${label} · ${money(price)}`,
            description,
          }))}
        />

        {needsAddress ? (
          <View style={styles.rows}>
            <Text weight="semibold" size="lg">
              Shipping address
            </Text>
            <TextField
              label="Full name"
              value={address.name}
              onValueChange={(name) => setAddress({ ...address, name })}
              autoComplete="name"
            />
            <TextField
              label="Street and number"
              placeholder="Rua da Rosa 118, 2º Esq"
              value={address.street}
              onValueChange={(street) => setAddress({ ...address, street })}
              error={show(errors.street)}
              autoComplete="street-address"
            />
            <HStack gap="md">
              <TextField
                label="Postcode"
                placeholder="1200-195"
                value={address.postcode}
                onValueChange={(postcode) => setAddress({ ...address, postcode })}
                error={show(errors.postcode)}
                keyboardType="numbers-and-punctuation"
                autoComplete="postal-code"
                style={styles.flex}
              />
              <TextField
                label="City"
                value={address.city}
                onValueChange={(city) => setAddress({ ...address, city })}
                style={styles.flex}
              />
            </HStack>
          </View>
        ) : null}

        <Select label="Pay with" sheetTitle="Payment method" options={CARDS} value={card} onValueChange={setCard} />
      </ScrollView>

      <View style={[styles.footer, { borderColor: colors.border, backgroundColor: colors.background }]}>
        <Button size="lg" loading={paying} onPress={pay} startIcon={<Icon name="lock" size="sm" color="onPrimary" />}>
          {paying ? 'Paying…' : `Pay ${money(total)}`}
        </Button>
      </View>
      <Toaster />
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  content: { gap: 16, padding: 20 },
  rows: { gap: 12 },
  flex: { flex: 1 },
  footer: { padding: 16, borderTopWidth: StyleSheet.hairlineWidth },
});

Components used​

  • Card: the order summary, with CardHeader, CardTitle and CardContent
  • RadioGroup: delivery speed, each option with a description
  • TextField: the address, with errors shown after the first Pay
  • Select: the saved cards, in a sheet on phones and a dropdown on the web
  • Button: loading keeps it busy and blocks a second tap while the payment runs
  • Toast: toast.success() and the <Toaster /> that shows it
  • Header, HStack, Separator, Text, Patine Icons

How it works​

  • The total follows the delivery. Its price is looked up from the picked option on every render, so the card and the button never disagree.
  • Pick-up hides the address. Its fields and their errors only apply when something ships.
  • The button stays in reach. It sits outside the scroll view, pinned above the home indicator, while the form scrolls under it.