//import : react components import React, { useRef, useState } from "react"; import { View, Text, Modal, StyleSheet, TouchableOpacity, Dimensions, FlatList, } from "react-native"; const { width, height } = Dimensions.get("screen"); interface PickerProps { visible: boolean; setVisibility: any; value?: any; setValue: any; minValue?: number; maxValue?: number; unit?: string; Title?: string; cancelButtonText?: string; cancelButtonStyle?: any; cancelButtonTextStyle?: any; confirmButtonText?: string; confirmButtonStyle?: any; confirmButtonTextStyle?: any; position?: "bottom" | "center"; bgBlur?: | "00" | "11" | "22" | "33" | "44" | "55" | "66" | "77" | "88" | "99" | "AA" | "BB" | "CC" | "DD" | "EE" | "FF"; } const ValuePicker: React.FC = ({ visible, setVisibility, value, setValue, minValue = 100, maxValue = 200, unit = "", Title = "Select Height", cancelButtonText = "Cancel", cancelButtonStyle, cancelButtonTextStyle, confirmButtonText = "Confirm", confirmButtonStyle, confirmButtonTextStyle, position = "center", bgBlur = "66", }) => { //variables : ref const flatlistRef = useRef(); const itemHeight = 50; //hook : states const [data, setData] = useState([]); const [selectedIndex, setSelectedIndex] = useState(1); //function : modal Function const closeModal = () => { setVisibility(false); }; //function : imp function const fetchData = () => { const tempData = [""]; for (let i = minValue; i <= maxValue; i++) { if (unit.trim().length > 0) { tempData.push(`${i} ${unit}`); } else { tempData.push(`${i}`); } } tempData.push(""); setData(tempData); }; const handleScroll = (event: any) => { const offsetY = event.nativeEvent.contentOffset.y; const visibleIndex = Math.floor(offsetY / itemHeight) + 1; // Calculate the index of the middle item if (visibleIndex >= 0 && visibleIndex < data.length) { const middleItem = data[visibleIndex]; setSelectedIndex(visibleIndex); } }; const confirmPress = () => { const value = data[selectedIndex]; setValue(value); closeModal(); }; //variables : styles const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#000000" + bgBlur, }, cenContainer: { flex: 1, backgroundColor: "#000000" + bgBlur, justifyContent: "center", alignItems: "center", }, blurView: { flex: 1, }, cenBlurView: { position: "absolute", top: 0, right: 0, left: 0, bottom: 0, }, mainView: { backgroundColor: "#FFFFFF", padding: 20, borderTopRightRadius: 30, borderTopLeftRadius: 30, }, cenMainView: { height: height / 3, backgroundColor: "#FFFFFF", padding: 20, borderRadius: 5, }, }); //function : render function const renderItem = ({ item, index }) => { const isMiddleItem = index === selectedIndex; return ( {item} ); }; //UI return ( {Title} index.toString()} style={{ height: 150, marginVertical: 20, }} getItemLayout={(data, index) => ({ length: 50, offset: 50 * index, index, })} onScroll={handleScroll} initialNumToRender={3} maxToRenderPerBatch={3} /> {cancelButtonText} {confirmButtonText} ); }; export default ValuePicker;