UIPackage React Native
Search
Menu

Item Substitution

Compare an out-of-stock order item with its proposed replacement, then accept or request a refund.

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 item-substitution

Source files

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

components/blocks/item-substitution/ItemSubstitution.tsx
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export interface SubstitutionItem {
  title: string
  detail?: string
  /** Localized price for the full line, including the relevant quantity. */
  price: string
}

export type SubstitutionDecision = 'accept' | 'refund'
export type ItemSubstitutionStatus = 'review' | 'accepted' | 'refund-requested'

interface SubstitutionLabels {
  original: string
  replacement: string
  noReplacement: string
  estimate: string
  accept: string
  refund: string
  accepted: string
  refundRequested: string
}

export interface ItemSubstitutionProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  original: SubstitutionItem
  replacement: SubstitutionItem | null
  /** An estimated, already formatted price delta, for example "+$0.55". */
  priceDifference?: string
  status?: ItemSubstitutionStatus
  pending?: SubstitutionDecision
  error?: string
  labels?: Partial<SubstitutionLabels>
  onDecision?: (decision: SubstitutionDecision) => void
}

export function ItemSubstitution({
  className,
  title = 'Review replacement',
  original,
  replacement,
  priceDifference,
  status = 'review',
  pending,
  error,
  labels,
  onDecision,
  ...props
}: ItemSubstitutionProps) {
  const reviewing = status === 'review'
  const outcome =
    status === 'accepted'
      ? (labels?.accepted ?? 'Replacement accepted')
      : status === 'refund-requested'
        ? (labels?.refundRequested ?? 'Refund requested')
        : undefined
  useAnnouncement(outcome ?? error)

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <Text size="title" accessibilityRole="header">
        {outcome ?? title}
      </Text>
      <View className="gap-[12px] rounded-lg border border-border bg-card p-[16px]">
        <View className="gap-[4px]">
          <Text size="sm" variant="muted">
            {labels?.original ?? 'Unavailable item'}
          </Text>
          <View className="flex-row flex-wrap items-start justify-between gap-[8px]">
            <View className="min-w-[120px] flex-1 gap-[4px]">
              <Text className="font-semibold text-card-foreground">{original.title}</Text>
              {original.detail ? (
                <Text size="sm" variant="muted">
                  {original.detail}
                </Text>
              ) : null}
            </View>
            <Text className="shrink font-semibold text-card-foreground" style={{ fontVariant: ['tabular-nums'] }}>
              {original.price}
            </Text>
          </View>
        </View>
        {replacement && status !== 'refund-requested' ? (
          <View className="gap-[4px] border-t border-border pt-[12px]">
            <Text size="sm" variant="muted">
              {labels?.replacement ?? 'Replacement'}
            </Text>
            <View className="flex-row flex-wrap items-start justify-between gap-[8px]">
              <View className="min-w-[120px] flex-1 gap-[4px]">
                <Text className="font-semibold text-card-foreground">{replacement.title}</Text>
                {replacement.detail ? (
                  <Text size="sm" variant="muted">
                    {replacement.detail}
                  </Text>
                ) : null}
              </View>
              <Text className="shrink font-semibold text-card-foreground" style={{ fontVariant: ['tabular-nums'] }}>
                {replacement.price}
              </Text>
            </View>
          </View>
        ) : null}
      </View>

      {reviewing ? (
        <>
          {replacement && priceDifference ? (
            <View className="flex-row flex-wrap items-start justify-between gap-[8px]">
              <Text size="sm" variant="muted" className="shrink">
                {labels?.estimate ?? 'Estimated price difference'}
              </Text>
              <Text size="sm" className="shrink font-semibold" style={{ fontVariant: ['tabular-nums'] }}>
                {priceDifference}
              </Text>
            </View>
          ) : !replacement ? (
            <Text size="sm" variant="muted">
              {labels?.noReplacement ?? 'A replacement isn’t available.'}
            </Text>
          ) : null}
          {error ? (
            <Text size="sm" accessibilityRole="alert" className="text-destructive">
              {error}
            </Text>
          ) : null}
          {onDecision ? (
            <View className="gap-[8px]">
              {replacement ? (
                <Button
                  accessibilityLabel={labels?.accept ?? 'Accept replacement'}
                  disabled={pending === 'refund'}
                  loading={pending === 'accept'}
                  onPress={() => {
                    if (!pending) onDecision('accept')
                  }}
                >
                  {labels?.accept ?? 'Accept replacement'}
                </Button>
              ) : null}
              <Button
                accessibilityLabel={labels?.refund ?? 'Request refund'}
                variant={replacement ? 'outline' : 'default'}
                disabled={pending === 'accept'}
                loading={pending === 'refund'}
                onPress={() => {
                  if (!pending) onDecision('refund')
                }}
              >
                {labels?.refund ?? 'Request refund'}
              </Button>
            </View>
          ) : null}
        </>
      ) : null}
    </View>
  )
}
components/blocks/item-substitution/index.ts
export {
  ItemSubstitution,
  type ItemSubstitutionProps,
  type ItemSubstitutionStatus,
  type SubstitutionDecision,
  type SubstitutionItem,
} from './ItemSubstitution'

Dependencies

Browse shopping and reservations