import React from 'react'; import {StyleProp, Text, TextStyle} from 'react-native'; import { isMentionPartType, parseValue, Part, PartType, } from 'react-native-controlled-mentions'; import {TouchableOpacity} from 'react-native-gesture-handler'; import TaggTypeahead from '../components/common/TaggTypeahead'; import {TAGG_LIGHT_BLUE} from '../constants'; import {UserType} from '../types'; import {normalize} from './layouts'; /** * Part renderer * * https://github.com/dabakovich/react-native-controlled-mentions#rendering-mentioninputs-value */ const renderPart = ( part: Part, index: number, handlePress: (user: UserType) => void, ) => { // Just plain text if (!part.partType) { return {part.text}; } // Mention type part if (isMentionPartType(part.partType)) { return ( { if (part.data) { handlePress({ userId: part.data.id, username: part.data.name, }); } }}> {part.text} ); } // Other styled part types return ( {part.text} ); }; interface RenderProps { value: string; styles: StyleProp; partTypes: PartType[]; onPress: (user: UserType) => void; } /** * Value renderer. Parsing value to parts array and then mapping the array using 'renderPart' * * https://github.com/dabakovich/react-native-controlled-mentions#rendering-mentioninputs-value */ export const renderTextWithMentions: React.FC = ({ value, styles, partTypes, onPress, }) => { const {parts} = parseValue(value, partTypes); return ( {parts.map((part, index) => renderPart(part, index, onPress))} ); }; export const mentionPartTypes: ( theme: 'blue' | 'white', component: 'caption' | 'comment', ) => PartType[] = (theme, component) => { return [ { trigger: '@', renderSuggestions: (props) => ( ), allowedSpacesCount: 0, isInsertSpaceAfterMention: true, textStyle: _textStyle(theme), }, ]; }; const _textStyle: (theme: 'blue' | 'white') => StyleProp = ( theme, ) => { switch (theme) { case 'blue': return { color: TAGG_LIGHT_BLUE, top: normalize(3), }; case 'white': default: return { color: 'white', fontWeight: '800', top: normalize(3), }; } };