2020-04-06 21:40:18 +00:00
|
|
|
import React from 'react';
|
2022-06-06 13:53:02 +00:00
|
|
|
import { FlatList, StyleSheet, Text } from 'react-native';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2022-05-03 15:13:53 +00:00
|
|
|
import { IBaseScreen } from '../definitions';
|
2020-04-06 21:40:18 +00:00
|
|
|
import I18n from '../i18n';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { debounce } from '../lib/methods/helpers';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-05-08 17:36:10 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2020-10-30 13:59:44 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
|
|
|
import { IOptionsField } from './NotificationPreferencesView/options';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2020-05-08 17:36:10 +00:00
|
|
|
noResult: {
|
|
|
|
fontSize: 16,
|
|
|
|
paddingVertical: 56,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
...sharedStyles.textAlignCenter
|
2022-02-17 14:57:30 +00:00
|
|
|
},
|
|
|
|
listNoHeader: {
|
|
|
|
marginTop: 32
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
interface IItem {
|
2021-12-03 19:27:57 +00:00
|
|
|
item: IOptionsField;
|
2021-11-17 19:56:28 +00:00
|
|
|
selected: boolean;
|
|
|
|
onItemPress: () => void;
|
2023-05-18 21:09:33 +00:00
|
|
|
theme?: TSupportedThemes;
|
2021-11-17 19:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
interface IRenderSearch {
|
|
|
|
hasSearch: boolean;
|
|
|
|
onChangeText: (text: string) => void;
|
|
|
|
}
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
interface IPickerViewState {
|
2021-12-03 19:27:57 +00:00
|
|
|
data: IOptionsField[];
|
2021-11-17 19:56:28 +00:00
|
|
|
value: string;
|
2022-02-17 14:57:30 +00:00
|
|
|
total: number;
|
|
|
|
searchText: string;
|
2021-11-17 19:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 15:13:53 +00:00
|
|
|
type IPickerViewProps = IBaseScreen<ChatsStackParamList, 'PickerView'>;
|
2021-11-17 19:56:28 +00:00
|
|
|
|
|
|
|
const Item = React.memo(({ item, selected, onItemPress, theme }: IItem) => (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2020-07-24 19:23:34 +00:00
|
|
|
title={I18n.t(item.label, { defaultValue: item.label, second: item?.second })}
|
2023-05-18 21:09:33 +00:00
|
|
|
right={() => (selected ? <List.Icon name='check' color={themes[theme!].tintColor} /> : null)}
|
2020-04-06 21:40:18 +00:00
|
|
|
onPress={onItemPress}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2020-04-06 21:40:18 +00:00
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
const RenderSearch = ({ hasSearch, onChangeText }: IRenderSearch) => {
|
|
|
|
if (!hasSearch) {
|
|
|
|
return <List.Separator style={styles.listNoHeader} />;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-06 13:53:02 +00:00
|
|
|
<SearchBox onChangeText={onChangeText} />
|
2022-02-17 14:57:30 +00:00
|
|
|
<List.Separator />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
class PickerView extends React.PureComponent<IPickerViewProps, IPickerViewState> {
|
2022-02-17 14:57:30 +00:00
|
|
|
private onSearch?: (text?: string) => Promise<any>;
|
2021-11-17 19:56:28 +00:00
|
|
|
|
|
|
|
static navigationOptions = ({ route }: IPickerViewProps) => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: route.params?.title ?? I18n.t('Select_an_option')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2020-04-06 21:40:18 +00:00
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
constructor(props: IPickerViewProps) {
|
2020-04-06 21:40:18 +00:00
|
|
|
super(props);
|
2020-06-15 14:00:46 +00:00
|
|
|
const data = props.route.params?.data ?? [];
|
2022-02-17 14:57:30 +00:00
|
|
|
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;
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
onChangeValue = (value: string) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation, route } = this.props;
|
|
|
|
const goBack = route.params?.goBack ?? true;
|
|
|
|
const onChange = route.params?.onChangeValue ?? (() => {});
|
2020-04-06 21:40:18 +00:00
|
|
|
onChange(value);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (goBack) {
|
|
|
|
navigation.goBack();
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2022-02-17 14:57:30 +00:00
|
|
|
onChangeText = debounce(async (searchText: string) => {
|
|
|
|
if (this.onSearch) {
|
|
|
|
const data = await this.onSearch(searchText);
|
|
|
|
if (data?.data) {
|
|
|
|
this.setState({ ...data, searchText });
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
}
|
2022-02-17 14:57:30 +00:00
|
|
|
}, 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] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-06 21:40:18 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { data, value } = this.state;
|
|
|
|
const { theme } = this.props;
|
|
|
|
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView>
|
2020-05-08 17:36:10 +00:00
|
|
|
<FlatList
|
|
|
|
data={data}
|
2021-12-03 19:27:57 +00:00
|
|
|
keyExtractor={item => item.value as string}
|
2020-05-08 17:36:10 +00:00
|
|
|
renderItem={({ item }) => (
|
|
|
|
<Item
|
|
|
|
item={item}
|
|
|
|
theme={theme}
|
|
|
|
selected={!this.onSearch && (value || data[0]?.value) === item.value}
|
2021-12-03 19:27:57 +00:00
|
|
|
onItemPress={() => this.onChangeValue(item.value as string)}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-02-17 14:57:30 +00:00
|
|
|
onEndReached={() => this.onEndReached()}
|
|
|
|
onEndReachedThreshold={0.5}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2022-02-17 14:57:30 +00:00
|
|
|
ListHeaderComponent={<RenderSearch hasSearch={!!this.onSearch} onChangeText={this.onChangeText} />}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListFooterComponent={List.Separator}
|
2021-09-13 20:41:05 +00:00
|
|
|
ListEmptyComponent={() => (
|
2023-05-18 21:09:33 +00:00
|
|
|
<Text style={[styles.noResult, { color: themes[theme!].titleText }]}>{I18n.t('No_results_found')}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
2020-05-08 17:36:10 +00:00
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
</SafeAreaView>
|
2020-04-06 21:40:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(PickerView);
|