UIPackage React Native
Search
Menu

Guest Count Picker

Controlled age-group guest counts with a shared total capacity and accessible group limits.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add guest-count-picker

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/guest-count-picker/GuestCountPicker.tsx
import { View, type ViewProps } from 'react-native'
import { QuantityStepper } from '@/components/ui/quantity-stepper'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface GuestCounts {
  adults: number
  children: number
  infants: number
}

export interface GuestCountGroup {
  label?: string
  /** The booking provider's own age range or attendance rule. */
  detail?: string
  max?: number
  incrementLabel?: string
  decrementLabel?: string
}

export interface GuestCountPickerProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  value: GuestCounts
  /** Total capacity across all three groups, including infants. */
  maxGuests?: number
  groups?: Partial<Record<keyof GuestCounts, GuestCountGroup>>
  disabled?: boolean
  formatCapacity?: (total: number, maxGuests: number) => string
  formatGroupLimit?: (max: number) => string
  onValueChange: (value: GuestCounts) => void
}

const groupDefinitions = [
  { key: 'adults', label: 'Adults', detail: 'Ages 13 and over', min: 1 },
  { key: 'children', label: 'Children', detail: 'Ages 2–12', min: 0 },
  { key: 'infants', label: 'Infants', detail: 'Under 2', min: 0 },
] as const
const formatter = new Intl.NumberFormat()

function count(value: number, minimum: number) {
  return Number.isSafeInteger(value) ? Math.max(minimum, value) : minimum
}

export function GuestCountPicker({
  className,
  title = 'Guests',
  value,
  maxGuests = 8,
  groups,
  disabled = false,
  formatCapacity,
  formatGroupLimit,
  onValueChange,
  ...props
}: GuestCountPickerProps) {
  const capacity = Number.isSafeInteger(maxGuests) ? Math.max(1, maxGuests) : 8
  const counts = {
    adults: count(value.adults, 1),
    children: count(value.children, 0),
    infants: count(value.infants, 0),
  }
  const total = counts.adults + counts.children + counts.infants
  const remaining = Math.max(0, capacity - total)

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <View className="gap-[4px]">
        <Text size="title" accessibilityRole="header">
          {title}
        </Text>
        <Text variant="muted">
          {formatCapacity?.(total, capacity) ?? `${formatter.format(total)} of ${formatter.format(capacity)} guests`}
        </Text>
      </View>
      <View className="rounded-lg border border-border bg-card">
        {groupDefinitions.map(({ key, label: defaultLabel, detail: defaultDetail, min }, index) => {
          const group = groups?.[key]
          const current = counts[key]
          const label = group?.label ?? defaultLabel
          const detail = group?.detail ?? defaultDetail
          const configuredLimit = group?.max
          const limit =
            configuredLimit !== undefined && Number.isSafeInteger(configuredLimit)
              ? Math.max(min, Math.min(capacity, configuredLimit))
              : capacity
          const max = Math.max(current, Math.min(limit, current + remaining))
          const rule =
            configuredLimit === undefined
              ? undefined
              : (formatGroupLimit?.(limit) ?? `Maximum ${formatter.format(limit)}`)
          return (
            <View
              key={key}
              className={cn(
                'flex-row flex-wrap items-center justify-between gap-[12px] p-[16px]',
                index > 0 && 'border-t border-border',
              )}
            >
              <View className="min-w-[95px] flex-1 gap-[4px]">
                <Text className="font-semibold text-card-foreground">{label}</Text>
                {detail ? (
                  <Text size="sm" variant="muted">
                    {detail}
                  </Text>
                ) : null}
                {rule ? (
                  <Text size="sm" variant="muted">
                    {rule}
                  </Text>
                ) : null}
              </View>
              <QuantityStepper
                accessibilityLabel={label}
                accessibilityHint={[detail, rule].filter(Boolean).join('. ') || undefined}
                decrementLabel={group?.decrementLabel ?? `Decrease ${label.toLocaleLowerCase()}`}
                incrementLabel={group?.incrementLabel ?? `Increase ${label.toLocaleLowerCase()}`}
                disabled={disabled}
                min={min}
                max={max}
                value={current}
                onValueChange={(next) => onValueChange({ ...counts, [key]: next })}
              />
            </View>
          )
        })}
      </View>
    </View>
  )
}
components/blocks/guest-count-picker/index.ts
export {
  GuestCountPicker,
  type GuestCountGroup,
  type GuestCountPickerProps,
  type GuestCounts,
} from './GuestCountPicker'

Dependencies

Browse shopping and reservations