How farishloy/react-native-controlled-mentions works
Project Type and Purpose
• This is a React Native component library (“react-native-controlled-mentions”) that provides a fully controlled mention input for RN’s TextInput.
• It is not a full-stack app but an open-source UI toolkit with parsing utilities (mentions, hashtags, custom patterns) and an example/demo app.
File Structure Analysis
• Top level
• package.json, tsconfig.json, README.md, LICENSE, CHANGELOG.md
• example/mentions-app.tsx – a standalone RN example demonstrating usage
• src/ – core implementation
• tests/ – unit tests for utilities
• src/
• components/ – React components (index.ts exports, mention-input.tsx is the heart of the library)
• utils/ – pure functions (e.g. recursive parser, replaceMentionValues, mentionRegEx)
• types/ – TypeScript type definitions for PartType, Suggestion, MentionData, API props
• index.ts – library entrypoint (exports components, utils, types)
• tests/
• utils.test.ts – validates parsing utilities
• No backend or external services; publishing to npm is the only “deployment.”
Key Architectural Patterns
• Separation of concerns: UI components (in components/) are completely decoupled from parsing logic (in utils/) and from type definitions (in types/).
• Fully controlled component pattern: passing value/onChange from parent, leveraging React hooks inside MentionInput.
• Plugin-style partTypes array: extensibility point for multiple mention or pattern types.
Main Components
• Consumer App (example or any RN app)
• Renders MentionInput, provides value and onChange, supplies partTypes
• MentionInput (UI Layer)
• Renders RN TextInput under the hood
• Accepts props: value, onChange, partTypes, styling, etc.
• On each keystroke invokes parsing utilities to detect triggers and maintain cursor position
• Calls renderSuggestions callback when needed
• Part Types & Suggestions Renderer (Extension Points)
• MentionPartType: defines trigger (@, #), style, renderSuggestions callback
• PatternPartType: defines regex for custom patterns (URLs, hashtags)
• Parsing Utilities (Business Logic Layer)
• mentionRegEx – core regex for extracting mentions
• replaceMentionValues – transforms mention syntax to plain text or other format
• parseValue – splits a string into parts array of text vs mention vs pattern
• Types Module
• Central definitions for Suggestion, MentionData, PartType, MentionSuggestionsProps
• Tests
• Unit tests for utils; ensure parsing correctness
• Example App
• Illustrates integration of all above; consumes library via local import (or npm)
Relationships and Data Flow
• Consumer App → (value,onChange,partTypes) → MentionInput
• MentionInput → Parsing Utilities → parts & suggestions
• MentionInput → renderSuggestions callback → Suggestions UI
• User Interaction (typing, selecting suggestion) → onSuggestionPress → MentionInput → onChange → Consumer App
• Consumer App may call utils (replaceMentionValues or parseValue) to process final string for storage or rendering