Skip to content

Commit 9d8f360

Browse files
author
Tom Hutman
committed
fix autocomplete suggestion highlighting for cases where the trigger is not the first character in the input value
1 parent 630a21d commit 9d8f360

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/components/AutoCompleteTextarea/List.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ const List = (props) => {
1010
const {
1111
className,
1212
component,
13+
currentTrigger,
1314
dropdownScroll,
1415
getSelectedItem,
1516
getTextToReplace,
1617
itemClassName,
1718
itemStyle,
1819
onSelect,
1920
style,
21+
selectionEnd,
2022
SuggestionItem = Item,
2123
value: propValue,
2224
values,
@@ -123,7 +125,10 @@ const List = (props) => {
123125
const restructureItem = (item) => {
124126
const matched = item.name || item.id;
125127

126-
const editedPropValue = escapeRegExp(propValue.slice(1));
128+
const textBeforeCursor = propValue.slice(0, selectionEnd);
129+
const triggerIndex = textBeforeCursor.lastIndexOf(currentTrigger);
130+
const editedPropValue = escapeRegExp(textBeforeCursor.slice(triggerIndex + 1));
131+
127132
const parts = matched.split(new RegExp(`(${editedPropValue})`, 'gi'));
128133

129134
const itemNameParts = { match: editedPropValue, parts };

src/components/AutoCompleteTextarea/Textarea.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,14 @@ class ReactTextareaAutocomplete extends React.Component {
635635
<SuggestionList
636636
className={listClassName}
637637
component={component}
638+
currentTrigger={currentTrigger}
638639
dropdownScroll={this._dropdownScroll}
639640
getSelectedItem={selectedItem}
640641
getTextToReplace={textToReplace}
641642
itemClassName={itemClassName}
642643
itemStyle={itemStyle}
643644
onSelect={this._onSelect}
645+
selectionEnd={this.state.selectionEnd}
644646
SuggestionItem={SuggestionItem}
645647
value={value}
646648
values={suggestionData}

0 commit comments

Comments
 (0)