如何自定义React-Native验证码

How to customixe React-Native verification Code

如何在 React Native 中创建一个带有验证码的页面(如图)? 我找到了一些库,但我无法按照设计对其进行自定义。 请给出定制方法 enter image description here

你可以在这里试试https://snack.expo.io/@vasylnahuliak/Whosebug-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',
  },
});