Skip to content

Commit e106567

Browse files
committed
fix: auto complete
1 parent a1650f0 commit e106567

2 files changed

Lines changed: 52 additions & 15 deletions

File tree

src/components/Search/SearchAutocompleteList.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ function SearchRouterItem(props: UserListItemProps<OptionData> | SearchQueryList
143143
);
144144
}
145145

146+
const ContinuationDetectionSearchFilterKey = [
147+
CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
148+
CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
149+
CONST.SEARCH.SYNTAX_FILTER_KEYS.ASSIGNEE,
150+
CONST.SEARCH.SYNTAX_FILTER_KEYS.PAYER,
151+
CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPORTER,
152+
] as SearchFilterKey[];
153+
146154
function SearchAutocompleteList(
147155
{
148156
autocompleteQueryValue,
@@ -260,15 +268,8 @@ function SearchAutocompleteList(
260268

261269
if (!autocomplete && ranges.length > 0) {
262270
const lastRange = ranges.at(ranges.length - 1);
263-
const nameFields = [
264-
CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
265-
CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
266-
CONST.SEARCH.SYNTAX_FILTER_KEYS.ASSIGNEE,
267-
CONST.SEARCH.SYNTAX_FILTER_KEYS.PAYER,
268-
CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPORTER,
269-
] as SearchFilterKey[];
270-
271-
if (lastRange && nameFields.includes(lastRange.key)) {
271+
272+
if (lastRange && ContinuationDetectionSearchFilterKey.includes(lastRange.key)) {
272273
const afterLastRange = autocompleteQueryValue.substring(lastRange.start + lastRange.length);
273274
const continuationMatch = afterLastRange.match(/^\s+(\w+)/);
274275

@@ -675,7 +676,23 @@ function SearchAutocompleteList(
675676
return;
676677
}
677678

678-
const trimmedUserSearchQuery = getQueryWithoutAutocompletedPart(autocompleteQueryValue);
679+
const fieldKey = focusedItem.mapKey?.includes(':') ? focusedItem.mapKey.split(':').at(0) : focusedItem.mapKey;
680+
const isNameField = fieldKey && ContinuationDetectionSearchFilterKey.includes(fieldKey as SearchFilterKey);
681+
682+
let trimmedUserSearchQuery;
683+
if (isNameField && fieldKey) {
684+
const fieldPattern = `${fieldKey}:`;
685+
const keyIndex = autocompleteQueryValue.toLowerCase().lastIndexOf(fieldPattern.toLowerCase());
686+
687+
if (keyIndex !== -1) {
688+
trimmedUserSearchQuery = autocompleteQueryValue.substring(0, keyIndex + fieldPattern.length);
689+
} else {
690+
trimmedUserSearchQuery = getQueryWithoutAutocompletedPart(autocompleteQueryValue);
691+
}
692+
} else {
693+
trimmedUserSearchQuery = getQueryWithoutAutocompletedPart(autocompleteQueryValue);
694+
}
695+
679696
setTextQuery(`${trimmedUserSearchQuery}${sanitizeSearchValue(focusedItem.searchQuery)}\u00A0`);
680697
updateAutocompleteSubstitutions(focusedItem);
681698
},

src/components/Search/SearchRouter/SearchRouter.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {AnimatedTextInputRef} from '@components/RNTextInput';
1111
import type {GetAdditionalSectionsCallback} from '@components/Search/SearchAutocompleteList';
1212
import SearchAutocompleteList from '@components/Search/SearchAutocompleteList';
1313
import SearchInputSelectionWrapper from '@components/Search/SearchInputSelectionWrapper';
14-
import type {SearchQueryString} from '@components/Search/types';
14+
import type {SearchFilterKey, SearchQueryString} from '@components/Search/types';
1515
import type {SearchQueryItem} from '@components/SelectionList/Search/SearchQueryListItem';
1616
import {isSearchQueryItem} from '@components/SelectionList/Search/SearchQueryListItem';
1717
import type {SelectionListHandle} from '@components/SelectionList/types';
@@ -350,10 +350,30 @@ function SearchRouter({onRouterClose, shouldHideInputCaret, isSearchRouterDispla
350350
});
351351
} else if (item.searchItemType === CONST.SEARCH.SEARCH_ROUTER_ITEM_TYPE.AUTOCOMPLETE_SUGGESTION && textInputValue) {
352352
const fieldKey = item.mapKey?.includes(':') ? item.mapKey.split(':').at(0) : item.mapKey;
353-
const keyIndex = fieldKey ? textInputValue.toLowerCase().lastIndexOf(`${fieldKey}:`) : -1;
354-
355-
const trimmedUserSearchQuery =
356-
keyIndex !== -1 && fieldKey ? textInputValue.substring(0, keyIndex + fieldKey.length + 1) : getQueryWithoutAutocompletedPart(textInputValue);
353+
const nameFields = [
354+
CONST.SEARCH.SYNTAX_FILTER_KEYS.TO,
355+
CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,
356+
CONST.SEARCH.SYNTAX_FILTER_KEYS.ASSIGNEE,
357+
CONST.SEARCH.SYNTAX_FILTER_KEYS.PAYER,
358+
CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPORTER,
359+
] as SearchFilterKey[];
360+
const isNameField = fieldKey && nameFields.includes(fieldKey as SearchFilterKey);
361+
362+
let trimmedUserSearchQuery;
363+
if (isNameField && fieldKey) {
364+
const fieldPattern = `${fieldKey}:`;
365+
const keyIndex = textInputValue.toLowerCase().lastIndexOf(fieldPattern.toLowerCase());
366+
367+
if (keyIndex !== -1) {
368+
trimmedUserSearchQuery = textInputValue.substring(0, keyIndex + fieldPattern.length);
369+
} else {
370+
trimmedUserSearchQuery = getQueryWithoutAutocompletedPart(textInputValue);
371+
}
372+
} else {
373+
const keyIndex = fieldKey ? textInputValue.toLowerCase().lastIndexOf(`${fieldKey}:`) : -1;
374+
trimmedUserSearchQuery =
375+
keyIndex !== -1 && fieldKey ? textInputValue.substring(0, keyIndex + fieldKey.length + 1) : getQueryWithoutAutocompletedPart(textInputValue);
376+
}
357377

358378
const newSearchQuery = `${trimmedUserSearchQuery}${sanitizeSearchValue(item.searchQuery)}\u00A0`;
359379
onSearchQueryChange(newSearchQuery, true);

0 commit comments

Comments
 (0)