JSPM

  • Created
  • Published
  • Downloads 64
  • Score
    100M100P100Q62425F
  • License MIT

Coming Soon

Package Exports

  • react-native-headless-mention

Readme

react-native-headless-mention

A headless mention component for React Native.

GitHub codecov npm

Description

A headless mention component for React Native. It's a headless component, so you'll need to provide your styles and suggestions renderer.

Features

  • Written In Typescript
  • Offers CJS, and ESM builds
  • Full TypeScript & JavaScript support

Install

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm i react-native-headless-mention

Usage

import { useState, useRef, useEffect } from 'react';
import { Pressable, Text, View } from 'react-native';
import { Input, type MentionSuggestionsProps } from 'react-native-headless-mention';

const suggestions = [
    { id: '1', name: 'Parbez' },
    { id: '2', name: 'Voxelli' },
    { id: '3', name: 'Sho' },
    { id: '4', name: 'Hound' },
    { id: '5', name: 'Sarcaster' },
];

const renderSuggestions = ({ keyword, onSuggestionPress }: MentionSuggestionsProps) => {
    if (!keyword) return null;

    return (
        <View>
            {suggestions
                .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>
    );
};

export default function Campaigns() {
    const [value, setValue] = useState('');
    const inputRef = useRef(null);

    useEffect(() => {
        console.log(value);
    }, [value]);

    return (
        <Input
            inputRef={inputRef}
            onChange={setValue}
            partTypes={[
                {
                    trigger: '@',
                    renderSuggestions,
                    textStyle: { fontWeight: 'bold', color: 'blue' },
                    getLabel(mention) {
                        const { name } = suggestions.find((one) => one.id === mention.id) ?? { name: mention.id };
                        return `@${name}`;
                    },
                    pattern: /<(?<trigger>@)(?<id>\d+)>/g,
                },
            ]}
            value={value}
        />
    );
}

[!Important] The pattern must be a global regex. If it's a mention regex then don't forget to add the group name trigger and id in the regex.

Some contents goes here

Buy me some doughnuts

If you want to support me by donating, you can do so by using any of the following methods. Thank you very much in advance!

Buy Me A Coffee Buy Me A Coffee Buy Me a Coffee at ko-fi.com

Contributors ✨

Thanks goes to these wonderful people: