2.2.5 • Published 2 years ago

react-native-controlled-mentions v2.2.5

Weekly downloads
983
License
MIT
Repository
github
Last release
2 years ago

react-native-controlled-mentions npm version

Pretty simple and fully controlled mentions input. It can:

  • Gracefully render formatted mentions directly in RN TextInput component
  • Use value/onChange as in usual TextInput props
  • Completely typed (written on TypeScript)
  • No need native libraries

Demo

Try it on Expo Snack: https://snack.expo.io/@dabakovich/mentionsapp

npm.io

Getting started

Install the library using either Yarn:

yarn add react-native-controlled-mentions

or npm:

npm install --save react-native-controlled-mentions

Example

import * as React from 'react';
import { FC, useState } from 'react';
import { Pressable, SafeAreaView, Text, View } from 'react-native';
import { Mentions, MentionSuggestionsProps, Suggestion } from 'react-native-controlled-mentions';

const suggestions = [
  {id: '1', name: 'David Tabaka'},
  {id: '2', name: 'Mary'},
  {id: '3', name: 'Tony'},
  {id: '4', name: 'Mike'},
  {id: '5', name: 'Grey'},
];

const MentionSuggestions: FC<MentionSuggestionsProps> = ({keyword, onSuggestionPress}) => {
  if (keyword == null) {
    return null;
  }

  return (
    <View>
      {users
        .filter(one => one.name.toLocaleLowerCase().includes(keyword.toLocaleLowerCase()))
        .map(one => (
          <Pressable
            key={one.id}
            onPress={() => onSuggestionPress(one)}

            style={{padding: 12}}
          >
            <Text>{one.name}</Text>
          </Pressable>
        ))
      }
    </View>
  );
}

const App = () => {
  const [value, setValue] = useState('Hello @[Mary](2)! How are you?');

  return (
    <SafeAreaView>
      <Mentions
        value={value}
        onChange={setValue}

        renderSuggestions={MentionSuggestions}

        placeholder="Type here..."
        style={{padding: 12}}
      />
    </SafeAreaView>
  );
};

export default App;

Configuration

The Mentions component supports next props:

Property nameTypeRequiredDefault valueDescription
valuestringtrue
onChangefunction (value)true
renderSuggestionsfunction ({keyword, onSuggestionPress}) ReactNodefalse
triggerstringfalse'@'Character that will trigger mentions
isInsertSpaceAfterMentionbooleanfalsefalseShould we add a space after selected mentions if the mention is at the end of row
inputRefRef\false
mentionTextStyleStyleProp\falseText style for mentions in TextInput
containerStyleStyleProp\false
...textInputPropsTextInputPropsfalseOther text input props

Parsing Mention's value

You can import RegEx that is using in the component and then extract all your mentions from Mention's value using your own logic.

import { mentionRegEx } from 'react-native-controlled-mentions';

Or you can use replaceMentionValues helper to replace all mentions from Mention's input using your replacer function that receives MentionData type and returns string.

import { replaceMentionValues } from 'react-native-controlled-mentions';

const value = 'Hello @[David Tabaka](5)! How are you?';

console.log(replaceMentionValues(value, ({id}) => `@${id}`)); // Hello @5! How are you?
console.log(replaceMentionValues(value, ({name}) => `@${name}`)); // Hello @David Tabaka! How are you?

To Do

  • Add more customizations
  • Add ability to handle few mention types ("#", "@" etc)

Known issues

  • Mention name regex accepts white spaces (eg {name: ' ', value: 1})
  • Keyboard auto-correction not working if suggested word has the same length FIXED
  • Text becomes transparent when setting custom font size in TextInput FIXED
3.0.0-alpha.4

2 years ago

3.0.0-alpha.1

2 years ago

3.0.0-alpha.0

2 years ago

3.0.0-alpha.3

2 years ago

3.0.0-alpha.2

2 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.3.0-0

3 years ago

2.2.4-1

3 years ago

2.2.4-0

3 years ago

2.2.1

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.0

3 years ago

2.2.0-1

3 years ago

2.2.0-0

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

1.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.0-0

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.22

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago