2023-06-27 02:24:43 +00:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2020-02-11 14:01:35 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2023-06-27 02:24:43 +00:00
|
|
|
const placeholderObject = useMemo(
|
|
|
|
() =>
|
|
|
|
placeholder && !items.some(item => item.label === textParser([placeholder]))
|
|
|
|
? { label: textParser([placeholder]), value: null }
|
|
|
|
: {},
|
|
|
|
[items.length, placeholder?.text]
|
|
|
|
);
|
|
|
|
|
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 (
|
|
|
|
<RNPickerSelect
|
|
|
|
items={items}
|
2023-06-27 02:24:43 +00:00
|
|
|
placeholder={placeholderObject}
|
2020-02-11 14:01:35 +00:00
|
|
|
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={{
|
2023-08-18 17:48:33 +00:00
|
|
|
// style property was Omitted in lib, but can be used normally
|
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
style: { ...styles.pickerText, color: selected ? themes[theme].titleText : themes[theme].auxiliaryText }
|
|
|
|
}}
|
2020-02-11 14:01:35 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|