Skip to content

Commit

Permalink
feat: add isInsertSpaceAfterMention property
Browse files Browse the repository at this point in the history
  • Loading branch information
dabakovich committed Dec 17, 2020
1 parent 89db1e9 commit 2ef3f04
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,16 @@ export default App;

The `Mentions` component supports next props:

| Prop name | Type | Required | Default value | Description |
|-------------------|---------------------------------------------------|----------|---------------|-----------------------------------|
| value | string | true | | |
| onChange | function (value) | true | | |
| renderSuggestions | function ({keyword, onSuggestionPress}) ReactNode | false | | |
| trigger | string | false | '@' | |
| inputRef | TextInput | false | | |
| containerStyle | StyleProp\<ViewStyle> | false | | |
| ...textInputProps | TextInputProps | false | | Other text input props |
| Property name | Type | Required | Default value | Description |
|---------------------------|---------------------------------------------------|----------|---------------|------------------------------------------------------------------------------------|
| value | string | true | | |
| onChange | function (value) | true | | |
| renderSuggestions | function ({keyword, onSuggestionPress}) ReactNode | false | | |
| trigger | string | false | '@' | Character that will trigger mentions |
| isInsertSpaceAfterMention | boolean | false | false | Should we add a space after selected mentions if the mention is at the end of row |
| inputRef | TextInput | false | | |
| containerStyle | StyleProp\<ViewStyle> | false | | |
| ...textInputProps | TextInputProps | false | | Other text input props |

### Parsing `Mention`'s value

Expand All @@ -130,6 +131,7 @@ console.log(replaceMentionValues(value, ({name}) => `@${name}`)); // Hello @Davi
```

### To Do

* Add more customizations
* Add ability to handle few mention types ("#", "@" etc)

Expand Down
14 changes: 9 additions & 5 deletions src/components/mentions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const Mentions: FC<MentionsProps> = (

trigger = '@',

isInsertSpaceAfterMention = false,

containerStyle,

inputRef: propInputRef,
Expand Down Expand Up @@ -119,7 +121,7 @@ const Mentions: FC<MentionsProps> = (
partWithAddition,
getPart(value, start),
...newParts.slice(partWithAdditionIndex + 1),
]
];

return;
}
Expand Down Expand Up @@ -210,24 +212,26 @@ const Mentions: FC<MentionsProps> = (
return;
}

const newMentionPart: Position = {
const newMentionPartPosition: Position = {
start: triggerPartIndex,
end: selection.end - currentPart.position.start,
};

const isInsertSpaceToNextPart = !parts[currentPartIndex]?.text.startsWith(' ');
const isInsertSpaceToNextPart = isInsertSpaceAfterMention
// Cursor it at the very of parts or text row
&& (plainText.length === selection.end || parts[currentPartIndex]?.text.startsWith('\n', newMentionPartPosition.end));

const newParts = [
...parts.slice(0, currentPartIndex),

// Create part with string before mention
getPart(currentPart.text.substring(0, newMentionPart.start)),
getPart(currentPart.text.substring(0, newMentionPartPosition.start)),
getMentionPart(trigger, {
...suggestion,
original: getMentionValue(suggestion),
}),
// Create part with rest of string after mention and add a space if needed
getPart(`${isInsertSpaceToNextPart ? ' ' : ''}${currentPart.text.substring(newMentionPart.end)}`),
getPart(`${isInsertSpaceToNextPart ? ' ' : ''}${currentPart.text.substring(newMentionPartPosition.end)}`),

...parts.slice(currentPartIndex + 1),
];
Expand Down
3 changes: 3 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type MentionsProps = Omit<TextInputProps, 'onChange'> & {
// Character that will trigger mentions (usually '@')
trigger?: string;

// Should we add a space after selected mentions if the mention is at the end of row
isInsertSpaceAfterMention?: boolean;

inputRef?: MutableRefObject<TextInput | null>;

containerStyle?: StyleProp<ViewStyle>;
Expand Down

0 comments on commit 2ef3f04

Please sign in to comment.