UIPackage React Native
Search
Menu

Order History

Date-grouped order statuses, optional status filters and one open-details action per order.

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 order-history

Source files

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

components/blocks/order-history/OrderHistory.tsx
import { Fragment, useState } from 'react'
import { View, type ViewProps } from 'react-native'
import { FilterChip, FilterChipGroup, FilterChipReset } from '@/components/ui/filter-chip-group'
import {
  List,
  ListItem,
  ListItemContent,
  ListItemDescription,
  ListItemTitle,
  ListSeparator,
} from '@/components/ui/list'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export type OrderHistoryStatus = 'in-progress' | 'delivered' | 'cancelled'

export interface OrderHistoryOrder {
  id: string
  title: string
  reference?: string
  status: OrderHistoryStatus
  /** Locale-formatted amount as shown in the order system. */
  total?: string
}

export interface OrderHistoryGroup {
  id: string
  /** Caller-localized date or month heading. */
  label: string
  orders: readonly OrderHistoryOrder[]
}

type OrderHistoryLabels = Partial<Record<OrderHistoryStatus | 'details' | 'clear' | 'empty' | 'noMatches', string>>

export interface OrderHistoryProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  /** A bounded set of date groups in the order that they should appear. */
  groups?: readonly OrderHistoryGroup[]
  showFilters?: boolean
  labels?: OrderHistoryLabels
  onOpen: (orderId: string) => void
}

const statuses = {
  'in-progress': 'In progress',
  delivered: 'Delivered',
  cancelled: 'Canceled',
} satisfies Record<OrderHistoryStatus, string>
const statusKeys = Object.keys(statuses) as OrderHistoryStatus[]

export function OrderHistory({
  className,
  title = 'Order history',
  groups = [],
  showFilters = false,
  labels,
  onOpen,
  ...props
}: OrderHistoryProps) {
  const [filters, setFilters] = useState<string[]>([])
  const active = showFilters && filters.length > 0
  const visible = groups
    .map((group) => ({
      ...group,
      orders: active ? group.orders.filter(({ status }) => filters.includes(status)) : group.orders,
    }))
    .filter(({ orders }) => orders.length > 0)
  const details = labels?.details ?? 'Details'

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <Text size="title" accessibilityRole="header">
        {title}
      </Text>
      {showFilters && groups.some(({ orders }) => orders.length > 0) ? (
        <FilterChipGroup value={filters} onValueChange={setFilters}>
          {statusKeys.map((status) => (
            <FilterChip key={status} value={status} label={labels?.[status] ?? statuses[status]} />
          ))}
          {filters.length > 0 ? (
            <FilterChipReset accessibilityLabel={labels?.clear ?? 'Clear filters'}>
              {labels?.clear ?? 'Clear filters'}
            </FilterChipReset>
          ) : null}
        </FilterChipGroup>
      ) : null}
      {visible.length > 0 ? (
        visible.map(({ id, label, orders }) => (
          <View key={id} className="gap-[8px]">
            <Text size="sm" accessibilityRole="header" className="font-semibold">
              {label}
            </Text>
            <List>
              {orders.map((order, index) => {
                const status = labels?.[order.status] ?? statuses[order.status]
                return (
                  <Fragment key={order.id}>
                    {index > 0 ? <ListSeparator /> : null}
                    <ListItem
                      className="flex-wrap focus:bg-muted"
                      accessibilityLabel={[order.title, order.reference, status, order.total, details]
                        .filter(Boolean)
                        .join(', ')}
                      onPress={() => onOpen(order.id)}
                    >
                      <ListItemContent>
                        <ListItemTitle>{order.title}</ListItemTitle>
                        <ListItemDescription>
                          {[order.reference, status].filter(Boolean).join(' · ')}
                        </ListItemDescription>
                        {order.total ? <Text size="sm">{order.total}</Text> : null}
                      </ListItemContent>
                      <Text size="sm" className="max-w-[35%] shrink font-semibold">
                        {details}
                      </Text>
                    </ListItem>
                  </Fragment>
                )
              })}
            </List>
          </View>
        ))
      ) : (
        <Text variant="muted">
          {active ? (labels?.noMatches ?? 'No orders match these filters.') : (labels?.empty ?? 'No orders yet.')}
        </Text>
      )}
    </View>
  )
}
components/blocks/order-history/index.ts
export {
  OrderHistory,
  type OrderHistoryGroup,
  type OrderHistoryOrder,
  type OrderHistoryProps,
  type OrderHistoryStatus,
} from './OrderHistory'

Dependencies

Browse shopping and reservations