From cf3ff99cd829fc96d5d7c2683415635c17e55c1e Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Thu, 18 Jun 2020 03:25:29 +0200 Subject: [PATCH] MM-24029 Fix date re-selection on search bar (#4441) 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. Co-authored-by: Amit Uttam --- .../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);