UIPackage React Native
Search
Menu

Spending Limit

An editable spending limit with effective scope, draft validation, and a recoverable save action.

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 spending-limit

Source files

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

components/blocks/spending-limit/SpendingLimit.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

const defaultLabels = {
  current: 'Current limit',
  save: 'Save limit',
  saving: 'Saving limit…',
  saved: 'Limit saved.',
  retry: 'Retry save',
  discard: 'Discard changes',
}

export interface SpendingLimitProps extends Omit<ViewProps, 'children'> {
  className?: string
  /** The effective limit and its caller-localized representation. Amounts are nonnegative integer minor units. */
  current: { minorUnits: number; display: string }
  /** The caller's parsed edit. Null represents empty, incomplete, or invalid input. */
  draft: number | null
  /** Describes what the effective limit applies to. */
  scope: string
  /** Describes when or where a saved change takes effect. */
  effectiveSummary: string
  editor: (state: { disabled: boolean }) => React.ReactNode
  onSave: (minorUnits: number) => void
  onDiscard?: () => void
  saving?: boolean
  disabled?: boolean
  unavailableReason?: string
  /** Validation blocks saving while a failed request remains retryable. */
  validationError?: string
  saveError?: string
  labels?: Partial<typeof defaultLabels>
}

export function SpendingLimit({
  className,
  current,
  draft,
  scope,
  effectiveSummary,
  editor,
  onSave,
  onDiscard,
  saving = false,
  disabled = false,
  unavailableReason,
  validationError,
  saveError,
  labels,
  ...props
}: SpendingLimitProps) {
  const copy = { ...defaultLabels, ...labels }
  const changed = draft !== current.minorUnits
  const blocked = disabled || saving || !!unavailableReason
  const canSave = !blocked && changed && draft !== null && Number.isSafeInteger(draft) && draft >= 0 && !validationError
  const [submitted, setSubmitted] = React.useState<number>()
  const saved = !saveError && submitted !== undefined && submitted === current.minorUnits
  const action = saving ? copy.saving : saveError ? copy.retry : copy.save
  useAnnouncement(saving ? copy.saving : saveError || (saved && !changed ? copy.saved : undefined))

  return (
    <View {...props} className={cn('gap-4', className)}>
      <View className="gap-1 rounded-xl bg-muted px-4 py-4">
        <Text size="sm" variant="muted">
          {copy.current}
        </Text>
        <Text size="title" accessibilityRole="header" style={{ fontVariant: ['tabular-nums'] }}>
          {current.display}
        </Text>
        <Text size="sm">{scope}</Text>
      </View>
      {editor({ disabled: blocked })}
      <Text size="sm" variant="muted">
        {effectiveSummary}
      </Text>
      {unavailableReason ? <Text size="sm">{unavailableReason}</Text> : null}
      {!saving && (validationError || saveError) ? (
        <Alert variant="destructive">
          <AlertDescription>{validationError || saveError}</AlertDescription>
        </Alert>
      ) : null}
      {saved && !changed && !saving ? (
        <Text size="sm" className="font-medium">
          {copy.saved}
        </Text>
      ) : null}
      <Button
        accessibilityLabel={action}
        disabled={!saving && !canSave}
        loading={saving}
        onPress={() => {
          if (!canSave) return
          setSubmitted(draft)
          onSave(draft)
        }}
      >
        {action}
      </Button>
      {changed && onDiscard ? (
        <Button
          variant="ghost"
          accessibilityLabel={copy.discard}
          disabled={blocked}
          onPress={() => {
            if (blocked) return
            setSubmitted(undefined)
            onDiscard()
          }}
        >
          {copy.discard}
        </Button>
      ) : null}
    </View>
  )
}
components/blocks/spending-limit/index.ts
export { SpendingLimit, type SpendingLimitProps } from './SpendingLimit'

Dependencies

Browse finance