2020-04-06 21:40:18 +00:00
|
|
|
import React from 'react';
|
2021-11-17 19:56:28 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, StyleSheet, Text, View } from 'react-native';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { themes } from '../constants/colors';
|
2020-05-08 17:36:10 +00:00
|
|
|
import debounce from '../utils/debounce';
|
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
|
|
|
search: {
|
|
|
|
width: '100%',
|
|
|
|
height: 56
|
|
|
|
},
|
|
|
|
noResult: {
|
|
|
|
fontSize: 16,
|
|
|
|
paddingVertical: 56,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textSemibold,
|
|
|
|
...sharedStyles.textAlignCenter
|
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;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IPickerViewState {
|
2021-12-03 19:27:57 +00:00
|
|
|
data: IOptionsField[];
|
2021-11-17 19:56:28 +00:00
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IPickerViewProps {
|
2021-12-03 19:27:57 +00:00
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'PickerView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'PickerView'>;
|
2021-11-17 19:56:28 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 })}
|
2020-10-30 13:59:44 +00:00
|
|
|
right={selected && (() => <List.Icon name='check' color={themes[theme].tintColor} />)}
|
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
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
2021-11-17 19:56:28 +00:00
|
|
|
class PickerView extends React.PureComponent<IPickerViewProps, IPickerViewState> {
|
2021-12-03 19:27:57 +00:00
|
|
|
private onSearch?: ((text: string) => IOptionsField[]) | ((term?: string | undefined) => 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 ?? [];
|
|
|
|
const value = props.route.params?.value;
|
2020-04-06 21:40:18 +00:00
|
|
|
this.state = { data, value };
|
2020-05-08 17:36:10 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
this.onSearch = props.route.params?.onChangeText;
|
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
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
onChangeText = debounce(
|
2021-11-17 19:56:28 +00:00
|
|
|
async (text: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (this.onSearch) {
|
|
|
|
const data = await this.onSearch(text);
|
|
|
|
this.setState({ data });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
300,
|
|
|
|
true
|
|
|
|
);
|
2020-05-08 17:36:10 +00:00
|
|
|
|
|
|
|
renderSearch() {
|
|
|
|
if (!this.onSearch) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.search}>
|
|
|
|
<SearchBox onChangeText={this.onChangeText} />
|
|
|
|
</View>
|
|
|
|
);
|
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
|
|
|
{this.renderSearch()}
|
|
|
|
<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
|
|
|
/>
|
|
|
|
)}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
2021-09-13 20:41:05 +00:00
|
|
|
ListEmptyComponent={() => (
|
|
|
|
<Text style={[styles.noResult, { color: themes[theme].titleText }]}>{I18n.t('No_results_found')}</Text>
|
|
|
|
)}
|
2020-10-30 13:59:44 +00:00
|
|
|
contentContainerStyle={[List.styles.contentContainerStyleFlatList]}
|
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);
|