phone_input.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { useState } from "react";
  2. import { View, Text, TextInput, StyleSheet, ViewStyle, StyleProp } from "react-native";
  3. import { HandleForgetPasswordFormDataChange, HandleSignUpFormDataChange } from "../../types/signup";
  4. interface PhoneInputProps {
  5. placeholder: string;
  6. extendedStyle?: StyleProp<ViewStyle>;
  7. handleFormDataChange?: HandleSignUpFormDataChange;
  8. handleForgetPasswordFormDataChange?: HandleForgetPasswordFormDataChange;
  9. editable?: boolean;
  10. }
  11. const PhoneInput: React.FC<PhoneInputProps> = ({
  12. placeholder,
  13. extendedStyle,
  14. handleFormDataChange,
  15. handleForgetPasswordFormDataChange,
  16. editable,
  17. }) => {
  18. const [error, setError] = useState("");
  19. //by calling <PhoneInput handleFormDataChange={}... />, we use Registration Form Type.
  20. //by calling <PhoneInput handleForgetPasswordFormDataChange={}... />, we use Forget-Password Form Type.
  21. const handleTextChange = (text: string) => {
  22. if (text.length >= 8) {
  23. setError("");
  24. handleFormDataChange?.("phone", text);
  25. handleFormDataChange?.("phoneVerificationStatus", true);
  26. handleForgetPasswordFormDataChange?.("phone", text);
  27. handleForgetPasswordFormDataChange?.("phoneVerificationStatus", true);
  28. } else {
  29. setError("Please enter at least 8 digits");
  30. handleFormDataChange?.("phone", text);
  31. handleFormDataChange?.("phoneVerificationStatus", false);
  32. handleForgetPasswordFormDataChange?.("phone", text);
  33. handleForgetPasswordFormDataChange?.("phoneVerificationStatus", false);
  34. }
  35. };
  36. return (
  37. <View>
  38. <View style={[styles.inputContainer, extendedStyle]}>
  39. <Text style={styles.prefix}>+852</Text>
  40. <View style={styles.horizontalLine} />
  41. <TextInput
  42. keyboardType="numeric"
  43. onChangeText={handleTextChange}
  44. placeholder={placeholder}
  45. style={[styles.input]}
  46. placeholderTextColor="#888888"
  47. editable={editable}
  48. />
  49. </View>
  50. {error && <Text style={styles.errorMessage}>{error}</Text>}
  51. </View>
  52. );
  53. };
  54. const styles = StyleSheet.create({
  55. inputContainer: {
  56. maxWidth: "100%",
  57. flexDirection: "row",
  58. alignItems: "center",
  59. justifyContent: "center",
  60. borderWidth: 1,
  61. borderColor: "#bbbbbb",
  62. borderRadius: 12,
  63. padding: 20,
  64. },
  65. prefix: {
  66. marginRight: 5,
  67. fontSize: 16,
  68. },
  69. horizontalLine: {
  70. width: 24,
  71. borderColor: "#bbbbbb",
  72. borderWidth: 0.5,
  73. transform: [{ rotate: "90deg" }],
  74. },
  75. input: {
  76. flex: 1,
  77. marginLeft: 5,
  78. fontSize: 16,
  79. },
  80. errorMessage: {
  81. fontSize: 14,
  82. color: "#ff0033",
  83. fontWeight: "400",
  84. marginLeft: 10,
  85. marginTop: 10,
  86. },
  87. });
  88. export default PhoneInput;