UIPackage React Native
Search

Input

Mobile text field (View + TextInput) with focus, invalid, and disabled states, prefix/suffix icon slots, and a shake-on-invalid animation. Works with RN + Expo keyboard.

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 input

Source files

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

components/ui/input/Input.tsx
import * as React from 'react'
import { Platform, Pressable, TextInput, View, type TextInputProps } from 'react-native'
import Animated, {
  useAnimatedStyle,
  useReducedMotion,
  useSharedValue,
  withSequence,
  withTiming,
} from 'react-native-reanimated'
import { cn } from '@/lib/utils'
import { useThemeColors } from '@/lib/theme'
import { motionDurations } from '@/lib/motion'

export interface InputProps extends TextInputProps {
  className?: string
  containerClassName?: string
  invalid?: boolean
  /** Non-interactive leading content, usually an icon. */
  prefixIcon?: React.ReactNode
  /** Non-interactive trailing content, usually an icon or unit label. */
  suffixIcon?: React.ReactNode
  /** Shake the field when it becomes invalid. Fires on the false -> true transition only. */
  shakeOnInvalid?: boolean
}

const Input = React.forwardRef<React.ComponentRef<typeof TextInput>, InputProps>(
  (
    {
      className,
      containerClassName,
      invalid,
      editable = true,
      shakeOnInvalid = true,
      prefixIcon,
      suffixIcon,
      placeholderTextColor,
      accessibilityState,
      onFocus,
      onBlur,
      ...props
    },
    ref,
  ) => {
    const colors = useThemeColors()
    const reduced = useReducedMotion()
    const inputRef = React.useRef<React.ComponentRef<typeof TextInput>>(null)
    const [focused, setFocused] = React.useState(false)
    const shake = useSharedValue(0)
    const shakeStyle = useAnimatedStyle(() => ({ transform: [{ translateX: shake.value }] }))
    const wasInvalid = React.useRef(invalid)
    const setInputRef = React.useCallback(
      (input: React.ComponentRef<typeof TextInput> | null) => {
        inputRef.current = input
        if (typeof ref === 'function') return ref(input)
        if (ref) ref.current = input
      },
      [ref],
    )

    React.useEffect(() => {
      if (invalid && !wasInvalid.current && shakeOnInvalid && !reduced) {
        const step = motionDurations.fast / 3
        shake.value = withSequence(
          withTiming(-8, { duration: step }),
          withTiming(8, { duration: step }),
          withTiming(-6, { duration: step }),
          withTiming(6, { duration: step }),
          withTiming(0, { duration: step }),
        )
      }
      wasInvalid.current = invalid
    }, [invalid, shakeOnInvalid, reduced, shake])

    return (
      <Animated.View cssInterop={false} style={shakeStyle}>
        <Pressable
          accessible={false}
          focusable={false}
          disabled={!editable}
          onPress={() => inputRef.current?.focus()}
          className={cn(
            'flex-row items-center gap-[8px] rounded-md border border-muted-foreground bg-background px-3',
            focused && !invalid && 'border-ring bg-muted',
            invalid && 'border-destructive',
            !editable && 'opacity-50',
            containerClassName,
          )}
        >
          {prefixIcon ? (
            <View pointerEvents="none" className="shrink-0">
              {prefixIcon}
            </View>
          ) : null}
          <TextInput
            ref={setInputRef}
            editable={editable}
            placeholderTextColor={placeholderTextColor ?? colors.mutedForeground}
            accessibilityState={{ ...accessibilityState, disabled: !editable }}
            onFocus={(event) => {
              setFocused(true)
              onFocus?.(event)
            }}
            onBlur={(event) => {
              setFocused(false)
              onBlur?.(event)
            }}
            className={cn(
              'min-h-[48px] min-w-0 flex-1 py-2 text-foreground',
              Platform.OS === 'ios' ? 'text-[17px] leading-[22px]' : 'text-[16px] leading-[24px]',
              className,
            )}
            {...props}
          />
          {suffixIcon ? (
            <View pointerEvents="none" className="shrink-0">
              {suffixIcon}
            </View>
          ) : null}
        </Pressable>
      </Animated.View>
    )
  },
)
Input.displayName = 'Input'

export { Input }
components/ui/input/index.ts
export { Input, type InputProps } from './Input'

Dependencies

Registry items

npm packages

  • react-native-reanimated
Browse controls and interactions