UIPackage React Native
Search
Menu

Pickup and Drop-off Editor

A controlled pickup and destination form with address validation, swapping, and caller-owned saved places.

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 pickup-dropoff-editor

Source files

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

components/blocks/pickup-dropoff-editor/PickupDropoffEditor.tsx
import { useRef, useState, type ComponentRef, type ReactNode } from 'react'
import { View, type ViewProps } from 'react-native'
import { ArrowUpDown, CircleDot, MapPin } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { FormField } from '@/components/ui/form-field'
import { Icon } from '@/components/ui/icon'
import { Input } from '@/components/ui/input'
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/segmented-control'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface PickupDropoffValue {
  pickup: string
  destination: string
}

type Endpoint = keyof PickupDropoffValue

const defaultLabels = {
  pickup: 'Pickup',
  destination: 'Destination',
  swap: 'Swap locations',
  confirm: 'Use these locations',
  confirming: 'Saving locations…',
  savedPlaces: 'Saved places',
  applySavedPlaceTo: 'Fill a saved place into',
  pickupRequired: 'Enter a pickup address.',
  destinationRequired: 'Enter a destination.',
  duplicateDestination: 'Choose a different destination.',
}

export interface PickupDropoffEditorProps extends Omit<ViewProps, 'children'> {
  className?: string
  value: PickupDropoffValue
  onValueChange: (value: PickupDropoffValue) => void
  /** Receives trimmed addresses once both are present and distinct. */
  onSubmit: (value: PickupDropoffValue) => void
  /** Caller-composed saved places. Apply an address to the currently targeted field. */
  savedPlaces?: (applyPlace: (address: string) => void, target: Endpoint, disabled: boolean) => ReactNode
  loading?: boolean
  disabled?: boolean
  labels?: Partial<typeof defaultLabels>
}

export function PickupDropoffEditor({
  value,
  onValueChange,
  onSubmit,
  savedPlaces,
  loading = false,
  disabled = false,
  labels,
  className,
  ...props
}: PickupDropoffEditorProps) {
  const copy = { ...defaultLabels, ...labels }
  const pickupInput = useRef<ComponentRef<typeof Input>>(null)
  const destinationInput = useRef<ComponentRef<typeof Input>>(null)
  const [target, setTarget] = useState<Endpoint>('destination')
  const [errors, setErrors] = useState<Partial<PickupDropoffValue>>({})
  const blocked = disabled || loading
  const confirmLabel = loading ? copy.confirming : copy.confirm

  function change(field: Endpoint, address: string) {
    if (blocked) return
    onValueChange({ ...value, [field]: address })
    setErrors({})
  }

  function submit() {
    if (blocked) return
    const pickup = value.pickup.trim()
    const destination = value.destination.trim()
    if (!pickup || !destination || pickup.toLocaleLowerCase() === destination.toLocaleLowerCase()) {
      const firstError = !pickup ? 'pickup' : 'destination'
      setErrors({
        pickup: pickup ? undefined : copy.pickupRequired,
        destination: !destination ? copy.destinationRequired : pickup ? copy.duplicateDestination : undefined,
      })
      setTarget(firstError)
      if (firstError === 'pickup') pickupInput.current?.focus()
      else destinationInput.current?.focus()
      return
    }
    setErrors({})
    onSubmit({ pickup, destination })
  }

  const places = savedPlaces?.(
    (address) => {
      if (blocked || !address.trim()) return
      change(target, address.trim())
      if (target === 'pickup') pickupInput.current?.blur()
      else destinationInput.current?.blur()
    },
    target,
    blocked,
  )

  return (
    <View {...props} className={cn('gap-[20px]', className)}>
      <View className="gap-[12px] rounded-lg bg-muted p-[16px]">
        <FormField label={copy.pickup} error={errors.pickup} required>
          {(field) => (
            <Input
              {...field}
              ref={pickupInput}
              value={value.pickup}
              editable={!blocked}
              onChangeText={(address) => change('pickup', address)}
              onFocus={() => setTarget('pickup')}
              onSubmitEditing={() => destinationInput.current?.focus()}
              returnKeyType="next"
              submitBehavior="submit"
              prefixIcon={<Icon icon={CircleDot} size={18} className="text-muted-foreground" />}
            />
          )}
        </FormField>
        <FormField label={copy.destination} error={errors.destination} required>
          {(field) => (
            <Input
              {...field}
              ref={destinationInput}
              value={value.destination}
              editable={!blocked}
              onChangeText={(address) => change('destination', address)}
              onFocus={() => setTarget('destination')}
              onSubmitEditing={submit}
              returnKeyType="done"
              prefixIcon={<Icon icon={MapPin} size={18} className="text-muted-foreground" />}
            />
          )}
        </FormField>
        <Button
          size="sm"
          variant="outline"
          accessibilityLabel={copy.swap}
          disabled={blocked || value.pickup === value.destination}
          onPress={() => {
            if (blocked || value.pickup === value.destination) return
            onValueChange({ pickup: value.destination, destination: value.pickup })
            setErrors({})
          }}
        >
          <Icon icon={ArrowUpDown} size={17} />
          {copy.swap}
        </Button>
      </View>

      {places ? (
        <View className="gap-[8px]">
          <Text className="font-semibold">{copy.savedPlaces}</Text>
          <Text size="sm" variant="muted">
            {copy.applySavedPlaceTo}
          </Text>
          <SegmentedControl
            accessibilityLabel={copy.applySavedPlaceTo}
            value={target}
            disabled={blocked}
            onValueChange={(next) => {
              if (next === 'pickup' || next === 'destination') setTarget(next)
            }}
          >
            <SegmentedControlItem value="pickup">{copy.pickup}</SegmentedControlItem>
            <SegmentedControlItem value="destination">{copy.destination}</SegmentedControlItem>
          </SegmentedControl>
          {places}
        </View>
      ) : null}

      <Button accessibilityLabel={confirmLabel} disabled={disabled} loading={loading} onPress={submit}>
        {confirmLabel}
      </Button>
    </View>
  )
}
components/blocks/pickup-dropoff-editor/index.ts
export { PickupDropoffEditor, type PickupDropoffEditorProps, type PickupDropoffValue } from './PickupDropoffEditor'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse delivery and transport