2018-01-30 19:48:26 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-12-11 23:01:12 +00:00
|
|
|
import { Text, TouchableOpacity, FlatList } from 'react-native';
|
2019-01-29 19:52:56 +00:00
|
|
|
|
2020-01-29 20:20:36 +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';
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
const EMOJI_SIZE = 50;
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
const renderEmoji = (emoji, size, baseUrl) => {
|
2019-12-11 23:01:12 +00:00
|
|
|
if (emoji && emoji.isCustom) {
|
|
|
|
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 }]}>
|
2019-10-07 20:56:30 +00:00
|
|
|
{shortnameToUnicode(`:${ emoji }:`)}
|
2018-01-30 19:48:26 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
class EmojiCategory extends React.Component {
|
2018-01-30 19:48:26 +00:00
|
|
|
static propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2018-01-30 19:48:26 +00:00
|
|
|
emojis: PropTypes.any,
|
|
|
|
onEmojiSelected: PropTypes.func,
|
|
|
|
emojisPerRow: PropTypes.number,
|
|
|
|
width: PropTypes.number
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 23:01:12 +00:00
|
|
|
renderItem(emoji) {
|
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-12-11 23:01:12 +00:00
|
|
|
key={emoji && emoji.isCustom ? emoji.content : emoji}
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress={() => onEmojiSelected(emoji)}
|
2019-12-11 23:01:12 +00:00
|
|
|
testID={`reaction-picker-${ emoji && emoji.isCustom ? emoji.content : emoji }`}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
2019-12-11 23:01:12 +00:00
|
|
|
{renderEmoji(emoji, EMOJI_SIZE, baseUrl)}
|
2019-02-07 15:48:10 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-12-11 23:01:12 +00:00
|
|
|
const { emojis, width } = this.props;
|
|
|
|
|
|
|
|
if (!width) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const numColumns = Math.trunc(width / EMOJI_SIZE);
|
|
|
|
const marginHorizontal = (width - (numColumns * EMOJI_SIZE)) / 2;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-02-08 14:08:50 +00:00
|
|
|
return (
|
2019-12-11 23:01:12 +00:00
|
|
|
<FlatList
|
|
|
|
contentContainerStyle={{ marginHorizontal }}
|
|
|
|
// rerender FlatList in case of width changes
|
|
|
|
key={`emoji-category-${ width }`}
|
|
|
|
keyExtractor={item => (item && item.isCustom && item.content) || item}
|
2018-09-25 19:28:42 +00:00
|
|
|
data={emojis}
|
2019-12-11 23:01:12 +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-20 19:24:22 +00:00
|
|
|
|
2020-07-06 19:01:35 +00:00
|
|
|
export default EmojiCategory;
|