UIPackage React Native
Search
Menu

Shipping Method Picker

Controlled shipping quotes with caller-localized fees, arrival estimates and unavailable options.

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 shipping-method-picker

Source files

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

components/blocks/shipping-method-picker/ShippingMethodPicker.tsx
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Spinner } from '@/components/ui/spinner'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export interface ShippingMethod {
  id: string
  label: string
  /** Localized price, including "Free" when the actual fee is zero. */
  fee?: string
  /** Localized carrier estimate, including fulfillment time when applicable. */
  arrivalRange?: string
  unavailableReason?: string
}

interface ShippingMethodPickerLabels {
  estimate: (range: string) => string
  estimateUnavailable: string
  priceUnavailable: string
  selectionUnavailable: string
  loading: string
  empty: string
  retry: string
}

export interface ShippingMethodPickerProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  destination?: string
  /** Caller-supplied rates for this destination, with unique and nonempty IDs. */
  methods?: readonly ShippingMethod[]
  value: string | null
  onValueChange: (value: string) => void
  loading?: boolean
  error?: string
  onRetry?: () => void
  labels?: Partial<ShippingMethodPickerLabels>
}

export function ShippingMethodPicker({
  className,
  title = 'Shipping method',
  destination,
  methods = [],
  value,
  onValueChange,
  loading,
  error,
  onRetry,
  labels,
  ...props
}: ShippingMethodPickerProps) {
  const options = methods.map(({ id, label, fee, arrivalRange, unavailableReason }) => {
    const quotedFee = fee?.trim()
    return {
      id,
      label,
      fee: quotedFee,
      estimate: arrivalRange?.trim(),
      unavailable:
        unavailableReason?.trim() ||
        (!quotedFee ? labels?.priceUnavailable?.trim() || 'Shipping price unavailable' : undefined),
    }
  })
  const selected = options.find((option) => option.id === value && !option.unavailable)
  const stale = !loading && !error && value !== null && !selected && options.some((option) => !option.unavailable)
  const selectionNotice = stale
    ? (labels?.selectionUnavailable ?? 'Your selected shipping method is unavailable. Choose another method.')
    : undefined
  useAnnouncement(loading ? undefined : error || selectionNotice)

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <View className="gap-[4px]">
        <Text size="title" accessibilityRole="header">
          {title}
        </Text>
        {destination ? <Text variant="muted">{destination}</Text> : null}
      </View>

      {loading ? (
        <Spinner className="self-start" accessibilityLabel={labels?.loading ?? 'Getting shipping rates'} />
      ) : error ? (
        <View className="gap-[8px]">
          <Text className="text-destructive" accessibilityRole="alert">
            {error}
          </Text>
          {onRetry ? (
            <Button variant="outline" accessibilityLabel={labels?.retry ?? 'Try again'} onPress={onRetry}>
              {labels?.retry ?? 'Try again'}
            </Button>
          ) : null}
        </View>
      ) : options.length > 0 ? (
        <RadioGroup accessibilityLabel={title} value={selected?.id ?? ''} onValueChange={onValueChange}>
          {options.map(({ id, label, fee, estimate, unavailable }) => {
            const arrival = estimate
              ? (labels?.estimate?.(estimate) ?? `Estimated delivery ${estimate}`)
              : (labels?.estimateUnavailable ?? 'Delivery estimate unavailable')
            return (
              <RadioGroupItem
                key={id}
                value={id}
                label={label}
                disabled={Boolean(unavailable)}
                accessibilityLabel={[label, ...(unavailable ? [unavailable] : [fee, arrival])]
                  .filter(Boolean)
                  .join(', ')}
                className={cn(
                  'rounded-lg border border-border bg-card focus:border-ring',
                  unavailable && 'bg-muted opacity-100',
                  selected?.id === id && 'border-primary',
                )}
              >
                {unavailable ? (
                  <Text size="sm" variant="muted">
                    {unavailable}
                  </Text>
                ) : (
                  <View className="gap-[4px]">
                    <Text size="sm" className="font-semibold">
                      {fee}
                    </Text>
                    <Text size="sm" variant="muted">
                      {arrival}
                    </Text>
                  </View>
                )}
              </RadioGroupItem>
            )
          })}
        </RadioGroup>
      ) : (
        <Text variant="muted">{labels?.empty ?? 'No shipping methods for this destination.'}</Text>
      )}
      {selectionNotice ? (
        <Text size="sm" variant="muted">
          {selectionNotice}
        </Text>
      ) : null}
    </View>
  )
}
components/blocks/shipping-method-picker/index.ts
export { ShippingMethodPicker, type ShippingMethod, type ShippingMethodPickerProps } from './ShippingMethodPicker'

Dependencies

Browse shopping and reservations