how to design react native OTP enter screen?

前端 未结 6 567
刺人心
刺人心 2021-01-12 13:34

I am new in react native design .Let me know how to achieve the screen shown below

is it necessary to use 4 TextInput or possible with one?

6条回答
  •  失恋的感觉
    2021-01-12 14:25

    Here I have created a screen with Six text input for otp verfication with Resend OTP functionality with counter timer of 90 sec. Fully tested on both android and ios.

    I have used react-native-confirmation-code-field for underlined text input. Below is the complete code.

    import React, { useState, useEffect } from 'react';
    import { SafeAreaView, Text, View ,TouchableOpacity} from 'react-native';
    import { CodeField, Cursor, useBlurOnFulfill, useClearByFocusCell } from 
    'react-native-confirmation-code-field';
    import { Button } from '../../../components';
    import { styles } from './style';
    
    interface VerifyCodeProps {
    }
    const CELL_COUNT = 6;
    const RESEND_OTP_TIME_LIMIT = 90;
    
    export const VerifyCode: React.FC = () => {
    let resendOtpTimerInterval: any;
    
    const [resendButtonDisabledTime, setResendButtonDisabledTime] = useState(
        RESEND_OTP_TIME_LIMIT,
    );
    
    //to start resent otp option
    const startResendOtpTimer = () => {
        if (resendOtpTimerInterval) {
            clearInterval(resendOtpTimerInterval);
        }
        resendOtpTimerInterval = setInterval(() => {
            if (resendButtonDisabledTime <= 0) {
                clearInterval(resendOtpTimerInterval);
            } else {
                setResendButtonDisabledTime(resendButtonDisabledTime - 1);
            }
        }, 1000);
    };
    
    //on click of resend button
    const onResendOtpButtonPress = () => {
        //clear input field
        setValue('')
        setResendButtonDisabledTime(RESEND_OTP_TIME_LIMIT);
        startResendOtpTimer();
    
        // resend OTP Api call
        // todo
        console.log('todo: Resend OTP');
    };
    
    //declarations for input field
    const [value, setValue] = useState('');
    const ref = useBlurOnFulfill({ value, cellCount: CELL_COUNT });
    const [props, getCellOnLayoutHandler] = useClearByFocusCell({
        value,
        setValue,
    });
    
    //start timer on screen on launch
    useEffect(() => {
        startResendOtpTimer();
        return () => {
            if (resendOtpTimerInterval) {
                clearInterval(resendOtpTimerInterval);
            }
        };
    }, [resendButtonDisabledTime]);
    
    return (
        
            Verify the Authorisation Code
            Sent to 7687653902
             (
                    
                        
                            {symbol || (isFocused ?  : null)}
                        
                    
                )}
            />
            {/* View for resend otp  */}
            {resendButtonDisabledTime > 0 ? (
                Resend Authorisation Code in {resendButtonDisabledTime} sec
            ) : (
                    
                        
                             Resend Authorisation Code
                             in {resendButtonDisabledTime} sec
                        
                    
                )
            }
            
                

    Style file for this screen is in given below code:

    import { StyleSheet } from 'react-native';
    import { Color } from '../../../constants';
    
    export const styles = StyleSheet.create({
    root: {
        flex: 1,
        padding: 20,
        alignContent: 'center',
        justifyContent: 'center'
    },
    title: {
        textAlign: 'left',
        fontSize: 20,
        marginStart: 20,
        fontWeight:'bold'
    },
    subTitle: {
        textAlign: 'left',
        fontSize: 16,
        marginStart: 20,
        marginTop: 10
    },
    codeFieldRoot: {
        marginTop: 40,
        width: '90%',
        marginLeft: 20,
        marginRight: 20,
    },
    cellRoot: {
        width: 40,
        height: 40,
        justifyContent: 'center',
        alignItems: 'center',
        borderBottomColor: '#ccc',
        borderBottomWidth: 1,
     },
     cellText: {
        color: '#000',
        fontSize: 28,
        textAlign: 'center',
    },
    focusCell: {
        borderBottomColor: '#007AFF',
        borderBottomWidth: 2,
    },
    
    button: {
        marginTop: 20
    },
    resendCode: {
        color: Color.BLUE,
        marginStart: 20,
        marginTop: 40,
    },
    resendCodeText: {
        marginStart: 20,
        marginTop: 40,
    },
    resendCodeContainer: {
        flexDirection: 'row',
        alignItems: 'center'
    }
    })
    

    Hope it will be helpful for many. Happy Coding!!

提交回复
热议问题