From d869ddf8e510f7b57aa78bce32827b626f6cd03d Mon Sep 17 00:00:00 2001 From: Amit Uttam Date: Fri, 29 May 2020 13:29:25 -0300 Subject: [PATCH] MM-24029 Fix date re-selection on search bar (#4368) Picking a new date string (tapping a date on the autocomplete calendar) to replace an already populated date string in the search bar, leaves the search text garbled. This was because of incorrect handling of the tap cursor position in the search box. --- .../date_suggestion/date_suggestion.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/components/autocomplete/date_suggestion/date_suggestion.js b/app/components/autocomplete/date_suggestion/date_suggestion.js index ff900811c..65cf0082d 100644 --- a/app/components/autocomplete/date_suggestion/date_suggestion.js +++ b/app/components/autocomplete/date_suggestion/date_suggestion.js @@ -60,13 +60,21 @@ export default class DateSuggestion extends PureComponent { completeMention = (day) => { const mention = day.dateString; const {cursorPosition, onChangeText, value} = this.props; - const mentionPart = value.substring(0, cursorPosition); + + // Substring to first space after current cursor position. + // If there is none, cursor is at the end of the date segment already. + let dateSegmentEnd = value.indexOf(' ', cursorPosition); + if (dateSegmentEnd === -1) { + dateSegmentEnd = cursorPosition; + } + const mentionPart = value.substring(0, dateSegmentEnd); + const flags = mentionPart.match(ALL_SEARCH_FLAGS_REGEX); const currentFlag = flags[flags.length - 1]; let completedDraft = mentionPart.replace(DATE_MENTION_SEARCH_REGEX, `${currentFlag} ${mention} `); - if (value.length > cursorPosition) { - completedDraft += value.substring(cursorPosition); + if (value.length > dateSegmentEnd) { + completedDraft += value.substring(dateSegmentEnd + 1); // accounting for extra space character } onChangeText(completedDraft, true);