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

[new feature] make textStyle accept a function of part data in trigger config #135

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

Conversation

juandjara
Copy link

@juandjara juandjara commented Jul 24, 2024

This PR adds the possibility of passing a function to textStyle when defining TriggersConfig.

This function will receive the TriggerData for each part and must return a TextStyle object.

This adds the possibility of defining triggers with varying styles depending on the trigger data, for example, changing the color in a piece of text. This can be used with a config like this:

const triggerConfigs: TriggersConfig<'color'> = {
  color: {
    trigger: '#?',
    pattern: /(\[fg=#[0-9a-fA-F]{6}\]\(.*?\))/gi,
    textStyle: (data) => ({
      color: data?.id,
    }),
    // How to parse regex match and get required for data for internal logic
    getTriggerData: (match) => {
      const [first, last] = match.split('](')
      const color = first.replace('[fg=', '')
      const text = last.replace(')', '');
      return ({
        original: match,
        trigger: '#?',
        name: text,
        id: color,
      });
    },

    // How to generate internal mention value from selected suggestion
    getTriggerValue: (suggestion) => `[fg=${suggestion.id}](${suggestion.name})`,

    // How the highlighted mention will appear in TextInput for user
    getPlainString: (triggerData) => triggerData.name,
  }
}

This has been tested on top of the version in the branch 3.0 of the upstream repo.

Documentation in the README has also been updated.

If anyone wants to give it a try, I have published it on NPM: react-native-more-controlled-mentions

@fukemy
Copy link

fukemy commented Oct 22, 2024

thanks you for updating this lib. please keep it up-to-date.

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.

2 participants