2020-02-11 14:01:35 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import RNPickerSelect from 'react-native-picker-select';
|
|
|
|
|
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2020-02-11 14:01:35 +00:00
|
|
|
import { textParser } from './utils';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isAndroid, isIOS } from '../../lib/methods/helpers';
|
2020-02-11 14:01:35 +00:00
|
|
|
import ActivityIndicator from '../ActivityIndicator';
|
2022-03-29 20:06:50 +00:00
|
|
|
import { useTheme } from '../../theme';
|
|
|
|
import { IText, Option } from './interfaces';
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
iosPadding: {
|
|
|
|
height: 48,
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
viewContainer: {
|
|
|
|
marginBottom: 16,
|
|
|
|
paddingHorizontal: 16,
|
2022-11-11 16:04:57 +00:00
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 4,
|
2020-02-11 14:01:35 +00:00
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
pickerText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 16
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
right: 16
|
|
|
|
},
|
|
|
|
loading: {
|
|
|
|
padding: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface ISelect {
|
2022-03-29 20:06:50 +00:00
|
|
|
options?: Option[];
|
|
|
|
placeholder?: IText;
|
2021-09-13 20:41:05 +00:00
|
|
|
onChange: Function;
|
|
|
|
loading: boolean;
|
2022-03-29 20:06:50 +00:00
|
|
|
disabled?: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
value: [];
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:06:50 +00:00
|
|
|
export const Select = ({ options = [], placeholder, onChange, loading, disabled, value: initialValue }: ISelect) => {
|
|
|
|
const { theme } = useTheme();
|
2020-02-11 14:01:35 +00:00
|
|
|
const [selected, setSelected] = useState(!Array.isArray(initialValue) && initialValue);
|
|
|
|
const items = options.map(option => ({ label: textParser([option.text]), value: option.value }));
|
|
|
|
const pickerStyle = {
|
|
|
|
...styles.viewContainer,
|
|
|
|
...(isIOS ? styles.iosPadding : {}),
|
|
|
|
borderColor: themes[theme].separatorColor,
|
|
|
|
backgroundColor: themes[theme].backgroundColor
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const Icon = () =>
|
|
|
|
loading ? (
|
|
|
|
<ActivityIndicator style={styles.loading} />
|
|
|
|
) : (
|
|
|
|
<CustomIcon size={22} name='chevron-down' style={isAndroid && styles.icon} color={themes[theme].auxiliaryText} />
|
|
|
|
);
|
2020-02-11 14:01:35 +00:00
|
|
|
|
|
|
|
return (
|
2023-03-20 18:14:25 +00:00
|
|
|
// @ts-ignore lib types issues. We need to fork it and maintain or find another lib
|
2020-02-11 14:01:35 +00:00
|
|
|
<RNPickerSelect
|
|
|
|
items={items}
|
|
|
|
placeholder={placeholder ? { label: textParser([placeholder]), value: null } : {}}
|
|
|
|
useNativeAndroidPickerStyle={false}
|
|
|
|
value={selected}
|
|
|
|
disabled={disabled}
|
2021-09-13 20:41:05 +00:00
|
|
|
onValueChange={value => {
|
2020-02-11 14:01:35 +00:00
|
|
|
onChange({ value });
|
|
|
|
setSelected(value);
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
viewContainer: pickerStyle,
|
|
|
|
inputAndroidContainer: pickerStyle
|
|
|
|
}}
|
|
|
|
Icon={Icon}
|
2021-09-13 20:41:05 +00:00
|
|
|
textInputProps={{
|
|
|
|
style: { ...styles.pickerText, color: selected ? themes[theme].titleText : themes[theme].auxiliaryText }
|
|
|
|
}}
|
2020-02-11 14:01:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|