UIPackage React Native
Search

Payment Method List

Choose a saved payment method with masked last four digits, unavailable reasons and caller-owned add intent.

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 payment-method-list

Source files

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

components/blocks/payment-method-list/PaymentMethodList.tsx
import { Fragment } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Separator } from '@/components/ui/separator'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

export interface StoredPaymentMethod {
  /** Non-sensitive identifier owned by the checkout or payment provider. */
  id: string
  /** A display name, for example "Debit card". */
  label: string
  /** A display suffix from the provider. Exactly four digit characters are accepted. */
  lastFour?: string
  unavailableReason?: string
}

interface PaymentMethodListLabels {
  add: string
  empty: string
  selectionUnavailable: string
  endingIn: (digits: string) => string
}

export interface PaymentMethodListProps extends Omit<ViewProps, 'children'> {
  className?: string
  title?: string
  /** Caller-owned methods with unique and nonempty IDs. */
  methods?: readonly StoredPaymentMethod[]
  value: string | null
  onValueChange: (value: string) => void
  onAdd?: () => void
  labels?: Partial<PaymentMethodListLabels>
}

export function PaymentMethodList({
  className,
  title = 'Payment method',
  methods = [],
  value,
  onValueChange,
  onAdd,
  labels,
  ...props
}: PaymentMethodListProps) {
  const selected = methods.find((method) => method.id === value && !method.unavailableReason)
  const selectionNotice =
    value !== null && !selected && methods.some((method) => !method.unavailableReason)
      ? (labels?.selectionUnavailable ?? 'Your selected payment method is unavailable. Choose another method.')
      : undefined
  const add = labels?.add ?? 'Add payment method'
  useAnnouncement(selectionNotice)

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <Text size="title" accessibilityRole="header">
        {title}
      </Text>
      {methods.length > 0 ? (
        <RadioGroup
          accessibilityLabel={title}
          value={selected?.id ?? ''}
          onValueChange={onValueChange}
          className="gap-0 overflow-hidden rounded-lg border border-border bg-card"
        >
          {methods.map(({ id, label, lastFour, unavailableReason }, index) => {
            const digits = typeof lastFour === 'string' && /^[0-9]{4}$/.test(lastFour) ? lastFour : undefined
            const ending = digits
              ? (labels?.endingIn?.(digits) ?? `ending in ${digits.split('').join(' ')}`)
              : undefined
            return (
              <Fragment key={id}>
                {index > 0 ? <Separator /> : null}
                <RadioGroupItem
                  value={id}
                  label={label}
                  disabled={Boolean(unavailableReason)}
                  accessibilityLabel={[label, ending, unavailableReason].filter(Boolean).join(', ')}
                  className={cn(
                    'rounded-none border border-transparent focus:border-ring',
                    unavailableReason && 'bg-muted opacity-100',
                  )}
                >
                  {digits ? <Text size="sm" style={{ fontVariant: ['tabular-nums'] }}>{`•••• ${digits}`}</Text> : null}
                  {unavailableReason ? (
                    <Text size="sm" variant="muted">
                      {unavailableReason}
                    </Text>
                  ) : null}
                </RadioGroupItem>
              </Fragment>
            )
          })}
        </RadioGroup>
      ) : (
        <Text variant="muted">{labels?.empty ?? 'No saved payment methods.'}</Text>
      )}
      {selectionNotice ? (
        <Text size="sm" variant="muted">
          {selectionNotice}
        </Text>
      ) : null}
      {onAdd ? (
        <Button variant="outline" accessibilityLabel={add} onPress={onAdd}>
          {add}
        </Button>
      ) : null}
    </View>
  )
}
components/blocks/payment-method-list/index.ts
export { PaymentMethodList, type StoredPaymentMethod, type PaymentMethodListProps } from './PaymentMethodList'

Dependencies

Browse shopping and reservations