UIPackage React Native
Search

Inventory Count

Record a physical stock count, compare with recorded stock and submit it for caller-managed review.

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 inventory-count

Source files

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

components/blocks/inventory-count/InventoryCount.tsx
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { FormField } from '@/components/ui/form-field'
import { Input } from '@/components/ui/input'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

interface InventoryCountLabels {
  recorded: string
  counted: string
  invalid: string
  submit: string
  difference: (units: number) => string
}

export interface InventoryCountProps extends Omit<ViewProps, 'children'> {
  className?: string
  item: string
  sku?: string
  location?: string
  /** Recorded non-negative whole units. Omit when the count is blind. */
  expected?: number
  /** The current input draft. Empty means the item has not been counted. */
  value: string
  onValueChange: (value: string) => void
  /** Sends a valid physical count to the caller. */
  onSubmit: (counted: number) => void
  busy?: boolean
  error?: string
  labels?: Partial<InventoryCountLabels>
}

const amount = new Intl.NumberFormat()

export function InventoryCount({
  className,
  item,
  sku,
  location,
  expected,
  value,
  onValueChange,
  onSubmit,
  busy = false,
  error,
  labels,
  ...props
}: InventoryCountProps) {
  const draft = value.trim()
  const parsed = /^[0-9]+$/.test(draft) ? Number(draft) : undefined
  const counted = parsed !== undefined && Number.isSafeInteger(parsed) ? parsed : undefined
  const invalidCount = draft.length > 0 && counted === undefined
  const recorded = expected !== undefined && Number.isSafeInteger(expected) && expected >= 0 ? expected : undefined
  const difference = counted !== undefined && recorded !== undefined ? counted - recorded : undefined
  const variance =
    difference === undefined
      ? undefined
      : (labels?.difference?.(difference) ??
        (difference === 0
          ? 'Matches recorded stock'
          : `${amount.format(Math.abs(difference))} ${difference < 0 ? 'fewer' : 'more'} than recorded`))
  const metadata = [sku, location].filter(Boolean).join(' · ')
  const submit = labels?.submit ?? 'Submit for review'
  const visibleError = busy ? undefined : error
  useAnnouncement(visibleError)

  return (
    <View {...props} className={cn('gap-[16px]', className)}>
      <View className="gap-[4px]">
        <Text size="title" accessibilityRole="header">
          {item}
        </Text>
        {metadata ? <Text variant="muted">{metadata}</Text> : null}
      </View>
      <View className="gap-[16px] rounded-lg border border-border bg-card p-[16px]">
        {recorded !== undefined ? (
          <View className="gap-[4px]">
            <Text size="sm" variant="muted">
              {labels?.recorded ?? 'Recorded stock'}
            </Text>
            <Text size="xl" style={{ fontVariant: ['tabular-nums'] }}>
              {amount.format(recorded)}
            </Text>
          </View>
        ) : null}
        <FormField
          label={labels?.counted ?? 'Counted units'}
          error={invalidCount ? (labels?.invalid ?? 'Enter a whole number of units, 0 or greater.') : undefined}
        >
          {(control) => (
            <Input
              {...control}
              inputMode="numeric"
              autoCorrect={false}
              editable={!busy}
              value={value}
              onChangeText={(next) => {
                if (!busy) onValueChange(next)
              }}
              shakeOnInvalid={false}
            />
          )}
        </FormField>
        {variance ? (
          <Text size="sm" className="font-medium">
            {variance}
          </Text>
        ) : null}
      </View>
      {visibleError ? (
        <Text className="text-destructive" accessibilityRole="alert">
          {visibleError}
        </Text>
      ) : null}
      <Button
        accessibilityLabel={submit}
        loading={busy}
        disabled={counted === undefined}
        onPress={() => {
          if (!busy && counted !== undefined) onSubmit(counted)
        }}
      >
        {submit}
      </Button>
    </View>
  )
}
components/blocks/inventory-count/index.ts
export { InventoryCount, type InventoryCountProps } from './InventoryCount'

Dependencies

Browse shopping and reservations