phone_input.tsx 1.9 KB

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