tooot/src/screens/Shared/Compose/Emojis.tsx

107 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-12-03 22:03:06 +01:00
import React, { Dispatch } from 'react'
import {
Image,
Pressable,
SectionList,
StyleSheet,
Text,
TextInput,
View
} from 'react-native'
import { StyleConstants } from 'src/utils/styles/constants'
import { useTheme } from 'src/utils/styles/ThemeManager'
2020-12-07 12:31:40 +01:00
import { PostAction, ComposeState } from '../Compose'
2020-12-03 22:03:06 +01:00
import updateText from './updateText'
export interface Props {
textInputRef: React.RefObject<TextInput>
2020-12-07 12:31:40 +01:00
composeState: ComposeState
composeDispatch: Dispatch<PostAction>
2020-12-03 22:03:06 +01:00
}
const ComposeEmojis: React.FC<Props> = ({
textInputRef,
2020-12-07 12:31:40 +01:00
composeState,
composeDispatch
2020-12-03 22:03:06 +01:00
}) => {
const { theme } = useTheme()
return (
<View style={styles.base}>
<SectionList
horizontal
2020-12-06 22:32:36 +01:00
keyboardShouldPersistTaps='handled'
2020-12-07 12:31:40 +01:00
sections={composeState.emoji.emojis!}
2020-12-03 22:03:06 +01:00
keyExtractor={item => item.shortcode}
renderSectionHeader={({ section: { title } }) => (
<Text style={[styles.group, { color: theme.secondary }]}>
{title}
</Text>
)}
renderItem={({ section, index }) => {
if (index === 0) {
return (
<View key={section.title} style={styles.emojis}>
{section.data.map(emoji => (
<Pressable
key={emoji.shortcode}
onPress={() => {
updateText({
2020-12-06 23:51:13 +01:00
origin: textInputRef.current?.isFocused()
? 'text'
: 'spoiler',
2020-12-07 12:31:40 +01:00
composeState,
composeDispatch,
2020-12-04 01:17:10 +01:00
newText: `:${emoji.shortcode}:`,
type: 'emoji'
2020-12-03 22:03:06 +01:00
})
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-04 01:17:10 +01:00
type: 'emoji',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.emoji, active: false }
2020-12-04 01:17:10 +01:00
})
2020-12-03 22:03:06 +01:00
}}
>
<Image source={{ uri: emoji.url }} style={styles.emoji} />
</Pressable>
))}
</View>
)
} else {
return null
}
}}
/>
</View>
)
}
const styles = StyleSheet.create({
base: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-around',
height: 260
},
group: {
position: 'absolute',
left: StyleConstants.Spacing.L,
fontSize: StyleConstants.Font.Size.S
},
emojis: {
flex: 1,
flexWrap: 'wrap',
marginTop: StyleConstants.Spacing.M,
marginLeft: StyleConstants.Spacing.M
},
emoji: {
width: 32,
height: 32,
padding: StyleConstants.Spacing.S,
margin: StyleConstants.Spacing.S
}
})
export default ComposeEmojis