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.
This commit is contained in:
Amit Uttam 2020-05-29 13:29:25 -03:00 committed by GitHub
parent 8089900678
commit d869ddf8e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);