Skip to content

Commit 04c6cbd

Browse files
authored
Merge pull request #21 from pac-guerreiro/fix/pac-guerreiro/web-textinput-cursor-jumps-to-start-on-type-change
fix(web)[TextInput]: cursor jumps to the start when secureTextEntry i…
2 parents 1fe5266 + 1659c4d commit 04c6cbd

File tree

1 file changed

+22
-10
lines changed
  • packages/react-native-web/src/exports/TextInput

1 file changed

+22
-10
lines changed

packages/react-native-web/src/exports/TextInput/index.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ const TextInput: React.AbstractComponent<
192192

193193
const dimensions = React.useRef({ height: null, width: null });
194194
const hostRef = React.useRef(null);
195+
const prevSelection = React.useRef(null);
196+
const prevSecureTextEntry = React.useRef(false);
197+
198+
React.useEffect(() => {
199+
if (hostRef.current && prevSelection.current) {
200+
setSelection(hostRef.current, prevSelection.current);
201+
}
202+
prevSecureTextEntry.current = secureTextEntry;
203+
}, [secureTextEntry]);
195204

196205
const handleContentSizeChange = React.useCallback(
197206
(hostNode) => {
@@ -323,18 +332,21 @@ const TextInput: React.AbstractComponent<
323332
}
324333

325334
function handleSelectionChange(e) {
326-
if (onSelectionChange) {
327-
try {
328-
const node = e.target;
329-
const { selectionStart, selectionEnd } = node;
330-
e.nativeEvent.selection = {
331-
start: selectionStart,
332-
end: selectionEnd
333-
};
335+
try {
336+
const { selectionStart, selectionEnd } = e.target;
337+
const selection = {
338+
start: selectionStart,
339+
end: selectionEnd
340+
};
341+
if (onSelectionChange) {
342+
e.nativeEvent.selection = selection;
334343
e.nativeEvent.text = e.target.value;
335344
onSelectionChange(e);
336-
} catch (e) {}
337-
}
345+
}
346+
if (prevSecureTextEntry.current === secureTextEntry) {
347+
prevSelection.current = selection;
348+
}
349+
} catch (e) {}
338350
}
339351

340352
useLayoutEffect(() => {

0 commit comments

Comments
 (0)