import React, { useLayoutEffect, useState } from 'react'; import { FlatList, StyleSheet, Text, TextInputProps } from 'react-native'; import * as List from '../containers/List'; import SafeAreaView from '../containers/SafeAreaView'; import SearchBox from '../containers/SearchBox'; import I18n from '../i18n'; import { useAppNavigation, useAppRoute } from '../lib/hooks/navigation'; import { useDebounce } from '../lib/methods/helpers'; import { TNavigation } from '../stacks/stackType'; import { useTheme } from '../theme'; import { IOptionsField } from './NotificationPreferencesView/options'; import sharedStyles from './Styles'; const styles = StyleSheet.create({ noResult: { fontSize: 16, paddingVertical: 56, ...sharedStyles.textSemibold, ...sharedStyles.textAlignCenter } }); interface IItem { item: IOptionsField; selected: boolean; onItemPress: () => void; } const Item = ({ item, selected, onItemPress }: IItem) => { const { colors } = useTheme(); return ( (selected ? : null)} onPress={onItemPress} translateTitle={false} /> ); }; const RenderSearch = ({ onChangeText }: TextInputProps) => ( <> ); const PickerView = (): React.ReactElement => { const navigation = useAppNavigation(); const { params: { title, data: paramData, value: paramValue, total: paramTotal, onSearch, onChangeValue, onEndReached } } = useAppRoute(); const { colors } = useTheme(); const [data, setData] = useState(paramData); const [total, setTotal] = useState(paramTotal ?? 0); const [searchText, setSearchText] = useState(''); useLayoutEffect(() => { navigation.setOptions({ title: title ?? I18n.t('Select_an_option') }); }, [navigation, title]); const handleChangeValue = (value: string | number) => { onChangeValue(value); navigation.goBack(); }; const onChangeText = useDebounce(async (text: string) => { const search = await onSearch(text); if (search?.data) { setSearchText(text); setData(search?.data); } }, 500); const handleOnEndReached = async () => { if (onEndReached && total && data.length < total) { const end = await onEndReached(searchText, data.length); if (end?.data) { setData([...data, ...end.data]); setTotal(end.total); } } }; return ( item.value as string} renderItem={({ item }) => ( handleChangeValue(item.value)} /> )} onEndReached={handleOnEndReached} onEndReachedThreshold={0.5} ItemSeparatorComponent={List.Separator} ListHeaderComponent={} ListFooterComponent={List.Separator} ListEmptyComponent={() => ( {I18n.t('No_results_found')} )} /> ); }; export default PickerView;