Skip to content

Enhance SearchAutocompleteInput with the debounced search #59659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 15, 2025
Merged
19 changes: 16 additions & 3 deletions src/components/Search/SearchAutocompleteInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable rulesdir/no-acc-spread-in-reduce */
import lodashDebounce from 'lodash/debounce';
import type {ForwardedRef, ReactNode, RefObject} from 'react';
import React, {forwardRef, useCallback, useEffect, useLayoutEffect, useMemo} from 'react';
import React, {forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import type {StyleProp, TextInputProps, ViewStyle} from 'react-native';
import {useOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -134,6 +135,18 @@ function SearchAutocompleteInput(
return focusedSharedValue.get() ? wrapperFocusedStyle : wrapperStyle ?? {};
});

const [localValue, setLocalValue] = useState(value);

const debouncedOnSearchQueryChange = useMemo(() => lodashDebounce(onSearchQueryChange, CONST.TIMING.USE_DEBOUNCED_STATE_DELAY), [onSearchQueryChange]);

const handleChangeText = useCallback(
(text: string) => {
setLocalValue(text);
debouncedOnSearchQueryChange(text);
},
[debouncedOnSearchQueryChange],
);

useEffect(() => {
runOnLiveMarkdownRuntime(() => {
'worklet';
Expand Down Expand Up @@ -193,8 +206,8 @@ function SearchAutocompleteInput(
>
<TextInput
testID="search-autocomplete-text-input"
value={value}
onChangeText={onSearchQueryChange}
value={localValue}
onChangeText={handleChangeText}
autoFocus={autoFocus}
shouldDelayFocus={shouldDelayFocus}
caretHidden={caretHidden}
Expand Down