import { Keyboard, Modal, ScrollView, StyleSheet, Text, TouchableOpacity, TouchableWithoutFeedback, View, } from 'react-native'; import React, { useCallback, useState, useEffect, ComponentType } from 'react'; import ReactNativeSafeAreaView from 'react-native-safe-area-view'; import { Picker } from '@react-native-picker/picker'; import { isAndroid } from '../helpers/platform'; import { useTheme } from './ThemeContext'; import noop from '../helpers/noop'; type Option = { label: string; value: string; }; const isEmptyString = (str: string) => str.length === 0; const getFirstOptionValue = (options: Option[]) => { const option = options[0]; if (option !== undefined) { return option.value; } return ''; }; type SelectProps = { readonly options?: Option[]; readonly show?: boolean; readonly value?: string; readonly onChange?: (v: string) => void; readonly onDismiss?: () => void; }; const SelectIOS: ComponentType> = ({ options, show, value, onChange, onDismiss }) => { const theme = useTheme(); const [pickedValue, setPickedValue] = useState(value); const handleChange = useCallback(() => { onChange(isEmptyString(pickedValue) ? getFirstOptionValue(options) : pickedValue); onDismiss(); }, [pickedValue, options]); useEffect(() => { if (show) { Keyboard.dismiss(); } const keyboardWillShowListener = Keyboard.addListener('keyboardWillShow', onDismiss); setPickedValue(value); return () => keyboardWillShowListener.remove(); }, [show, value]); if (!show) { return null; } return ( Cancel Done setPickedValue(value)}> {options.map(option => ( ))} ); }; const SelectAndroid: ComponentType> = ({ options, show, value, onChange, onDismiss }) => { const theme = useTheme(); const handleChange = value => { onChange(value); onDismiss(); }; return ( {options.map(option => ( handleChange(option.value)} style={theme.select.optionWrapper} > {option.label} ))} ); }; const Select: ComponentType = ({ options = [], show = false, value = '', onChange = noop, onDismiss = noop, }) => { return isAndroid ? ( ) : ( ); }; export default Select;