From 60712775a7fc9edbc70d38d10d8d78ecaccb7821 Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Sat, 6 Jan 2024 16:26:49 +0100 Subject: [PATCH] Place cursor at the end of the replaced selection --- src/components/RoomNameInput/index.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/components/RoomNameInput/index.js b/src/components/RoomNameInput/index.js index 8bfc25a5db75..61f004a47b96 100644 --- a/src/components/RoomNameInput/index.js +++ b/src/components/RoomNameInput/index.js @@ -27,18 +27,15 @@ function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, // Prevent cursor jump behaviour: // Check if newRoomNameWithHash is the same as modifiedRoomName - // If it is then the room name is valid (does not contain unallowed characters); no action required - // If not then the room name contains unvalid characters and we must adjust the cursor position manually + // If it is, then the room name is valid (does not contain forbidden characters) – no action required + // If not, then the room name contains invalid characters, and we must adjust the cursor position manually // Read more: https://github.com/Expensify/App/issues/12741 const oldRoomNameWithHash = value || ''; const newRoomNameWithHash = `${CONST.POLICY.ROOM_PREFIX}${roomName}`; if (modifiedRoomName !== newRoomNameWithHash) { const offset = modifiedRoomName.length - oldRoomNameWithHash.length; - const newSelection = { - start: selection.start + offset, - end: selection.end + offset, - }; - setSelection(newSelection); + const newCursorPosition = selection.end + offset; + setSelection({start: newCursorPosition, end: newCursorPosition}); } };