Rocket.Chat.ReactNative/app/containers/UIKit/MultiSelect/Items.tsx

62 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-02-11 14:01:35 +00:00
import React from 'react';
import { Text, View } from 'react-native';
2020-02-11 14:01:35 +00:00
import Touchable from 'react-native-platform-touchable';
import FastImage from 'react-native-fast-image';
import { FlatList } from 'react-native-gesture-handler';
2020-02-11 14:01:35 +00:00
import * as List from '../../List';
2020-02-11 14:01:35 +00:00
import { textParser } from '../utils';
import styles from './styles';
import { IItemData } from '.';
import { useTheme } from '../../../theme';
import { CustomIcon } from '../../CustomIcon';
2020-02-11 14:01:35 +00:00
interface IItem {
item: IItemData;
selected: boolean;
onSelect: Function;
}
interface IItems {
items: IItemData[];
selected: IItemData[];
onSelect: Function;
}
const keyExtractor = (item: IItemData) => item.value?.name || item.text?.text;
2020-02-11 14:01:35 +00:00
// RectButton doesn't work on modal (Android)
const Item = ({ item, selected, onSelect }: IItem) => {
const itemName = item.value?.name || item.text.text.toLowerCase();
const { colors } = useTheme();
return (
<Touchable testID={`multi-select-item-${itemName}`} key={itemName} onPress={() => onSelect(item)}>
<View style={styles.item}>
<View style={styles.flexZ}>
{item.imageUrl ? <FastImage style={styles.itemImage} source={{ uri: item.imageUrl }} /> : null}
</View>
<View style={styles.flex}>
<Text numberOfLines={1} style={{ color: colors.titleText }}>
{textParser([item.text])}
</Text>
</View>
<View style={styles.flexZ}>{selected ? <CustomIcon color={colors.tintColor} size={22} name='check' /> : null}</View>
</View>
</Touchable>
);
};
2020-02-11 14:01:35 +00:00
const Items = ({ items, selected, onSelect }: IItems) => (
2020-02-11 14:01:35 +00:00
<FlatList
data={items}
style={styles.items}
contentContainerStyle={styles.itemContent}
2020-02-11 14:01:35 +00:00
keyboardShouldPersistTaps='always'
ItemSeparatorComponent={List.Separator}
2020-02-11 14:01:35 +00:00
keyExtractor={keyExtractor}
renderItem={({ item }) => <Item item={item} onSelect={onSelect} selected={!!selected.find(s => s.value === item.value)} />}
2020-02-11 14:01:35 +00:00
/>
);
export default Items;