UIPackage React Native
Search
Menu

Account Picker

Select a caller-owned account using type, masked identity, balance, and availability, with optional confirmation.

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 account-picker

Source files

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

components/blocks/account-picker/AccountPicker.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { RadioGroup, RadioGroupItem, type RadioGroupItemProps } from '@/components/ui/radio-group'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface AccountPickerOptionProps extends Omit<RadioGroupItemProps, 'children'> {
  accountType: string
  /** A pre-masked account suffix or other non-sensitive label. */
  maskedSuffix?: string
  /** A localized balance string. */
  availableBalance?: string
  unavailableReason?: string
}

export interface AccountPickerProps extends ViewProps {
  className?: string
  /** AccountPickerOption children with unique, nonempty values. */
  children?: React.ReactNode
  value: string | null
  onValueChange: (value: string) => void
  onConfirm?: (value: string) => void
  busy?: boolean
  disabled?: boolean
  optionsLabel?: string
  confirmLabel?: string
  confirmingLabel?: string
  emptyMessage?: string
}

function accountOptions(children: React.ReactNode): React.ReactElement<AccountPickerOptionProps>[] {
  return React.Children.toArray(children).flatMap((child) => {
    if (!React.isValidElement<{ children?: React.ReactNode }>(child)) return []
    if (child.type === React.Fragment) return accountOptions(child.props.children)
    if (child.type !== AccountPickerOption) return []
    const option = child as React.ReactElement<AccountPickerOptionProps>
    return option.props.value.trim() ? [option] : []
  })
}

export function AccountPicker({
  className,
  children,
  value,
  onValueChange,
  onConfirm,
  busy = false,
  disabled = false,
  optionsLabel = 'Accounts',
  confirmLabel = 'Use this account',
  confirmingLabel = 'Selecting account…',
  emptyMessage = 'No accounts are available.',
  ...props
}: AccountPickerProps) {
  const options = accountOptions(children)
  const selected = options.find(
    (item) => item.props.value === value && !item.props.disabled && !item.props.unavailableReason,
  )
  const blocked = disabled || busy
  const actionLabel = busy ? confirmingLabel : confirmLabel

  return (
    <View {...props} className={cn('gap-4', className)}>
      {options.length ? (
        <RadioGroup
          accessibilityLabel={optionsLabel}
          value={selected?.props.value ?? ''}
          disabled={blocked}
          onValueChange={onValueChange}
        >
          {options.map((option) => React.cloneElement(option, { key: option.props.value }))}
        </RadioGroup>
      ) : (
        <View className="items-center rounded-xl border border-border px-4 py-8">
          <Text className="text-center font-medium">{emptyMessage}</Text>
        </View>
      )}
      {onConfirm ? (
        <Button
          accessibilityLabel={actionLabel}
          disabled={disabled || !selected}
          loading={busy}
          onPress={() => {
            if (selected && !blocked) onConfirm(selected.props.value)
          }}
        >
          {actionLabel}
        </Button>
      ) : null}
    </View>
  )
}

export function AccountPickerOption({
  className,
  accountType,
  maskedSuffix,
  availableBalance,
  unavailableReason,
  label,
  disabled,
  accessibilityLabel,
  ...props
}: AccountPickerOptionProps) {
  return (
    <RadioGroupItem
      {...props}
      label={label}
      disabled={disabled || !!unavailableReason}
      className={cn('rounded-xl border border-border bg-card', className)}
      accessibilityLabel={
        accessibilityLabel ??
        [label, accountType, maskedSuffix, availableBalance, unavailableReason].filter(Boolean).join(', ')
      }
    >
      <View className="flex-row flex-wrap gap-x-3 gap-y-1">
        <Text size="sm" variant="muted">
          {accountType}
        </Text>
        {maskedSuffix ? (
          <Text size="sm" variant="muted">
            {maskedSuffix}
          </Text>
        ) : null}
      </View>
      {availableBalance ? (
        <Text size="sm" className="font-medium" style={{ fontVariant: ['tabular-nums'] }}>
          {availableBalance}
        </Text>
      ) : null}
      {unavailableReason ? (
        <Text size="sm" variant="muted">
          {unavailableReason}
        </Text>
      ) : null}
    </RadioGroupItem>
  )
}
components/blocks/account-picker/index.ts
export {
  AccountPicker,
  AccountPickerOption,
  type AccountPickerProps,
  type AccountPickerOptionProps,
} from './AccountPicker'

Dependencies

Registry items

Browse finance