2020-04-06 21:40:18 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-05-08 17:36:10 +00:00
|
|
|
import {
|
|
|
|
View, FlatList, StyleSheet, Text
|
|
|
|
} 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-04-06 21:40:18 +00:00
|
|
|
import sharedStyles from './Styles';
|
|
|
|
|
|
|
|
import ListItem from '../containers/ListItem';
|
|
|
|
import Check from '../containers/Check';
|
|
|
|
import Separator from '../containers/Separator';
|
2020-05-08 17:36:10 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2020-04-06 21:40:18 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
check: {
|
|
|
|
marginHorizontal: 0
|
2020-05-08 17:36:10 +00:00
|
|
|
},
|
|
|
|
search: {
|
|
|
|
width: '100%',
|
|
|
|
height: 56
|
|
|
|
},
|
|
|
|
noResult: {
|
|
|
|
fontSize: 16,
|
|
|
|
paddingVertical: 56,
|
|
|
|
...sharedStyles.textAlignCenter,
|
|
|
|
...sharedStyles.textSemibold
|
|
|
|
},
|
|
|
|
withoutBorder: {
|
|
|
|
borderBottomWidth: 0,
|
|
|
|
borderTopWidth: 0
|
2020-04-06 21:40:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Item = React.memo(({
|
|
|
|
item,
|
|
|
|
selected,
|
|
|
|
onItemPress,
|
|
|
|
theme
|
|
|
|
}) => (
|
|
|
|
<ListItem
|
|
|
|
title={item.label}
|
|
|
|
right={selected && (() => <Check theme={theme} style={styles.check} />)}
|
|
|
|
onPress={onItemPress}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
Item.propTypes = {
|
|
|
|
item: PropTypes.object,
|
|
|
|
selected: PropTypes.bool,
|
|
|
|
onItemPress: PropTypes.func,
|
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
class PickerView extends React.PureComponent {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ route }) => ({
|
|
|
|
title: route.params?.title ?? I18n.t('Select_an_option')
|
2020-04-06 21:40:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2020-04-06 21:40:18 +00:00
|
|
|
theme: PropTypes.string
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
onChangeValue = (value) => {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeText = debounce(async(text) => {
|
|
|
|
if (this.onSearch) {
|
|
|
|
const data = await this.onSearch(text);
|
|
|
|
this.setState({ data });
|
|
|
|
}
|
|
|
|
}, 300, true)
|
|
|
|
|
|
|
|
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-05-08 17:36:10 +00:00
|
|
|
<>
|
|
|
|
{this.renderSearch()}
|
|
|
|
<FlatList
|
|
|
|
data={data}
|
|
|
|
keyExtractor={item => item.value}
|
|
|
|
renderItem={({ item }) => (
|
|
|
|
<Item
|
|
|
|
item={item}
|
|
|
|
theme={theme}
|
|
|
|
selected={!this.onSearch && (value || data[0]?.value) === item.value}
|
|
|
|
onItemPress={() => this.onChangeValue(item.value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
ItemSeparatorComponent={() => <Separator theme={theme} />}
|
|
|
|
ListEmptyComponent={() => <Text style={[styles.noResult, { color: themes[theme].titleText }]}>{I18n.t('No_results_found')}</Text>}
|
|
|
|
contentContainerStyle={[
|
|
|
|
sharedStyles.listContentContainer,
|
|
|
|
{
|
|
|
|
backgroundColor: themes[theme].auxiliaryBackground,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
},
|
|
|
|
!data.length && styles.withoutBorder
|
|
|
|
]}
|
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
|
|
|
/>
|
|
|
|
</>
|
2020-04-06 21:40:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withTheme(PickerView);
|