Skip to content

Commit

Permalink
Fix input focusing on selection prop change (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid authored Apr 18, 2024
1 parent cd42c45 commit ad28b14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,13 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
currentlyFocusedField.current = hostNode;
setEventProps(e);
if (divRef.current) {
const valueLength = value ? value.length : 0;
CursorUtils.setCursorPosition(divRef.current, contentSelection.current ? contentSelection.current.end : valueLength);
updateSelection(event);
if (contentSelection.current) {
CursorUtils.setCursorPosition(divRef.current, contentSelection.current.start, contentSelection.current.end);
} else {
const valueLength = value ? value.length : divRef.current.innerText.length;
CursorUtils.setCursorPosition(divRef.current, valueLength, null);
}
updateSelection(event, contentSelection.current);
}

if (onFocus) {
Expand Down
5 changes: 5 additions & 0 deletions src/web/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function setPrevText(target: HTMLElement) {
}

function setCursorPosition(target: HTMLElement, start: number, end: number | null = null) {
// We don't want to move the cursor if the target is not focused
if (target !== document.activeElement) {
return;
}

const range = document.createRange();
range.selectNodeContents(target);

Expand Down

0 comments on commit ad28b14

Please sign in to comment.