verdnatura-chat/app/containers/EmojiPicker/EmojiCategory.js

75 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

import React from 'react';
import PropTypes from 'prop-types';
2019-11-25 20:01:17 +00:00
import { Text, TouchableOpacity, FlatList } from 'react-native';
2019-01-29 19:52:56 +00:00
import shortnameToUnicode from '../../utils/shortnameToUnicode';
import styles from './styles';
import CustomEmoji from './CustomEmoji';
import scrollPersistTaps from '../../utils/scrollPersistTaps';
2019-11-25 20:01:17 +00:00
const EMOJI_SIZE = 50;
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} />;
}
return (
<Text style={[styles.categoryEmoji, { height: size, width: size, fontSize: size - 14 }]}>
{shortnameToUnicode(`:${ emoji }:`)}
</Text>
);
};
class EmojiCategory extends React.Component {
static propTypes = {
baseUrl: PropTypes.string.isRequired,
emojis: PropTypes.any,
onEmojiSelected: PropTypes.func,
emojisPerRow: PropTypes.number,
width: PropTypes.number
}
2019-11-25 20:01:17 +00:00
renderItem(emoji) {
const { baseUrl, onEmojiSelected } = this.props;
return (
<TouchableOpacity
activeOpacity={0.7}
2019-11-25 20:01:17 +00:00
key={emoji && emoji.isCustom ? emoji.content : emoji}
onPress={() => onEmojiSelected(emoji)}
2019-11-25 20:01:17 +00:00
testID={`reaction-picker-${ emoji && emoji.isCustom ? emoji.content : emoji }`}
>
2019-11-25 20:01:17 +00:00
{renderEmoji(emoji, EMOJI_SIZE, baseUrl)}
2019-02-07 15:48:10 +00:00
</TouchableOpacity>
);
}
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;
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}
data={emojis}
2019-11-25 20:01:17 +00:00
extraData={this.props}
renderItem={({ item }) => this.renderItem(item)}
numColumns={numColumns}
initialNumToRender={45}
removeClippedSubviews
{...scrollPersistTaps}
/>
);
}
}
export default EmojiCategory;