phone_input.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { useState } from "react";
  2. import { View, Text, TextInput, StyleSheet, ViewStyle, StyleProp } from "react-native";
  3. interface PhoneInputProps {
  4. placeholder: string;
  5. extendedStyle?: StyleProp<ViewStyle>;
  6. onChangeText: (text: string) => void;
  7. onValidationStatusChange: (validationStatus: boolean) => void;
  8. }
  9. const PhoneInput: React.FC<PhoneInputProps> = ({
  10. placeholder,
  11. extendedStyle,
  12. onChangeText,
  13. onValidationStatusChange,
  14. }) => {
  15. const [error, setError] = useState("");
  16. const handleTextChange = (text: string) => {
  17. if (text.length >= 8) {
  18. onChangeText(text);
  19. setError("");
  20. onValidationStatusChange(true);
  21. } else {
  22. setError("Please enter at least 8 digits.");
  23. onValidationStatusChange(false);
  24. }
  25. };
  26. return (
  27. <View>
  28. <View style={[styles.inputContainer, extendedStyle]}>
  29. <Text style={styles.prefix}>+852</Text>
  30. <TextInput
  31. keyboardType="numeric"
  32. onChangeText={handleTextChange}
  33. placeholder={placeholder}
  34. style={[styles.input]}
  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: 10,
  50. padding: 15,
  51. margin: 10,
  52. },
  53. prefix: {
  54. marginRight: 5,
  55. },
  56. input: {
  57. flex: 1,
  58. marginLeft: 5,
  59. },
  60. errorMessage: {
  61. fontSize: 12,
  62. color: "#ff0033",
  63. marginLeft: 20,
  64. },
  65. });
  66. export default PhoneInput;