UIPackage React Native
Search
Menu

Ride Option Selector

Compare caller-owned ride quotes against a pickup and destination and confirm an available option.

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 ride-option-selector

Source files

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

components/blocks/ride-option-selector/RideOptionSelector.tsx
import { Children, Fragment, isValidElement, type ReactElement, type ReactNode } from 'react'
import { View, type ViewProps } from 'react-native'
import { CircleDot, MapPin } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { Icon } from '@/components/ui/icon'
import { RadioGroup, RadioGroupItem, type RadioGroupItemProps } from '@/components/ui/radio-group'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

interface RideOptionSelectorLabels {
  pickup: string
  destination: string
  options: string
  confirm: string
  confirming: string
}

export interface RideOptionSelectorProps extends ViewProps {
  className?: string
  /** RideOption children with unique, nonempty values. Fragments and arrays are supported. */
  children?: ReactNode
  pickup: string
  destination: string
  /** Pass null when the caller has no available selected quote. */
  value: string | null
  onValueChange: (value: string) => void
  onConfirm: (value: string) => void
  /** Disable the options and confirmation while the caller checks or books the selection. */
  loading?: boolean
  disabled?: boolean
  labels?: Partial<RideOptionSelectorLabels>
  emptyMessage?: string
}

function rideOptions(children: ReactNode): ReactElement<RideOptionProps>[] {
  return Children.toArray(children).flatMap((child) => {
    if (!isValidElement<{ children?: ReactNode }>(child)) return []
    if (child.type === Fragment) return rideOptions(child.props.children)
    return child.type === RideOption ? [child as ReactElement<RideOptionProps>] : []
  })
}

export function RideOptionSelector({
  pickup,
  destination,
  value,
  onValueChange,
  onConfirm,
  loading = false,
  disabled = false,
  labels,
  emptyMessage = 'No ride options are available. Try a different pickup location.',
  children,
  className,
  ...props
}: RideOptionSelectorProps) {
  const options = rideOptions(children)
  const selected = value ? options.find((option) => option.props.value === value && !option.props.disabled) : undefined
  const confirmLabel = loading ? (labels?.confirming ?? 'Confirming…') : (labels?.confirm ?? 'Confirm ride option')

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <View className="gap-[12px] rounded-lg bg-muted p-[16px]">
        <View className="flex-row items-start gap-[12px]">
          <Icon icon={CircleDot} size={18} className="mt-[2px] text-muted-foreground" />
          <View className="min-w-0 flex-1">
            <Text size="xs" variant="muted">
              {labels?.pickup ?? 'Pickup'}
            </Text>
            <Text className="font-medium">{pickup}</Text>
          </View>
        </View>
        <View className="flex-row items-start gap-[12px]">
          <Icon icon={MapPin} size={18} className="mt-[2px] text-muted-foreground" />
          <View className="min-w-0 flex-1">
            <Text size="xs" variant="muted">
              {labels?.destination ?? 'Destination'}
            </Text>
            <Text className="font-medium">{destination}</Text>
          </View>
        </View>
      </View>

      {options.length > 0 ? (
        <RadioGroup
          accessibilityLabel={labels?.options ?? 'Ride options'}
          className="gap-[8px]"
          value={selected?.props.value ?? ''}
          onValueChange={onValueChange}
          disabled={disabled || loading}
        >
          {children}
        </RadioGroup>
      ) : (
        <Text variant="muted">{emptyMessage}</Text>
      )}

      <Button
        accessibilityLabel={confirmLabel}
        disabled={disabled || !selected}
        loading={loading}
        onPress={() => {
          if (selected && !disabled && !loading) onConfirm(selected.props.value)
        }}
      >
        {confirmLabel}
      </Button>
    </View>
  )
}

export interface RideOptionProps extends Omit<RadioGroupItemProps, 'children'> {
  /** Localized fare and estimate, as received or formatted by the caller. */
  fare?: string
  pickupEstimate?: string
  detail?: string
}

export function RideOption({
  label,
  fare,
  pickupEstimate,
  detail,
  className,
  accessibilityLabel,
  ...props
}: RideOptionProps) {
  return (
    <RadioGroupItem
      {...props}
      label={label}
      accessibilityLabel={accessibilityLabel ?? [label, fare, pickupEstimate, detail].filter(Boolean).join(', ')}
      className={cn('rounded-lg border border-border bg-card', className)}
    >
      {fare || pickupEstimate ? (
        <View className="flex-row flex-wrap items-baseline gap-x-[10px] gap-y-[2px]">
          {fare ? (
            <Text size="sm" className="font-semibold" style={{ fontVariant: ['tabular-nums'] }}>
              {fare}
            </Text>
          ) : null}
          {pickupEstimate ? (
            <Text size="sm" variant="muted">
              {pickupEstimate}
            </Text>
          ) : null}
        </View>
      ) : null}
      {detail ? (
        <Text size="sm" variant="muted">
          {detail}
        </Text>
      ) : null}
    </RadioGroupItem>
  )
}
components/blocks/ride-option-selector/index.ts
export {
  RideOption,
  RideOptionSelector,
  type RideOptionProps,
  type RideOptionSelectorProps,
} from './RideOptionSelector'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse delivery and transport