You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A specialized input component for entering verification codes, PINs, and other sequential character inputs with customizable styling and enhanced user experience.
Features
🔢 Flexible Length - Configurable number of input cells (1-20)
🎨 Customizable Styling - Individual cell styling with focus states
🔒 Secure Input - Built-in secure text entry mode
⌨️ Smart Keyboard - Automatic keyboard type selection
📱 Mobile Optimized - Touch-friendly interface with proper focus management
🎯 Theme Integration - Seamlessly integrates with the design system
♿ Accessibility Ready - Full accessibility support
Installation
npm install rn-base-component
# or
yarn add rn-base-component
constSMSVerification=()=>{const[code,setCode]=useState('')const[loading,setLoading]=useState(false)consthandleCodeSubmit=asyncenteredCode=>{setLoading(true)try{awaitverifyCode(enteredCode)navigation.navigate('Success')}catch(error){setCode('')showError('Invalid code')}finally{setLoading(false)}}return(<Viewstyle={styles.container}><Textstyle={styles.title}>Enter verification code</Text><Textstyle={styles.subtitle}>We sent a 6-digit code to your phone</Text><CodeInputlength={6}value={code}onChangeText={setCode}onSubmit={handleCodeSubmit}keyboardType="number-pad"autoFocus={true}disabled={loading}/><TouchableOpacityonPress={resendCode}><Textstyle={styles.resendText}>Resend code</Text></TouchableOpacity></View>)}
constTwoFactorAuth=()=>{const[code,setCode]=useState('')const[error,setError]=useState('')consthandleCodeChange=newCode=>{setCode(newCode)setError('')// Clear error when user types}consthandleVerification=asyncfinalCode=>{try{awaitverifyTwoFactorCode(finalCode)onSuccess()}catch(err){setError('Invalid code. Please try again.')setCode('')}}return(<Viewstyle={styles.authContainer}><Textstyle={styles.title}>Enter authentication code</Text><Textstyle={styles.instruction}>Check your authenticator app for the 6-digit code</Text><CodeInputlength={6}value={code}onChangeText={handleCodeChange}onSubmit={handleVerification}keyboardType="number-pad"cellStyle={[styles.authCell,error&&styles.errorCell]}autoFocus={true}/>{error&&<Textstyle={styles.errorText}>{error}</Text>}</View>)}
OTP with Resend Timer
constOTPInput=()=>{const[otp,setOtp]=useState('')const[timeLeft,setTimeLeft]=useState(60)const[canResend,setCanResend]=useState(false)useEffect(()=>{if(timeLeft>0){consttimer=setTimeout(()=>setTimeLeft(timeLeft-1),1000)return()=>clearTimeout(timer)}else{setCanResend(true)}},[timeLeft])constresendOTP=()=>{setTimeLeft(60)setCanResend(false)setOtp('')// API call to resend OTP}return(<Viewstyle={styles.otpContainer}><CodeInputlength={4}value={otp}onChangeText={setOtp}onSubmit={verifyOTP}cellStyle={styles.otpCell}focusCellStyle={styles.otpCellFocused}/><Viewstyle={styles.resendContainer}><Text>Didn't receive the code? </Text>{canResend ? (<TouchableOpacityonPress={resendOTP}><Textstyle={styles.resendButton}>Resend</Text></TouchableOpacity>) : (<Textstyle={styles.timer}>Resend in {timeLeft}s</Text>)}</View></View>)}
Error State Handling
constCodeInputWithError=()=>{const[code,setCode]=useState('')const[error,setError]=useState(false)const[errorMessage,setErrorMessage]=useState('')consthandleCodeChange=(newCode: string)=>{setCode(newCode)// Clear error when user typesif(error){setError(false)setErrorMessage('')}}consthandleCodeSubmit=async(finalCode: string)=>{try{awaitverifyCode(finalCode)// Success handlingnavigation.navigate('Success')}catch(err){setError(true)setErrorMessage('Invalid code. Please try again.')setCode('')}}return(<CodeInputlength={6}value={code}onChangeText={handleCodeChange}onSubmit={handleCodeSubmit}error={error}errorText={errorMessage}errorCellStyle={styles.errorCell}label="Verification Code"isRequire={true}helperText="Enter the 6-digit code sent to your phone"/>)}conststyles=StyleSheet.create({errorCell: {borderColor: '#FF3B30',borderWidth: 2,backgroundColor: '#FFF5F5',},})
<CodeInputlength={6}accessibilityLabel="Six digit verification code"accessibilityHint="Enter the six digit code sent to your phone"accessibilityValue={{text: `${code.length} of 6 digits entered`,}}/>