TextInput Component
A comprehensive and customizable text input component for React Native applications with multiple variants, built-in validation support, and extensive styling options.
Features
- 🎨 Multiple Variants - Default, Outlined, and Flat input styles
- 🏷️ Label Support - Built-in label with required field indicators
- ❌ Error Handling - Built-in error display and styling
- 🎯 Icon Integration - Left and right component support
- 🔧 Highly Customizable - Extensive styling and behavior options
- ♿ Accessibility Ready - Screen reader support and proper semantics
- 📱 Platform Optimized - Consistent behavior across iOS and Android
Installation
💻Bash
npm install rn-base-component
# or
yarn add rn-base-component
Basic Usage
⚛️TSX
import React from 'react'
import { TextInput } from 'rn-base-component'
export default function App() {
return console.log(text)} />
}
TextInput Variants
Default TextInput
⚛️TSX
Outlined TextInput
⚛️TSX
Flat TextInput
⚛️TSX
Advanced Usage
With Label and Validation
⚛️TSX
With Icons
⚛️TSX
}
rightComponent={
}
value={searchQuery}
onChangeText={setSearchQuery}
/>
Password Input with Toggle
⚛️TSX
import React, { useState } from 'react'
export default function PasswordInput() {
const [password, setPassword] = useState('')
const [showPassword, setShowPassword] = useState(false)
return (
setShowPassword(!showPassword)}>
}
/>
)
}
API Reference
TextInputProps
| Prop | Type | Default | Description |
|---|---|---|---|
containerStyle | StyleProp<ViewStyle> | undefined | Style for the outer container |
editable | boolean | true | Whether text is editable |
inputContainerStyle | StyleProp<ViewStyle> | undefined | Style for the input container |
inputStyle | StyleProp<TextStyle> | undefined | Style for the input component |
label | string | undefined | Label text above the input |
isRequire | boolean | undefined | Add asterisk beside the label |
labelStyle | StyleProp<TextStyle> | undefined | Style for the label |
labelProps | TextProps | undefined | Additional props for the label |
leftComponent | React.ReactNode | undefined | Component on the left side |
rightComponent | React.ReactNode | undefined | Component on the right side |
errorText | string | undefined | Error message to display |
errorProps | TextProps | undefined | Props for the error text |
onFocus | () => void | undefined | Focus callback |
onBlur | () => void | undefined | Blur callback |
Inherited Props
All React Native TextInputProps are supported, including value, onChangeText, placeholder, secureTextEntry, multiline, numberOfLines, keyboardType, autoCapitalize, autoCorrect, maxLength, returnKeyType, onSubmitEditing.
Form Integration
Login Form
⚛️TSX
import React, { useState } from 'react'
import { View } from 'react-native'
import { TextInput, Button } from 'rn-base-component'
export default function LoginForm() {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const [errors, setErrors] = useState({})
const validateForm = () => {
const newErrors = {}
if (!email) {
newErrors.email = 'Email is required'
} else if (!/\S+@\S+\.\S+/.test(email)) {
newErrors.email = 'Email is invalid'
}
if (!password) {
newErrors.password = 'Password is required'
} else if (password.length < 6) {
newErrors.password = 'Password must be at least 6 characters'
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
const handleSubmit = () => {
if (validateForm()) {
console.log('Login:', { email, password })
}
}
return (
)
const styles = StyleSheet.create({
formContainer: {
padding: 20,
},
inputSpacing: {
marginBottom: 16,
},
inputLargeSpacing: {
marginBottom: 24,
},
})
}
Best Practices
- Label Clarity - Use clear, descriptive labels
- Placeholder Text - Provide helpful placeholder examples
- Error Messages - Show specific, actionable error messages
- Keyboard Types - Use appropriate keyboard types for input
- Auto-correction - Disable for sensitive inputs (emails, passwords)
- Accessibility - Provide proper accessibility labels and hints
Performance
Use onChangeText debouncing for search inputs and avoid complex validation on every keystroke.