【问题标题】:How to customixe React-Native verification Code如何自定义 React-Native 验证码
【发布时间】:2021-06-23 08:54:07
【问题描述】:

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

【问题讨论】:

  • 放上你需要定制的东西。可以帮助你
  • 我只想按照我帖子中的附加图片为我的应用程序自定义键盘。
  • 我这里有一个设计 UI,如果你能告诉我遵循我的设计 UI UI Design 的组件和库。非常感谢。我只是一个初学者,想了解所有与 UI 设计相匹配的组件

标签: react-native user-interface verify customizing


【解决方案1】:

你可以在这里试试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',
  },
});

【讨论】:

  • 但是如何在这个上使用“onInputCompleted”函数
  • 请添加您有问题的代码。我不知道你对onInputCompleted 是什么意思。在我的代码中,完整代码的逻辑在点击 4 代码符号后是自动的
  • 是的,这将是点击4个代码符号后跳转到下一页的功能。它看起来像库 react-native-sms-verifycode react-native-sms-verifycode. 上的 onInputCompleted
  • 如果你能告诉我遵循我的设计 UI UI Design 的组件和库。非常感谢。我只是一个初学者,想了解所有与 UI 设计相匹配的组件
  • 对不起。但是在 StackOverflow 上,您可以创建有关如何帮助的问题,但不能为您的工作提供所有答案。就我而言,我会为您提供有关如何制作组件和完全定制的示例的完整答案。现在你必须自己做这件事。谢谢和再见
猜你喜欢
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2022-11-24
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
相关资源
最近更新 更多