0

How can I create a page with a verification code like this (on the picture) in React Native? I found some libs but I can not customize it following the design. Please give the way to customize it enter image description here

  • Put what exactly you need to customise. can help you – codehesh Jun 23 '21 at 09:21
  • I just want to customize the keyboard for my application following the attached image on my post. – DaeHoon Jang Jun 24 '21 at 02:45
  • I have a design UI here, If you can please tell me the components and the libraries following my design UI [UI Design](https://drive.google.com/file/d/1ObpkuKUVbdM2v5nehu3uyUqVazqKcxsN/view?usp=sharing). Thank you so much. I am just a beginner and would like to understand all of the components matching with UI Design – DaeHoon Jang Jun 24 '21 at 09:26

1 Answers1

1

You can try here https://snack.expo.io/@vasylnahuliak/stackoverflow-68096640

import React, { useState, useEffect } from 'react';
import {
  SafeAreaView,
  View,
  TouchableOpacity,
  Text,
  StyleSheet,
  Dimensions,
} from 'react-native';

const WINDOW_WIDTH = Dimensions.get('window').width;

const KEYBOARD_WIDTH = WINDOW_WIDTH - 40;

const keyboardConfig = [
  {
    label: '1',
    value: 1,
  },
  {
    label: '2',
    value: 2,
  },
  {
    label: '3',
    value: 3,
  },
  {
    label: '4',
    value: 4,
  },
  {
    label: '5',
    value: 5,
  },
  {
    label: '6',
    value: 6,
  },
  {
    label: '7',
    value: 7,
  },
  {
    label: '8',
    value: 8,
  },
  {
    label: '9',
    value: 9,
  },
  {
    label: 'C',
    value: 'clear',
  },
  {
    label: '0',
    value: 0,
  },
  {
    label: '⌫',
    value: 'remove',
  },
];

const Key = ({ label, value, onPress }) => {
  const handlePress = () => {
    onPress({ label, value });
  };

  return (
    <TouchableOpacity onPress={handlePress} style={styles.keyView}>
      <Text style={styles.keyText}>{label}</Text>
    </TouchableOpacity>
  );
};

const CIRCLE_SIZE = 40;

const Circle = ({ isActive }) => {
  return <View style={[styles.circle, isActive && styles.circleActive]} />;
};

const CODE_LENGTH = 4;

export default function App() {
  const [code, setCode] = useState('');

  const handleKeyPress = (key) => {
    if (typeof key.value === 'number') {
      setCode(code + key.value);
    }

    if (key.value === 'remove') {
      setCode(code.slice(0, -1));
    }

    if (key.value === 'clear') {
      setCode('');
    }
  };

  useEffect(() => {
    if (code.length === CODE_LENGTH) {
      // NOTE: example right code
      if (code === '1234') {
        alert('Success')
      } else {
        alert('Wrong code. Try Again')
      }

      setCode('');
    }
  }, [code])

  return (
    <SafeAreaView>
      <View style={styles.circles}>
        {Array(4)
          .fill()
          .map((_, index) => (
            <Circle isActive={code.length > index} />
          ))}
      </View>

      <Text style={styles.title}>Enter the code</Text>
      <View style={styles.keyboard}>
        {keyboardConfig.map((key) => (
          <Key {...key} onPress={handleKeyPress} />
        ))}
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  title: {
    paddingBottom: 16,
    textAlign: 'center',
    fontSize: 32,
  },
  keyboard: {
    width: KEYBOARD_WIDTH,
    flexDirection: 'row',
    flexWrap: 'wrap',
    alignSelf: 'center',
  },
  keyView: {
    width: KEYBOARD_WIDTH / 3,
    height: 60,
    justifyContent: 'center',
    alignItems: 'center',
  },
  keyText: {
    fontSize: 32,
  },
  circles: {
    flexDirection: 'row',
    justifyContent: 'center',
    marginVertical: 32
  },
  circle: {
    width: CIRCLE_SIZE,
    height: CIRCLE_SIZE,
    marginHorizontal: 8,
    borderRadius: CIRCLE_SIZE,
    backgroundColor: 'grey',
  },
  circleActive: {
    backgroundColor: 'tomato',
  },
});
Vasyl Nahuliak
  • 1,912
  • 2
  • 14
  • 32
  • But How can I use "onInputCompleted" function on this – DaeHoon Jang Jun 24 '21 at 03:02
  • Please add your code in question. I don't know what you mean about `onInputCompleted`. In my code logic of complete code be automatic after tap 4 code symbol – Vasyl Nahuliak Jun 24 '21 at 04:30
  • Yes, it will be the function move to next page after tap 4 codes symbol. It look like the **onInputCompleted** on library **react-native-sms-verifycode** [react-native-sms-verifycode](https://www.npmjs.com/package/react-native-sms-verifycode/v/2.0.0). – DaeHoon Jang Jun 24 '21 at 08:53
  • If you can please tell me the components and the libraries following my design UI [UI Design](https://drive.google.com/file/d/1ObpkuKUVbdM2v5nehu3uyUqVazqKcxsN/view?usp=sharing). Thank you so much. I am just a beginner and would like to understand all of the components matching with UI Design – DaeHoon Jang Jun 24 '21 at 09:27
  • I'm sorry. But on StackOverflow you can create question about help how todo, but not for all answer for you job. In my case I get to you full answer about how to do components and fully customised example. And now you must do this by yourself. Thanks & goodbye – Vasyl Nahuliak Jun 24 '21 at 13:27