2018-01-30 19:48:26 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, Text, TouchableOpacity } from 'react-native';
|
2019-01-29 19:52:56 +00:00
|
|
|
|
2019-12-11 19:00:38 +00:00
|
|
|
import shortnameToUnicode from '../../utils/shortnameToUnicode';
|
2018-01-30 19:48:26 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import CustomEmoji from './CustomEmoji';
|
2018-02-08 14:08:50 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { IEmoji, IEmojiCategory } from './interfaces';
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
const EMOJI_SIZE = 50;
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const renderEmoji = (emoji: IEmoji, size: number, baseUrl: string) => {
|
2019-11-25 20:01:17 +00:00
|
|
|
if (emoji && emoji.isCustom) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return (
|
|
|
|
<CustomEmoji
|
|
|
|
style={[styles.customCategoryEmoji, { height: size - 16, width: size - 16 }]}
|
|
|
|
emoji={emoji}
|
|
|
|
baseUrl={baseUrl}
|
|
|
|
/>
|
|
|
|
);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Text style={[styles.categoryEmoji, { height: size, width: size, fontSize: size - 14 }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
{shortnameToUnicode(`:${emoji}:`)}
|
2018-01-30 19:48:26 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
class EmojiCategory extends React.Component<Partial<IEmojiCategory>> {
|
|
|
|
renderItem(emoji: any) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { baseUrl, onEmojiSelected } = this.props;
|
2018-01-30 19:48:26 +00:00
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
activeOpacity={0.7}
|
2019-11-25 20:01:17 +00:00
|
|
|
key={emoji && emoji.isCustom ? emoji.content : emoji}
|
2021-09-13 20:41:05 +00:00
|
|
|
onPress={() => onEmojiSelected!(emoji)}
|
|
|
|
testID={`reaction-picker-${emoji && emoji.isCustom ? emoji.content : emoji}`}>
|
|
|
|
{renderEmoji(emoji, EMOJI_SIZE, baseUrl!)}
|
2019-02-07 15:48:10 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-11-25 20:01:17 +00:00
|
|
|
const { emojis, width } = this.props;
|
|
|
|
|
|
|
|
if (!width) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const numColumns = Math.trunc(width / EMOJI_SIZE);
|
2021-09-13 20:41:05 +00:00
|
|
|
const marginHorizontal = (width - numColumns * EMOJI_SIZE) / 2;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
return (
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2019-11-25 20:01:17 +00:00
|
|
|
<FlatList
|
|
|
|
contentContainerStyle={{ marginHorizontal }}
|
|
|
|
// rerender FlatList in case of width changes
|
2021-09-13 20:41:05 +00:00
|
|
|
key={`emoji-category-${width}`}
|
2019-11-25 20:01:17 +00:00
|
|
|
keyExtractor={item => (item && item.isCustom && item.content) || item}
|
2018-09-25 19:28:42 +00:00
|
|
|
data={emojis}
|
2019-11-25 20:01:17 +00:00
|
|
|
extraData={this.props}
|
|
|
|
renderItem={({ item }) => this.renderItem(item)}
|
|
|
|
numColumns={numColumns}
|
2018-02-08 14:08:50 +00:00
|
|
|
initialNumToRender={45}
|
|
|
|
removeClippedSubviews
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
/>
|
|
|
|
);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
export default EmojiCategory;
|