Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature, expose extra mention data to onChange/onSuggestionPress #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vforvasile
Copy link

@vforvasile vforvasile commented Jun 22, 2023

Hey!

Thanks for creating this project.

We had a use case where we needed a more complex data object to be saved when a mention was pressed but encountered some issues when we tried to implement it only with onSuggestionPress (some events not triggering).

As a solution, I added an optional param extraMentionData to be able to access the data from onChange event.

Added this example in README:

import React from "react";
import { Pressable, Text, View } from "react-native";
import {
  MentionInput,
  MentionSuggestionsProps,
} from "react-native-controlled-mentions";

const randomSuggestions = [
  {
    url: "google.com",
    id: "123",
    name: "Google",
    avatar: "GoogleAvatarUrl",
  },
  {
    url: "bing.com",
    id: "345",
    name: "Bing",
    avatar: "BingAvatarUrl",
  },
];

const MentionList = (props: MentionSuggestionsProps) => {
  const { keyword, onSuggestionPress } = props;

  if (keyword == null) {
    return null;
  }

  const onAddMention = (mention: any) => {
    const { name, id, url, avatar } = mention;

    // preview purpose, you can pass any data to mention
    const extraMentionData = {
      url,
      avatar,
      name,
      id,
    };
    // passes to MentionInput onChange, it's optional
    onSuggestionPress({ id, name }, extraMentionData);
  };

  return (
    <View>
      {randomSuggestions
        .filter((one) =>
          one.name.toLocaleLowerCase().includes(keyword.toLocaleLowerCase())
        )
        .map((suggestion) => (
          <Pressable
            key={suggestion.id}
            onPress={() => onAddMention(suggestion)}
            style={{ padding: 12 }}
          >
            <Text>{suggestion.name}</Text>
            <Text>{suggestion.url}</Text>
            <Text>{suggestion.avatar}</Text>
            <Text>{suggestion.id}</Text>
          </Pressable>
        ))}
    </View>
  );
};

export const ExtraMentionDataSample = () => {
  const [textValue, setTextValue] = React.useState("");
  const [mentions, setMentions] = React.useState([]);

  const onChangeText = (text: string, mentionData?: any) => {
    const newMentions = mentionData ? [...mentions, mentionData] : mentions;
    // save newly selected mention
    setMentions(newMentions);
    setTextValue(text);
  };

  return (
    <MentionInput
      value={textValue}
      onChange={onChangeText}
      partTypes={[
        {
          trigger: "@",
          renderSuggestions: (localProps) => {
            return (
              <MentionList
                onSuggestionPress={localProps.onSuggestionPress}
                keyword={localProps.keyword}
              />
            );
          },
        },
      ]}
    />
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant