forked from yonahforst/react-native-emoji-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (175 loc) · 4.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict'
import React, {
PropTypes,
Component,
} from 'react'
import {
StyleSheet,
ScrollView,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
View,
Platform,
} from 'react-native'
import emoji from 'emoji-datasource'
import {
groupBy,
orderBy,
includes,
} from 'lodash/collection'
import {
mapValues,
} from 'lodash/object'
//polyfil for android
require('string.fromcodepoint');
// i dont understand ANY of this but there's somethign called codepoints and surrogate pairs
// and this converts utf16 to a charachter in javascript. see more here:
//https://mathiasbynens.be/notes/javascript-unicode
//https://mathiasbynens.be/notes/javascript-escapes#unicode-code-point
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
const charFromUtf16 = utf16 => String.fromCodePoint(...utf16.split('-').map(u => '0x' + u))
const charFromEmojiObj = obj => charFromUtf16(obj.unified)
const blacklistedEmojis = ['white_frowning_face', 'keycap_star', 'eject']
const isAndroid = Platform.OS == 'android'
const letterSpacing = 10
const defaultEmojiSize = 30
const padding = 5
const filteredEmojis = emoji.filter(e => isAndroid ? !!e.google : !includes(blacklistedEmojis, e.short_name))
// sort emojis by 'sort_order' then group them into categories
const groupedAndSorted = groupBy(orderBy(filteredEmojis, 'sort_order'), 'category')
// convert the emoji object to a character
const emojisByCategory = mapValues(groupedAndSorted, group => group.map(charFromEmojiObj))
const CATEGORIES = ['People', 'Nature', 'Foods', 'Activity', 'Places', 'Objects', 'Symbols', 'Flags']
class EmojiPicker extends Component {
state = {
categories: CATEGORIES.slice(0, 1),
}
componentWillUnmount() {
clearTimeout(this._timeout)
}
loadNextCategory() {
if (this.state.categories.length < CATEGORIES.length) {
this.setState({categories: CATEGORIES.slice(0, this.state.categories.length + 1)})
}
}
renderCategory(category) {
return (
<EmojiCategory
{...this.props}
key={category}
category={category}
finishedLoading={() => this._timeout = setTimeout(this.loadNextCategory.bind(this), 100)} />
)
}
render() {
return (
<View style={[styles.container, this.props.style]}>
<ScrollView horizontal={true}>
{this.state.categories.map(this.renderCategory.bind(this))}
</ScrollView>
{this.props.hideClearButton ? null : <ClearButon {...this.props} />}
</View>
)
}
}
class EmojiCategory extends Component {
componentDidMount() {
this.props.finishedLoading()
}
render() {
let emojis = emojisByCategory[this.props.category]
let size = this.props.emojiSize || defaultEmojiSize
let style = {
fontSize: size-10,
color: 'black',
height: size+4,
width: size+4,
textAlign: 'center',
padding: padding,
}
return (
<View style={style.categoryOuter}>
<Text style={[styles.headerText, this.props.headerStyle]}>{this.props.category}</Text>
<View style={styles.categoryInner}>
{emojis.map(e =>
<Text style={style}
key={e}
onPress={() => this.props.onEmojiSelected(e)}>
{e}
</Text>
)}
</View>
</View>
)
}
}
const ClearButon = props => {
return (
<TouchableOpacity
onPress={() => props.onEmojiSelected(null)}>
<Text style={[styles.clearButton, props.clearButtonStyle]}>
{props.clearButtonText || 'Clear'}
</Text>
</TouchableOpacity>
)
}
const EmojiOverlay = props => (
<View style={[styles.absolute, props.visible ? styles.visible : styles.hidden]}>
<TouchableOpacity style={styles.absolute} onPress={props.onTapOutside}>
<View style={styles.background} />
</TouchableOpacity>
{props.visible ? <EmojiPicker {...props}/> : null}
</View>
)
let styles = StyleSheet.create({
container: {
padding: padding,
},
clearButton: {
flex: 1,
padding: 15,
textAlign: 'center',
color: 'black',
textAlignVertical: 'center',
},
absolute: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
},
visible: {
top: 0,
flex: 1,
justifyContent: 'center',
},
hidden: {
top: 1000,
flex: 1,
},
background: {
flex: 1,
backgroundColor: 'grey',
opacity: 0.5,
},
categoryOuter: {
flex: -1,
},
categoryInner: {
flex: 1,
flexWrap: 'wrap',
flexDirection: 'column',
},
headerText: {
padding: padding,
color: 'black',
justifyContent: 'center',
textAlignVertical: 'center',
},
})
EmojiPicker.propTypes = {
onEmojiSelected: PropTypes.func.isRequired,
}
export { EmojiPicker as default, EmojiOverlay as EmojiOverlay }