UIPackage React Native
Search

Password Input

Password text entry with controlled visibility and a separately labelled reveal 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 password-input

Source files

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

components/ui/password-input/PasswordInput.tsx
import * as React from 'react'
import { TextInput, View, type ViewProps } from 'react-native'
import { Eye, EyeOff } from 'lucide-react-native'
import { Input, type InputProps } from '@/components/ui/input'
import { IconButton } from '@/components/ui/icon-button'
import { cn } from '@/lib/utils'

export interface PasswordInputProps extends Omit<InputProps, 'secureTextEntry' | 'multiline' | 'suffixIcon' | 'style'> {
  visible?: boolean
  defaultVisible?: boolean
  onVisibleChange?: (visible: boolean) => void
  disabled?: boolean
  showLabel?: string
  hideLabel?: string
  inputClassName?: string
  style?: ViewProps['style']
}

export const PasswordInput = React.forwardRef<React.ComponentRef<typeof TextInput>, PasswordInputProps>(
  (
    {
      value,
      defaultValue = '',
      onChangeText,
      visible,
      defaultVisible = false,
      onVisibleChange,
      disabled = false,
      editable = true,
      invalid,
      className,
      inputClassName,
      containerClassName,
      style,
      showLabel = 'Show password',
      hideLabel = 'Hide password',
      onFocus,
      onBlur,
      autoComplete = 'current-password',
      autoCapitalize = 'none',
      autoCorrect = false,
      ...props
    },
    ref,
  ) => {
    const [internalValue, setInternalValue] = React.useState(defaultValue)
    const [internalVisible, setInternalVisible] = React.useState(defaultVisible)
    const [focused, setFocused] = React.useState(false)
    const input = React.useRef<React.ComponentRef<typeof TextInput>>(null)
    const wasFocused = React.useRef(false)
    const resolvedVisible = visible ?? internalVisible
    const canEdit = editable && !disabled
    const setInputRef = React.useCallback(
      (node: React.ComponentRef<typeof TextInput> | null) => {
        input.current = node
        if (typeof ref === 'function') return ref(node)
        if (ref) ref.current = node
      },
      [ref],
    )

    return (
      <View
        style={style}
        className={cn(
          'flex-row items-center rounded-md border border-muted-foreground bg-background',
          focused && !invalid && 'border-ring bg-muted',
          invalid && 'border-destructive',
          className,
        )}
      >
        <View className="min-w-0 flex-1">
          <Input
            {...props}
            ref={setInputRef}
            value={value ?? internalValue}
            editable={canEdit}
            invalid={invalid}
            secureTextEntry={!resolvedVisible}
            multiline={false}
            autoComplete={autoComplete}
            autoCapitalize={autoCapitalize}
            autoCorrect={autoCorrect}
            className={inputClassName}
            containerClassName={cn('border-0', containerClassName)}
            onChangeText={(next) => {
              if (!canEdit || props.readOnly) return
              if (value === undefined) setInternalValue(next)
              onChangeText?.(next)
            }}
            onFocus={(event) => {
              setFocused(true)
              onFocus?.(event)
            }}
            onBlur={(event) => {
              setFocused(false)
              onBlur?.(event)
            }}
          />
        </View>
        <IconButton
          icon={resolvedVisible ? EyeOff : Eye}
          accessibilityLabel={resolvedVisible ? hideLabel : showLabel}
          accessibilityState={{ expanded: resolvedVisible }}
          disabled={!canEdit}
          onPressIn={() => {
            wasFocused.current = input.current?.isFocused() ?? focused
          }}
          onPress={() => {
            if (!canEdit) return
            const restoreFocus = wasFocused.current || input.current?.isFocused() || focused
            wasFocused.current = false
            const next = !resolvedVisible
            if (visible === undefined) setInternalVisible(next)
            onVisibleChange?.(next)
            if (restoreFocus) input.current?.focus()
          }}
        />
      </View>
    )
  },
)
PasswordInput.displayName = 'PasswordInput'
components/ui/password-input/index.ts
export { PasswordInput } from './PasswordInput'
export type { PasswordInputProps } from './PasswordInput'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse controls and interactions