UIPackage React Native
Search
Menu

Cart Line Item

Editable cart line with a thumbnail, formatted total, bounded quantity and a separate remove action.

Select Add to cart, then scroll to Cart to edit the line item.

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 cart-line-item

Source files

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

components/blocks/cart-line-item/CartLineItem.tsx
import { View, type ViewProps } from 'react-native'
import { ImageOff } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { Icon } from '@/components/ui/icon'
import { Image, ImageFallback, ImageSource, type ImageSourceValue } from '@/components/ui/image'
import { QuantityStepper } from '@/components/ui/quantity-stepper'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

const numberFormatter = new Intl.NumberFormat()

export interface CartLineItemProps extends Omit<ViewProps, 'children'> {
  className?: string
  title: string
  detail?: string
  /** The line total from the caller, already formatted for currency and locale. */
  total: string
  /** The actual positive quantity currently in the cart. */
  quantity: number
  /** Supply a small thumbnail source for the cart layout. */
  image?: ImageSourceValue
  availableQuantity?: number
  unavailableReason?: string
  updating?: boolean
  removing?: boolean
  quantityLabel?: string
  removeLabel?: string
  removeAccessibilityLabel?: string
  soldOutLabel?: string
  formatLimit?: (available: number) => string
  onQuantityChange?: (quantity: number) => void
  onRemove?: () => void
}

export function CartLineItem({
  className,
  title,
  detail,
  total,
  quantity,
  image,
  availableQuantity,
  unavailableReason,
  updating = false,
  removing = false,
  quantityLabel = 'Quantity',
  removeLabel = 'Remove',
  removeAccessibilityLabel = `${removeLabel} ${title}`,
  soldOutLabel = 'Sold out',
  formatLimit,
  onQuantityChange,
  onRemove,
  ...props
}: CartLineItemProps) {
  const stock =
    availableQuantity != null && Number.isSafeInteger(availableQuantity) ? Math.max(0, availableQuantity) : undefined
  const blocked = stock === 0 || Boolean(unavailableReason)
  const unavailable = unavailableReason || (stock === 0 ? soldOutLabel : undefined)
  const shortage =
    stock !== undefined && stock > 0 && quantity > stock
      ? (formatLimit?.(stock) ?? `Only ${numberFormatter.format(stock)} available. Reduce the quantity to continue.`)
      : undefined
  const notice = unavailable || shortage
  const quantityValid = Number.isSafeInteger(quantity) && quantity > 0
  useAnnouncement(notice)

  return (
    <View {...props} className={cn('gap-[12px] rounded-lg border border-border bg-card p-[16px]', className)}>
      <View className="flex-row items-start gap-[12px]">
        {image !== undefined ? (
          <Image aria-hidden source={image} className="shrink-0" style={{ width: 72, aspectRatio: 1 }}>
            <ImageSource />
            <ImageFallback className="p-0">
              <Icon icon={ImageOff} size={18} className="text-muted-foreground" />
            </ImageFallback>
          </Image>
        ) : null}
        <View className="min-w-0 flex-1 gap-[4px]">
          <Text className="font-semibold text-card-foreground">{title}</Text>
          {detail ? (
            <Text size="sm" variant="muted">
              {detail}
            </Text>
          ) : null}
          <Text size="sm" className="font-semibold text-card-foreground" style={{ fontVariant: ['tabular-nums'] }}>
            {total}
          </Text>
        </View>
      </View>
      {notice ? (
        <Text size="sm" className="text-destructive">
          {notice}
        </Text>
      ) : null}
      {quantityValid || onRemove ? (
        <View className="flex-row flex-wrap items-center justify-between gap-[8px]">
          {quantityValid ? (
            onQuantityChange ? (
              <QuantityStepper
                accessibilityLabel={`${quantityLabel}, ${title}`}
                value={quantity}
                min={1}
                max={Math.max(quantity, stock ?? 99)}
                disabled={updating || removing || blocked}
                onValueChange={onQuantityChange}
              />
            ) : (
              <Text size="sm" variant="muted">{`${quantityLabel}: ${numberFormatter.format(quantity)}`}</Text>
            )
          ) : null}
          {onRemove ? (
            <Button
              variant="ghost"
              accessibilityLabel={removeAccessibilityLabel}
              disabled={updating}
              loading={removing}
              textClassName="text-destructive"
              onPress={() => {
                if (!updating && !removing) onRemove()
              }}
            >
              {removeLabel}
            </Button>
          ) : null}
        </View>
      ) : null}
    </View>
  )
}
components/blocks/cart-line-item/index.ts
export { CartLineItem, type CartLineItemProps } from './CartLineItem'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse shopping and reservations