2018-01-30 19:48:26 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { Text, TouchableOpacity, FlatList } from 'react-native';
|
2018-01-30 19:48:26 +00:00
|
|
|
import { responsive } from 'react-native-responsive-ui';
|
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';
|
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
|
|
|
|
2018-09-11 16:32:52 +00:00
|
|
|
const renderEmoji = (emoji, size, baseUrl) => {
|
2019-11-25 20:01:17 +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-09-16 20:50:51 +00:00
|
|
|
{shortnameToUnicode(`:${ emoji }:`)}
|
2018-01-30 19:48:26 +00:00
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-08-07 13:51:34 +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,
|
|
|
|
window: PropTypes.any,
|
|
|
|
onEmojiSelected: PropTypes.func,
|
|
|
|
emojisPerRow: PropTypes.number,
|
|
|
|
width: PropTypes.number
|
2018-09-25 19:28:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +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-11-25 20:01:17 +00:00
|
|
|
key={emoji && emoji.isCustom ? emoji.content : emoji}
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress={() => onEmojiSelected(emoji)}
|
2019-11-25 20:01:17 +00:00
|
|
|
testID={`reaction-picker-${ emoji && emoji.isCustom ? emoji.content : emoji }`}
|
2018-01-30 19:48:26 +00:00
|
|
|
>
|
2019-11-25 20:01:17 +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-11-25 20:01:17 +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-11-25 20:01:17 +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-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
|
|
|
|
|
|
|
export default responsive(EmojiCategory);
|