import React from 'react'; import { StackNavigationProp } from '@react-navigation/stack'; import { RouteProp } from '@react-navigation/native'; import { FlatList, StyleSheet, Text, View } from 'react-native'; import I18n from '../i18n'; import { TSupportedThemes, withTheme } from '../theme'; import { themes } from '../lib/constants'; import debounce from '../utils/debounce'; import * as List from '../containers/List'; import SearchBox from '../containers/SearchBox'; import SafeAreaView from '../containers/SafeAreaView'; import sharedStyles from './Styles'; import { ChatsStackParamList } from '../stacks/types'; import { IOptionsField } from './NotificationPreferencesView/options'; const styles = StyleSheet.create({ search: { width: '100%', height: 56 }, noResult: { fontSize: 16, paddingVertical: 56, ...sharedStyles.textSemibold, ...sharedStyles.textAlignCenter }, listNoHeader: { marginTop: 32 } }); interface IItem { item: IOptionsField; selected: boolean; onItemPress: () => void; theme: TSupportedThemes; } interface IRenderSearch { hasSearch: boolean; onChangeText: (text: string) => void; } interface IPickerViewState { data: IOptionsField[]; value: string; total: number; searchText: string; } interface IPickerViewProps { navigation: StackNavigationProp; route: RouteProp; theme: TSupportedThemes; } const Item = React.memo(({ item, selected, onItemPress, theme }: IItem) => ( (selected ? : null)} onPress={onItemPress} translateTitle={false} /> )); const RenderSearch = ({ hasSearch, onChangeText }: IRenderSearch) => { if (!hasSearch) { return ; } return ( <> ); }; class PickerView extends React.PureComponent { private onSearch?: (text?: string) => Promise; static navigationOptions = ({ route }: IPickerViewProps) => ({ title: route.params?.title ?? I18n.t('Select_an_option') }); constructor(props: IPickerViewProps) { super(props); const data = props.route.params?.data ?? []; const value = props.route.params?.value ?? ''; const total = props.route.params?.total ?? 0; this.state = { data, value, total, searchText: '' }; this.onSearch = props.route.params?.onSearch; } onChangeValue = (value: string) => { const { navigation, route } = this.props; const goBack = route.params?.goBack ?? true; const onChange = route.params?.onChangeValue ?? (() => {}); onChange(value); if (goBack) { navigation.goBack(); } }; onChangeText = debounce(async (searchText: string) => { if (this.onSearch) { const data = await this.onSearch(searchText); if (data?.data) { this.setState({ ...data, searchText }); } } }, 500); onEndReached = async () => { const { route } = this.props; const { data, total, searchText } = this.state; const onEndReached = route.params?.onEndReached; if (onEndReached && data.length < total) { const val = await onEndReached(searchText, data.length); if (val?.data) { this.setState({ ...val, data: [...data, ...val.data] }); } } }; render() { const { data, value } = this.state; const { theme } = this.props; return ( item.value as string} renderItem={({ item }) => ( this.onChangeValue(item.value as string)} /> )} onEndReached={() => this.onEndReached()} onEndReachedThreshold={0.5} ItemSeparatorComponent={List.Separator} ListHeaderComponent={} ListFooterComponent={List.Separator} ListEmptyComponent={() => ( {I18n.t('No_results_found')} )} /> ); } } export default withTheme(PickerView);