import React from 'react'; import { FlatList, StyleSheet, Text } from 'react-native'; import { IBaseScreen } from '../definitions'; import I18n from '../i18n'; import { TSupportedThemes, withTheme } from '../theme'; import { themes } from '../lib/constants'; import { debounce } from '../lib/methods/helpers'; 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({ 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; } type IPickerViewProps = IBaseScreen; 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);