🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

react-native-controlled-mentions

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-controlled-mentions

Fully controlled React Native mentions component

0.1.0
Source
npm
Version published
Weekly downloads
14K
2.26%
Maintainers
1
Weekly downloads
 
Created
Source

React Native Controlled Mentions

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 React, { FC, useState } from 'react';
import { Mentions, Suggestion } from 'react-native-controlled-mentions';
import { Pressable, Text, View } from 'react-native';

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

type MentionSuggestionsProps = {
  keyword?: string;
  suggestions: Suggestion[];
  onSuggestionPress: (suggestion: Suggestion) => void;
};

const MentionSuggestions: FC<MentionSuggestionsProps> = (
  {
    keyword,
    suggestions,
    onSuggestionPress,
  },
) => keyword != null ? (
  <View>
    {suggestions
      .filter(item => item.name.toLowerCase().includes(keyword.toLowerCase()))
      .map(suggestion => (
        <Pressable
          key={suggestion.id}
          style={{padding: 8}}
          onPress={() => onSuggestionPress(suggestion)}
        >
          <Text>{suggestion.name}</Text>
        </Pressable>
      ))
    }
  </View>
) : null;

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

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

      renderSuggestions={({keyword, onSuggestionPress}) => (
        <MentionSuggestions
          keyword={keyword}
          suggestions={suggestions}
          onSuggestionPress={onSuggestionPress}
        />
      )}
    />
  );
};

export { App };

Configuration

The Mentions component supports next props:

Prop nameTypeRequiredDefault valueDescription
valuestringtrue
onChangefunction (value)true
renderSuggestionsfunction ({keyword, onSuggestionPress}) ReactNodefalse
triggerstringfalse'@'
inputRefTextInputfalse
containerStyleStylePropfalse

Known issues

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

Keywords

react

FAQs

Package last updated on 09 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts