UIPackage React Native
Search
Menu

Sign In Form

Email and password sign-in section composed from Input, Checkbox, and Button primitives: inline field validation on submit, focus chaining, secure entry, and a busy submit state. Credentials leave through onSubmit; wire your own auth call.

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 sign-in-form

Source files

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

components/blocks/sign-in-form/SignInForm.tsx
import * as React from 'react'
import { Pressable, View, type TextInput, type ViewProps } from 'react-native'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { Input } from '@/components/ui/input'
import { Text } from '@/components/ui/text'

export interface SignInValues {
  email: string
  password: string
  remember: boolean
}

export interface SignInFormProps extends ViewProps {
  className?: string
  title?: string
  description?: string
  submitLabel?: string
  /** Server or auth error shown above the submit button. Field errors are handled internally. */
  error?: string | null
  loading?: boolean
  onSubmit: (values: SignInValues) => void
  /** Receives the current (trimmed) email value so the consumer can start a reset for it. */
  onForgotPassword?: (email: string) => void
}

const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/

/** Email + password sign-in section. The consumer owns the auth call:
 *  validate-and-submit happens here, credentials leave through onSubmit. */
function SignInForm({
  className,
  title = 'Welcome back',
  description = 'Sign in to your account to continue.',
  submitLabel = 'Sign in',
  error,
  loading = false,
  onSubmit,
  onForgotPassword,
  ...props
}: SignInFormProps) {
  const [email, setEmail] = React.useState('')
  const [password, setPassword] = React.useState('')
  const [remember, setRemember] = React.useState(true)
  const [emailError, setEmailError] = React.useState<string | null>(null)
  const [passwordError, setPasswordError] = React.useState<string | null>(null)
  const emailRef = React.useRef<React.ComponentRef<typeof TextInput>>(null)
  const passwordRef = React.useRef<React.ComponentRef<typeof TextInput>>(null)

  function handleSubmit() {
    if (loading) return
    const nextEmailError = EMAIL_PATTERN.test(email.trim()) ? null : 'Enter a valid email address.'
    const nextPasswordError = password.length > 0 ? null : 'Enter your password.'
    setEmailError(nextEmailError)
    setPasswordError(nextPasswordError)
    if (nextEmailError || nextPasswordError) {
      // Move focus to the first invalid field so keyboard and
      // screen-reader users land on the problem, not on the button.
      if (nextEmailError) emailRef.current?.focus()
      else passwordRef.current?.focus()
      return
    }
    onSubmit({ email: email.trim(), password, remember })
  }

  return (
    <View className={cn('gap-5', className)} {...props}>
      <View className="gap-1">
        <Text size="title" accessibilityRole="header">
          {title}
        </Text>
        <Text variant="muted" size="sm">
          {description}
        </Text>
      </View>

      <View className="gap-2">
        <Text size="sm" className="font-medium">
          Email
        </Text>
        <Input
          ref={emailRef}
          accessibilityLabel="Email"
          autoCapitalize="none"
          autoComplete="email"
          autoCorrect={false}
          keyboardType="email-address"
          textContentType="emailAddress"
          returnKeyType="next"
          placeholder="you@example.com"
          value={email}
          invalid={!!emailError}
          onChangeText={(next) => {
            setEmail(next)
            if (emailError) setEmailError(null)
          }}
          onSubmitEditing={() => passwordRef.current?.focus()}
        />
        {emailError ? (
          <Text size="sm" className="text-destructive" accessibilityRole="alert">
            {emailError}
          </Text>
        ) : null}
      </View>

      <View className="gap-2">
        <Text size="sm" className="font-medium">
          Password
        </Text>
        <Input
          ref={passwordRef}
          accessibilityLabel="Password"
          autoCapitalize="none"
          autoComplete="current-password"
          autoCorrect={false}
          textContentType="password"
          returnKeyType="done"
          secureTextEntry
          placeholder="Your password"
          value={password}
          invalid={!!passwordError}
          onChangeText={(next) => {
            setPassword(next)
            if (passwordError) setPasswordError(null)
          }}
          onSubmitEditing={handleSubmit}
        />
        {passwordError ? (
          <Text size="sm" className="text-destructive" accessibilityRole="alert">
            {passwordError}
          </Text>
        ) : null}
      </View>

      <View className="flex-row flex-wrap items-center justify-between gap-[8px]">
        <Pressable
          accessibilityRole="checkbox"
          accessibilityState={{ checked: remember }}
          accessibilityLabel="Keep me signed in"
          onPress={() => setRemember((value) => !value)}
          className="min-h-[48px] max-w-full flex-row items-center gap-[8px] active:opacity-80"
        >
          <Checkbox aria-hidden focusable={false} pointerEvents="none" checked={remember} />
          <Text size="sm" className="shrink">
            Keep me signed in
          </Text>
        </Pressable>
        {onForgotPassword ? (
          <Button
            variant="link"
            size="sm"
            accessibilityLabel="Forgot password"
            onPress={() => onForgotPassword(email.trim())}
          >
            Forgot password?
          </Button>
        ) : null}
      </View>

      {error ? (
        <Text size="sm" className="text-destructive" accessibilityRole="alert">
          {error}
        </Text>
      ) : null}

      <Button accessibilityLabel={submitLabel} loading={loading} onPress={handleSubmit}>
        {submitLabel}
      </Button>
    </View>
  )
}

export { SignInForm }
components/blocks/sign-in-form/index.ts
export { SignInForm, type SignInFormProps, type SignInValues } from './SignInForm'

Dependencies

Registry items

Browse accounts and messaging