UNPKG

rn-phone-input-field

Version:

A React Native phone number input component built from scratch, featuring a text input for number entry, a custom dropdown for selecting country codes, and validation logic using regex or country-specific rules. It supports formatting, localization, and s

117 lines (116 loc) 5.29 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importStar(require("react")); var react_native_1 = require("react-native"); var Picker_1 = __importDefault(require("./Picker")); var picker_style_1 = require("./styles/picker.style"); var screenHeight = react_native_1.Dimensions.get('window').height; var statusBarHeight = react_native_1.Platform.OS === 'android' ? react_native_1.StatusBar.currentHeight || 0 : 0; // Animation configuration var ANIMATION_CONFIG = { friction: 8, tension: 50, useNativeDriver: true, }; var CountryPickerModal = (0, react_1.forwardRef)(function (_a, ref) { var darkMode = _a.darkMode, onSelect = _a.onSelect, searchInputProps = _a.searchInputProps; var _b = (0, react_1.useState)(false), isModalVisible = _b[0], setIsModalVisible = _b[1]; var _c = (0, react_1.useState)(false), isAnimating = _c[0], setIsAnimating = _c[1]; var animationValue = (0, react_1.useRef)(new react_native_1.Animated.Value(screenHeight)).current; var animateToValue = (0, react_1.useCallback)(function (toValue, onComplete) { react_native_1.Animated.spring(animationValue, __assign(__assign({}, ANIMATION_CONFIG), { toValue: toValue })).start(onComplete); }, [animationValue]); var openModal = (0, react_1.useCallback)(function () { setIsModalVisible(true); setIsAnimating(true); animateToValue(statusBarHeight, function () { setIsAnimating(false); }); }, [animateToValue]); var closeModal = (0, react_1.useCallback)(function () { if (isAnimating) return; setIsAnimating(true); animateToValue(screenHeight, function () { requestAnimationFrame(function () { setIsModalVisible(false); setIsAnimating(false); }); }); }, [isAnimating, animateToValue]); var handleCountrySelect = (0, react_1.useCallback)(function (country) { onSelect(country); closeModal(); }, [onSelect, closeModal]); (0, react_1.useImperativeHandle)(ref, function () { return ({ openModal: openModal, }); }, [openModal]); // Reset animation value when modal closes (0, react_1.useEffect)(function () { if (!isModalVisible) { animationValue.setValue(screenHeight); } }, [isModalVisible, animationValue]); // Cleanup animation on unmount (0, react_1.useEffect)(function () { return function () { animationValue.stopAnimation(); }; }, [animationValue]); // Memoize animated style var animatedStyle = (0, react_1.useMemo)(function () { return ({ flex: 1, transform: [{ translateY: animationValue }], }); }, [animationValue]); if (!isModalVisible) return null; return (<> <react_native_1.StatusBar barStyle={darkMode ? 'light-content' : 'dark-content'} backgroundColor={darkMode ? '#000000' : '#FFFFFF'}/> <react_native_1.Modal visible={isModalVisible} transparent animationType="none" statusBarTranslucent={react_native_1.Platform.OS === 'android'} onRequestClose={closeModal}> <react_native_1.Animated.View style={animatedStyle}> <react_native_1.SafeAreaView style={[picker_style_1.countryPickerStyles.flex, picker_style_1.countryPickerStyles.mb40]}> <Picker_1.default onSelect={handleCountrySelect} darkMode={darkMode} closeModal={closeModal} searchInputProps={searchInputProps}/> </react_native_1.SafeAreaView> </react_native_1.Animated.View> </react_native_1.Modal> </>); }); CountryPickerModal.displayName = 'CountryPickerModal'; exports.default = CountryPickerModal;