Skip to content

Commit cfe7be3

Browse files
committed
fix: rewrite onTextChanged to onSelectionChanged event handler in KeyboardAwareScrollView
1 parent a265bf0 commit cfe7be3

10 files changed

+22
-19
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

src/components/KeyboardAwareScrollView/index.tsx

+22-19
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const KeyboardAwareScrollView = forwardRef<
189189
);
190190

191191
const scrollFromCurrentPosition = useCallback(
192-
(customHeight?: number) => {
192+
(customHeight: number) => {
193193
"worklet";
194194

195195
const prevScrollPosition = scrollPosition.value;
@@ -204,7 +204,7 @@ const KeyboardAwareScrollView = forwardRef<
204204
...input.value,
205205
layout: {
206206
...input.value.layout,
207-
height: customHeight ?? input.value.layout.height,
207+
height: customHeight ?? input.value.layout.height, // TODO: math.min? When we have multiline input with limited amount of lines, then custom height can be very big?
208208
},
209209
};
210210
scrollPosition.value = position.value;
@@ -214,39 +214,42 @@ const KeyboardAwareScrollView = forwardRef<
214214
},
215215
[maybeScroll],
216216
);
217-
const onChangeText = useCallback(() => {
218-
"worklet";
217+
const onChangeText = useCallback(
218+
(customHeight: number) => {
219+
"worklet";
219220

220-
// if typing a text caused layout shift, then we need to ignore this handler
221-
// because this event will be handled in `useAnimatedReaction` below
222-
if (layout.value?.layout.height !== input.value?.layout.height) {
223-
return;
224-
}
221+
// if typing a text caused layout shift, then we need to ignore this handler
222+
// because this event will be handled in `useAnimatedReaction` below
223+
if (layout.value?.layout.height !== input.value?.layout.height) {
224+
return;
225+
}
225226

226-
scrollFromCurrentPosition();
227-
}, [scrollFromCurrentPosition]);
227+
scrollFromCurrentPosition(customHeight);
228+
},
229+
[scrollFromCurrentPosition],
230+
);
231+
const onChangeTextHandler = useMemo(
232+
() => debounce(onChangeText, 200),
233+
[onChangeText],
234+
);
228235
const onSelectionChange = useCallback(
229236
(e: FocusedInputSelectionChangedEvent) => {
230237
"worklet";
231238

232239
if (e.selection.start.position !== e.selection.end.position) {
233240
scrollFromCurrentPosition(e.selection.end.y);
234241
}
235-
},
236-
[scrollFromCurrentPosition],
237-
);
238242

239-
const onChangeTextHandler = useMemo(
240-
() => debounce(onChangeText, 200),
241-
[onChangeText],
243+
onChangeTextHandler(e.selection.end.y);
244+
},
245+
[scrollFromCurrentPosition, onChangeTextHandler],
242246
);
243247

244248
useFocusedInputHandler(
245249
{
246-
onChangeText: onChangeTextHandler,
247250
onSelectionChange: onSelectionChange,
248251
},
249-
[onChangeTextHandler, onSelectionChange],
252+
[onSelectionChange],
250253
);
251254

252255
useSmoothKeyboardHandler(

0 commit comments

Comments
 (0)