UIPackage React Native
Search
Menu

Fulfillment Window Picker

Reserve an available pickup or delivery window with current availability, loading, and retry states.

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 fulfillment-window-picker

Source files

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

components/blocks/fulfillment-window-picker/FulfillmentWindowPicker.tsx
import * as React from 'react'
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 { cn } from '@/lib/utils'

export interface FulfillmentWindow {
  id: string
  /** Localized range in the fulfillment location's time zone. */
  label: string
  detail?: string
  /** Readable reason for a window that the user cannot reserve. */
  unavailableReason?: string
}

export interface FulfillmentWindowPickerProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  description?: string
  /** Supply the currently viewed day's windows. Window IDs must be unique and nonempty. */
  windows?: readonly FulfillmentWindow[]
  value?: string | null
  defaultValue?: string
  loading?: boolean
  pending?: boolean
  error?: string
  labels?: Partial<Record<'confirm' | 'loading' | 'empty' | 'retry', string>>
  onValueChange?: (windowId: string) => void
  onConfirm: (windowId: string) => void
  onRetry?: () => void
}

export function FulfillmentWindowPicker({
  className,
  title = 'Pickup window',
  description,
  windows = [],
  value,
  defaultValue,
  loading = false,
  pending = false,
  error,
  labels,
  onValueChange,
  onConfirm,
  onRetry,
  ...props
}: FulfillmentWindowPickerProps) {
  const [internal, setInternal] = React.useState(defaultValue)
  const current = value === undefined ? internal : value
  const selected = windows.find(({ id, unavailableReason }) => id && id === current && !unavailableReason)

  React.useEffect(() => {
    if (value === undefined && internal !== undefined && !selected && !loading && !error) setInternal(undefined)
  }, [value, internal, selected, loading, error])

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

      {loading ? (
        <Spinner accessibilityLabel={labels?.loading ?? 'Loading pickup windows'} className="self-start" />
      ) : error ? (
        <View className="gap-[8px]">
          <Text accessibilityRole="alert" className="text-destructive">
            {error}
          </Text>
          {onRetry ? (
            <Button variant="outline" accessibilityLabel={labels?.retry ?? 'Try again'} onPress={onRetry}>
              {labels?.retry ?? 'Try again'}
            </Button>
          ) : null}
        </View>
      ) : windows.length === 0 ? (
        <Text variant="muted">{labels?.empty ?? 'No pickup windows are available.'}</Text>
      ) : (
        <>
          <RadioGroup
            accessibilityLabel={title}
            value={selected?.id ?? ''}
            disabled={pending}
            onValueChange={(next) => {
              if (value === undefined) setInternal(next)
              onValueChange?.(next)
            }}
          >
            {windows.map(({ id, label, detail, unavailableReason }) => (
              <RadioGroupItem
                key={id}
                value={id}
                label={label}
                disabled={!id || Boolean(unavailableReason)}
                accessibilityLabel={[label, detail, unavailableReason].filter(Boolean).join(', ')}
                className={cn('rounded-lg border border-border bg-card', selected?.id === id && 'border-primary')}
              >
                {detail ? (
                  <Text size="sm" variant="muted">
                    {detail}
                  </Text>
                ) : null}
                {unavailableReason ? (
                  <Text size="sm" variant="muted">
                    {unavailableReason}
                  </Text>
                ) : null}
              </RadioGroupItem>
            ))}
          </RadioGroup>
          <Button
            disabled={!selected}
            loading={pending}
            accessibilityLabel={labels?.confirm ?? 'Reserve pickup window'}
            onPress={() => {
              if (selected && !pending) onConfirm(selected.id)
            }}
          >
            {labels?.confirm ?? 'Reserve pickup window'}
          </Button>
        </>
      )}
    </View>
  )
}
components/blocks/fulfillment-window-picker/index.ts
export {
  FulfillmentWindowPicker,
  type FulfillmentWindow,
  type FulfillmentWindowPickerProps,
} from './FulfillmentWindowPicker'

Dependencies

Browse shopping and reservations