Rocket.Chat.ReactNative/app/containers/UIKit/Select.tsx

97 lines
2.5 KiB
TypeScript
Raw Normal View History

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';
import { themes } from '../../lib/constants';
import { CustomIcon } from '../CustomIcon';
2020-02-11 14:01:35 +00:00
import { textParser } from './utils';
import { isAndroid, isIOS } from '../../lib/methods/helpers';
2020-02-11 14:01:35 +00:00
import ActivityIndicator from '../ActivityIndicator';
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,
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
}
});
interface ISelect {
options?: Option[];
placeholder?: IText;
onChange: Function;
loading: boolean;
disabled?: boolean;
value: [];
}
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 : {}),
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
borderColor: themes[theme].strokeLight,
backgroundColor: themes[theme].surfaceRoom
2020-02-11 14:01:35 +00:00
};
const placeholderObject = useMemo(
() =>
placeholder && !items.some(item => item.label === textParser([placeholder]))
? { label: textParser([placeholder]), value: null }
: {},
[items.length, placeholder?.text]
);
const Icon = () =>
loading ? (
<ActivityIndicator style={styles.loading} />
) : (
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
<CustomIcon size={22} name='chevron-down' style={isAndroid && styles.icon} color={themes[theme].fontSecondaryInfo} />
);
2020-02-11 14:01:35 +00:00
return (
<RNPickerSelect
items={items}
placeholder={placeholderObject}
2020-02-11 14:01:35 +00:00
useNativeAndroidPickerStyle={false}
value={selected}
disabled={disabled}
onValueChange={value => {
2020-02-11 14:01:35 +00:00
onChange({ value });
setSelected(value);
}}
style={{
viewContainer: pickerStyle,
inputAndroidContainer: pickerStyle
}}
Icon={Icon}
textInputProps={{
// style property was Omitted in lib, but can be used normally
// @ts-ignore
feat: mobile color normalization (#5616) * chore: remove auxiliaryText color * chore: remove titleText * chore: password colors change * chore: use fontDefault on ActionSheet item * wip: type * chore: set custom icon default color * remove tintActive color * only set color when checked * remove icon color * remove tintActive * chore: remove STATUS_COLORS * chore: remove mentions colors * chore: remove switch color * chore: background color * chore: auxiliaryBackground * chore: one local colors * wip: some colors * wip: colors * wip: colors * wip: end colors * test: update * chore: fix some colors * chore: fix lint * chore: back to text props * chore: fix ts errors * revert * chore: fix lint * test: update snapshot * update storybook * cocoapods * fix app theme color * remove unused color * fix login service button color * remove unused color * unused backgroundColor * fix background color * fix transparent color * fix background color * wip: key * fix color * chore: revert to 1 tick * test: update * chore: use same color as front end * test: update * fix radius * fix background color * fix wrong color * change some colors * chore: update stories * chore: fix button style * chore: fix item color * lint * update snapshot * link * remove background color * change send to channel color * call icons * video conf colors * fix app default color * bottom sheet * remove background * two factor color * update tests * feat: add force-logout stream listener * remove icon colors * improve badge color * update tests * fix header colors * fix collapsible icon color * imagePreview color * wip * update test * lint --------- Co-authored-by: Diego Mello <diegolmello@gmail.com>
2024-04-18 10:19:54 +00:00
style: { ...styles.pickerText, color: selected ? themes[theme].fontTitlesLabels : themes[theme].fontSecondaryInfo }
}}
2020-02-11 14:01:35 +00:00
/>
);
};