UIPackage React Native
Search
Menu

Location Finder

Search and select pickup points, branches, or other locations, with an optional synchronized map.

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 location-finder

Source files

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

components/blocks/location-finder/LocationFinder.tsx
import * as React from 'react'
import { ActivityIndicator, View, type ViewProps } from 'react-native'
import { Alert, AlertActions, AlertDescription, AlertTitle } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { SearchBar } from '@/components/ui/search-bar'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface LocationFinderOption {
  id: string
  name: string
  address: string
  distance?: string
  detail?: string
  unavailableReason?: string
}

const sampleLocations: LocationFinderOption[] = [
  {
    id: 'canal',
    name: 'Canal Street Market',
    address: '18 Canal Street',
    distance: '0.4 mi',
    detail: 'Counter pickup until 8 PM',
  },
  {
    id: 'cedar',
    name: 'Cedar Station Lockers',
    address: '204 Cedar Avenue',
    distance: '1.1 mi',
    detail: 'Locker access all day',
  },
]

interface LocationFinderMap {
  results: readonly LocationFinderOption[]
  selectedId: string | null
  select: (id: string) => void
}

export interface LocationFinderProps extends ViewProps {
  className?: string
  title?: string
  searchLabel?: string
  locations?: readonly LocationFinderOption[]
  value?: string | null
  defaultValue?: string
  onValueChange?: (location: LocationFinderOption) => void
  renderMap?: (map: LocationFinderMap) => React.ReactNode
  loading?: boolean
  error?: string
  onRetry?: () => void
  confirmLabel?: string
  onConfirm?: (location: LocationFinderOption) => void
}

export function LocationFinder({
  className,
  title = 'Find a location',
  searchLabel = 'Search locations',
  locations = sampleLocations,
  value,
  defaultValue,
  onValueChange,
  renderMap,
  loading = false,
  error,
  onRetry,
  confirmLabel = 'Use this location',
  onConfirm,
  ...props
}: LocationFinderProps) {
  const [query, setQuery] = React.useState('')
  const [internal, setInternal] = React.useState(defaultValue)
  const selected = locations.find(
    (location) => location.id === (value === undefined ? internal : value) && !location.unavailableReason,
  )
  const term = query.trim().toLocaleLowerCase()
  const results = term
    ? locations.filter((location) =>
        [location.name, location.address, location.detail].some((field) => field?.toLocaleLowerCase().includes(term)),
      )
    : locations

  function select(id: string) {
    if (loading || error || selected?.id === id) return
    const location = locations.find((entry) => entry.id === id && !entry.unavailableReason)
    if (!location) return
    if (value === undefined) setInternal(id)
    onValueChange?.(location)
  }

  return (
    <View {...props} className={cn('gap-4', className)}>
      <Text size="title" accessibilityRole="header">
        {title}
      </Text>
      <SearchBar value={query} onChangeText={setQuery} accessibilityLabel={searchLabel} placeholder={searchLabel} />
      {loading ? (
        <View className="flex-row items-center gap-2" accessibilityRole="progressbar" accessibilityState={{ busy: true }}>
          <ActivityIndicator aria-hidden />
          <Text size="sm">Finding locations…</Text>
        </View>
      ) : null}
      {error && !loading ? (
        <Alert variant="destructive">
          <AlertTitle>Locations unavailable</AlertTitle>
          <AlertDescription>{error}</AlertDescription>
          {onRetry ? (
            <AlertActions>
              <Button variant="outline" onPress={onRetry}>
                Try again
              </Button>
            </AlertActions>
          ) : null}
        </Alert>
      ) : (
        <>
          {!error && results.length > 0 ? (
            <>
              {renderMap?.({ results, selectedId: selected?.id ?? null, select })}
              <RadioGroup
                accessibilityLabel="Locations"
                value={selected?.id ?? ''}
                onValueChange={select}
                disabled={loading}
              >
                {results.map((location) => (
                  <RadioGroupItem
                    key={location.id}
                    className={cn(
                      'items-start rounded-lg border border-border bg-card p-3',
                      selected?.id === location.id && 'border-ring bg-muted',
                    )}
                    value={location.id}
                    label={location.name}
                    accessibilityLabel={[
                      location.name,
                      location.address,
                      location.distance,
                      location.detail,
                      location.unavailableReason,
                    ]
                      .filter(Boolean)
                      .join('. ')}
                    disabled={Boolean(location.unavailableReason)}
                  >
                    <Text size="sm" variant="muted">
                      {location.address}
                    </Text>
                    {location.distance || location.detail ? (
                      <Text size="sm">{[location.distance, location.detail].filter(Boolean).join(' · ')}</Text>
                    ) : null}
                    {location.unavailableReason ? <Text size="sm">{location.unavailableReason}</Text> : null}
                  </RadioGroupItem>
                ))}
              </RadioGroup>
            </>
          ) : loading ? null : (
            <View className="gap-1 rounded-lg border border-border bg-card p-4">
              <Text className="font-semibold">{term ? 'No matching locations' : 'No locations available'}</Text>
              <Text variant="muted" size="sm">
                {term ? 'Try another name, address, or service.' : 'Check again later.'}
              </Text>
            </View>
          )}
        </>
      )}
      {onConfirm ? (
        <View className="gap-2">
          {selected ? <Text size="sm">Selected: {selected.name}</Text> : null}
          <Button
            disabled={!selected || loading || Boolean(error)}
            onPress={() => {
              if (selected && !loading && !error) onConfirm(selected)
            }}
          >
            {confirmLabel}
          </Button>
        </View>
      ) : null}
    </View>
  )
}
components/blocks/location-finder/index.ts
export * from './LocationFinder'

Dependencies

Browse delivery and transport