import React, { useEffect, useRef, useState } from 'react'; import { FlatList, Keyboard, Pressable, StyleSheet, Text, View } from 'react-native'; import Modal from 'react-native-modal'; import Button from './Button'; import Dialog, { getDialogWidth } from './Dialog'; //const DialogMaxWidth = $vw(85) > 500 ? 500 : $vw(85); //const DialogIOSWidth = $vw(75) > 450 ? 450 : $vw(75); export default Dropdown = ({ list = [], title = '', value, onChange, nameKey, valueKey, extraText='', itemHeight=50, rippleStyle, style = styles.valueView, textStyle = styles.valueText, placeholderStyle=styles.placeText, placeholder='', showIcon = true, iconColor = '#888', iconStyle = styles.iconStyle, autoSelect = true, customerItemView }) => { const refFlat = useRef(); const [visible, showDialog] = useState(false); const [selected, changeValue] = useState(''); const [currentIndex, setCurrent] = useState(0); useEffect(() => { if (value !== selected) { if (nameKey && valueKey) { for (var i = 0; i < list.length; i++) { let item = list[i]; if (item[valueKey] == value) { changeValue(extraText+item[nameKey]); if (list.length > 20) { setCurrent(i > 5 ? i - 4 : 0); } break; } } } else { changeValue(extraText+value); } } }, [value, []]); useEffect(() => { if (autoSelect && list.length > 0) { if (!value) { const item = list[0]; /*if (nameKey) { changeValue(item[nameKey]); } else { changeValue(item); }*/ if (onChange) { onChange(valueKey ? item[valueKey] : item, 0) } } } }, [list]); const showList = () => { Keyboard.dismiss(); showDialog(true); /*if (currentIndex > 0) { console.log(refFlat.current); setTimeout(() => { if (refFlat.current) { refFlat.current.scrollToIndex({ animated: false, index: currentIndex, viewPosition: 0.5 }) } }, 100) }*/ } const renderItem = ({ item, index, separators }) => { if (customerItemView) { return customerItemView(item, index, () => { showDialog(false); if (onChange) { onChange(valueKey ? item[valueKey] : item, index) } }) } else { return (