2018-01-30 19:48:26 +00:00
|
|
|
import React from 'react';
|
2022-10-21 18:27:55 +00:00
|
|
|
import { useWindowDimensions } from 'react-native';
|
|
|
|
import { FlatList } from 'react-native-gesture-handler';
|
2019-01-29 19:52:56 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
import { EMOJI_BUTTON_SIZE } from './styles';
|
2022-06-06 14:17:51 +00:00
|
|
|
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
2022-10-21 18:27:55 +00:00
|
|
|
import { IEmoji } from '../../definitions/IEmoji';
|
|
|
|
import { PressableEmoji } from './PressableEmoji';
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
interface IEmojiCategoryProps {
|
|
|
|
emojis: IEmoji[];
|
|
|
|
onEmojiSelected: (emoji: IEmoji) => void;
|
|
|
|
tabLabel?: string; // needed for react-native-scrollable-tab-view only
|
|
|
|
}
|
2018-01-30 19:48:26 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const EmojiCategory = ({ onEmojiSelected, emojis }: IEmojiCategoryProps): React.ReactElement | null => {
|
|
|
|
const { width } = useWindowDimensions();
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const numColumns = Math.trunc(width / EMOJI_BUTTON_SIZE);
|
|
|
|
const marginHorizontal = (width % EMOJI_BUTTON_SIZE) / 2;
|
2019-11-25 20:01:17 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
const renderItem = ({ item }: { item: IEmoji }) => <PressableEmoji emoji={item} onPress={onEmojiSelected} />;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2022-10-21 18:27:55 +00:00
|
|
|
if (!width) {
|
|
|
|
return null;
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
2022-10-21 18:27:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
// needed to update the numColumns when the width changes
|
|
|
|
key={`emoji-category-${width}`}
|
|
|
|
keyExtractor={item => (typeof item === 'string' ? item : item.name)}
|
|
|
|
data={emojis}
|
|
|
|
renderItem={renderItem}
|
|
|
|
numColumns={numColumns}
|
|
|
|
initialNumToRender={45}
|
|
|
|
removeClippedSubviews
|
|
|
|
contentContainerStyle={{ marginHorizontal }}
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
keyboardDismissMode={'none'}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2020-06-17 17:35:58 +00:00
|
|
|
export default EmojiCategory;
|