'@'mention integration with react native gifted chat

February 22, 2020

•

3 min read

'@'mention integration with react native gifted chat

 With this article we can learn about how to implement '@' mention in chat applications for mobiles with react-native-gifted-chat.

What is mention?

“
First question that comes to mind is that about mention, mention is mainly used for group chatting in that we can mention a member who's already join the group. We can also use it for giving suggestions of hashtags (for eg. #like, #friends) when # key triggered from keyboard it will showing a popup and we can select a suggested option from list of suggestions.

Firstly, initial process to start install react-native-gifted-chat using following:

  • Using npm: npm install react-native-gifted-chat --save
  • Using Yarn: yarn add react-native-gifted-chat
“
If you’re getting errors or want to get know more about props, methods and integration of this particular package please click on this

Now, after performing installation steps start implementation of react-native-gifted-chat  into your project as per your requirement.


Add gifted chat in your app

  • By adding this into your app you can able to see chat input box and messages on your screen.
  • In this all the props which are shown in code snippet are the basic props of gifted chat library.
  • Add following code snippet into your file:
1<GiftedChat messages={this.state.messages}
2 onSend={messages => this.onSend(messages)}
3 renderComposer={this.renderComposer}
4 listViewProps={{
5 style: styles.listContainer,
6 contentContainerStyle: styles.msgListContainer,
7 }}
8 user={{
9 _id: 1,
10 }}
11 renderMessage={(props) => {
12 return <MessageBubble messageObj={props.currentMessage}
13 position= {props.position} />}}
14 renderAvatar={null}
15 renderSend={this.renderSend}
16 text={this.state.messageText}
17 alwaysShowSend={true}
18 minComposerHeight={55}
19 maxComposerHeight={55}
20 bottomOffset={Platform.select({
21 ios: 200,
22 android: 0
23 })}
24 />
  • After adding above code snippet into your file it will create ui like below:
  • For more UI styling and detail about screen please download project from link and refer app.js file

Integration of message composer

  • For creating message composer import textInput component from react-native package and you can also use other input libraries for creating text-input. In this,  I am using default text-input for message composer.
  • From above code snippet of please find renderComposer property. It looks like following:
1renderComposer = (props) => {
2 return (
3 <View style={styles.composerContainer}>
4 <View style={styles.inputContainer}>
5 <TextInput {...props}
6 placeholder={'Type something...'}
7 ref={(input) => { this.msgInput = input; }}
8 onChangeText={(value) => this.onTextChange(value, props)}
9 style={styles.textInput}
10 value={props.text}
11 multiline={true}
12 />
13 </View>
14 <Send {...props} containerStyle={styles.sendWrapperStyle} >
15 <View style={styles.sendContainer}>
16 <Image source={require('./images/Send.png')} style={styles.sendIconStyle} />
17 </View>
18 </Send>
19 </View>
20 )
21 }
  • From above code onChangeText property will help to generate UI as following:
  • onChangeText method containing following code:
1onTextChange = (value, props) => {
2 const lastChar = this.state.messageText.substr(this.state.messageText.length - 1)
3 const currentChar = value.substr(value.length - 1)
4 const spaceCheck = /[^@A-Za-z_]/g
5 props.onTextChanged(value)
6 this.setState({
7 messageText: value
8 })
9 if(value.length === 0) {
10 this.setModalVisible(false)
11 } else {
12 if (spaceCheck.test(lastChar) && currentChar != '@') {
13 this.setModalVisible(false)
14 } else {
15 const checkSpecialChar = currentChar.match(/[^@A-Za-z_]/)
16 if (checkSpecialChar === null || currentChar === '@') {
17 const pattern = new RegExp(`\\B@[a-z0-9_-]+|\\B@`, `gi`);
18 const matches = value.match(pattern) || []
19 if (matches.length > 0) {
20 this.getUserSuggestions(matches[matches.length - 1])
21 this.setModalVisible(true)
22 } else {
23 this.setModalVisible(false)
24 }
25 } else if (checkSpecialChar != null) {
26 this.setModalVisible(false)
27 }
28 }
29 }
30 }
31
32getUserSuggestions = (keyword) => {
33 this.setState({
34 isLoading: true
35 }, () => {
36 if(Array.isArray(userList)) {
37 if(keyword.slice(1) === '') {
38 this.setState({
39 userData: [...userList],
40 isLoading: false
41 })
42 } else {
43 const userDataList = userList.filter(obj => obj.name.indexOf(keyword.slice(1)) !== -1)
44 this.setState({
45 userData: [...userDataList],
46 isLoading: false
47 })
48 }
49 }
50 })
51 }
  • By following above code, we can create suggestion list on trigger of specific keyword character @.

If you want to change triggering of  @ you can replace all the @ inside onChangeText method and replace with your special characters like # + = $ % & and it will trigger suggestion method whenever it will checks all the conditions.

  • By following above code it will help to integrate @ mention with react-naitve-gifted-chat.
  • If you want code sample please download it from link. This will help you to understand key logic of @ mention suggesting user name as a tappable list.
  • In this sample, I have also used for displaying suggestion panel react-native-modal package.

That's it. If you have any queries and want to ask any questions please ask me in comment section.
Thanks for reading my article. :)