UIPackage React Native
Search

Saved Items

Saved-product list with separate stock-aware add-to-cart and remove actions and a useful empty state.

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 saved-items

Source files

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

components/blocks/saved-items/SavedItems.tsx
import { Children, Fragment, isValidElement, type ReactElement } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import {
  List,
  ListItem,
  ListItemContent,
  ListItemDescription,
  ListItemTitle,
  ListSeparator,
} from '@/components/ui/list'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface SavedItemsProps extends ViewProps {
  className?: string
  title?: string
  emptyMessage?: string
  emptyAction?: ReactElement
}

/** Group a short saved list. For unbounded collections, render SavedItem in the caller's virtualized list. */
export function SavedItems({
  className,
  title = 'Saved items',
  emptyMessage = 'You haven’t saved any items yet.',
  emptyAction,
  children,
  ...props
}: SavedItemsProps) {
  const rows = Children.toArray(children).filter(isValidElement)

  return (
    <View {...props} className={cn('gap-[12px]', className)}>
      <Text size="title" accessibilityRole="header">
        {title}
      </Text>
      {rows.length ? (
        <List>
          {rows.map((row, index) => (
            <Fragment key={row.key}>
              {index > 0 ? <ListSeparator /> : null}
              {row}
            </Fragment>
          ))}
        </List>
      ) : (
        <View className="gap-[8px]">
          <Text variant="muted">{emptyMessage}</Text>
          {emptyAction}
        </View>
      )}
    </View>
  )
}

interface SavedItemLabels {
  add: string
  remove: string
  unavailable: string
  addAccessibilityLabel: string
  removeAccessibilityLabel: string
}

export interface SavedItemProps extends Omit<ViewProps, 'children'> {
  className?: string
  title: string
  detail?: string
  price?: string
  media?: ReactElement
  available?: boolean
  availabilityMessage?: string
  adding?: boolean
  removing?: boolean
  labels?: Partial<SavedItemLabels>
  onAdd?: () => void
  onRemove?: () => void
}

export function SavedItem({
  className,
  title,
  detail,
  price,
  media,
  available = true,
  availabilityMessage,
  adding = false,
  removing = false,
  labels,
  onAdd,
  onRemove,
  ...props
}: SavedItemProps) {
  const availability = availabilityMessage ?? (!available ? (labels?.unavailable ?? 'Sold out') : undefined)
  const addLabel = labels?.add ?? 'Add to cart'

  return (
    <ListItem {...props} className={cn('flex-col items-stretch gap-[12px] p-[16px]', className)}>
      <View className="flex-row items-start gap-[12px]">
        {media ? (
          <View aria-hidden className="shrink-0">
            {media}
          </View>
        ) : null}
        <ListItemContent>
          <ListItemTitle>{title}</ListItemTitle>
          {detail ? <ListItemDescription>{detail}</ListItemDescription> : null}
          {price ? (
            <Text size="sm" className="font-semibold" style={{ fontVariant: ['tabular-nums'] }}>
              {price}
            </Text>
          ) : null}
          {availability ? <ListItemDescription>{availability}</ListItemDescription> : null}
        </ListItemContent>
      </View>
      {onAdd || onRemove ? (
        <View className="flex-row flex-wrap items-center gap-[8px]">
          {onAdd ? (
            <View className="max-w-full shrink">
              <Button
                variant="secondary"
                accessibilityLabel={labels?.addAccessibilityLabel ?? `${addLabel}, ${title}`}
                disabled={!available || removing}
                loading={adding}
                onPress={() => {
                  if (available && !adding && !removing) onAdd()
                }}
              >
                {addLabel}
              </Button>
            </View>
          ) : null}
          {onRemove ? (
            <View className="max-w-full shrink">
              <Button
                variant="ghost"
                accessibilityLabel={labels?.removeAccessibilityLabel ?? `Remove ${title} from saved items`}
                disabled={adding}
                loading={removing}
                onPress={() => {
                  if (!adding && !removing) onRemove()
                }}
              >
                {labels?.remove ?? 'Remove'}
              </Button>
            </View>
          ) : null}
        </View>
      ) : null}
    </ListItem>
  )
}
components/blocks/saved-items/index.ts
export { SavedItem, SavedItems, type SavedItemProps, type SavedItemsProps } from './SavedItems'

Dependencies

Registry items

Browse shopping and reservations