Skip to content

Commit 128be80

Browse files
author
Zabilsya
committed
[DOP-21927] rewrite useDebouncedState
1 parent 9fe6120 commit 128be80

File tree

7 files changed

+31
-42
lines changed

7 files changed

+31
-42
lines changed

src/shared/hooks/useDebouncedState/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
1+
import { useCallback, useEffect, useRef, useState } from 'react';
22

3-
/**
4-
* Hook for handling debounced state
5-
*
6-
* @param initialValue - initial state
7-
* @param delay - state change timeout
8-
*/
9-
export function useDebouncedState<T>(initialValue: T, delay: number) {
3+
import { UseDebouncedStateProps } from './types';
4+
5+
/** Hook for handling debounced state */
6+
export function useDebouncedState<T>({ initialValue, delay, onDebounce = () => undefined }: UseDebouncedStateProps<T>) {
107
const [value, setValue] = useState(initialValue);
118
const timeoutRef = useRef<number | null>(null);
129

1310
const clearTimeout = () => window.clearTimeout(timeoutRef.current!);
1411
useEffect(() => clearTimeout, []);
1512

1613
const setDebouncedValue = useCallback(
17-
(newValue: SetStateAction<T>) => {
14+
(newValue: T) => {
1815
clearTimeout();
1916
timeoutRef.current = window.setTimeout(() => {
2017
setValue(newValue);
18+
onDebounce(newValue);
2119
}, delay);
2220
},
23-
[delay],
21+
[delay, onDebounce],
2422
);
2523

2624
const setValueImmediately = (newValue: T) => {
2725
clearTimeout();
2826
setValue(newValue);
27+
onDebounce(newValue);
2928
};
3029

3130
return { value, setValue: setValueImmediately, setDebouncedValue };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Interface as Props type of "useDebouncedState" hook.
3+
*
4+
* @template T - Value type.
5+
*/
6+
export interface UseDebouncedStateProps<T> {
7+
/** Initial value */
8+
initialValue: T;
9+
/** Debounce timeout */
10+
delay: number;
11+
/** Callback for handling debounced state change */
12+
onDebounce?: (value: T) => void;
13+
}

src/shared/ui/ManagedSelect/ManagedSelect.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React, { useState } from 'react';
22
import { Select, Spin } from 'antd';
33
import { DefaultOptionType } from 'antd/lib/select';
4+
import { useDebouncedState } from '@shared/hooks';
45

56
import { ManagedSelectProps } from './types';
6-
import { useGetList, useGetSelectedItem, useHandleSelectEvents, usePrepareOptions, useSearch } from './hooks';
7+
import { useGetList, useGetSelectedItem, useHandleSelectEvents, usePrepareOptions } from './hooks';
8+
import { SEARCH_VALUE_CHANGE_DELAY, SEARCH_VALUE_DEFAULT } from './constants';
79

810
/** Select component for infinite pagination of data in a dropdown */
911
export const ManagedSelect = <T, V extends DefaultOptionType['value']>({
@@ -21,7 +23,11 @@ export const ManagedSelect = <T, V extends DefaultOptionType['value']>({
2123
}: ManagedSelectProps<T, V>) => {
2224
const [hasTouched, setTouched] = useState(false);
2325

24-
const { searchValue, setSearchValue, handleSearch } = useSearch({ onSearch });
26+
const {
27+
value: searchValue,
28+
setValue: setSearchValue,
29+
setDebouncedValue: handleDebouncedSearchValue,
30+
} = useDebouncedState({ initialValue: SEARCH_VALUE_DEFAULT, delay: SEARCH_VALUE_CHANGE_DELAY, onDebounce: onSearch });
2531

2632
const { data, hasNextPage, fetchNextPage, isLoading, isFetching } = useGetList({
2733
queryKey,
@@ -62,7 +68,7 @@ export const ManagedSelect = <T, V extends DefaultOptionType['value']>({
6268
showSearch
6369
onDropdownVisibleChange={handleOpenDropdown}
6470
onSelect={handleSelect}
65-
onSearch={handleSearch}
71+
onSearch={handleDebouncedSearchValue}
6672
filterOption={false}
6773
// render notFoundContent when first request data is in progress
6874
options={isLoading ? [] : options}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './useGetList';
22
export * from './useGetSelectedItem';
3-
export * from './useSearch';
43
export * from './useHandleSelectEvents';
54
export * from './usePrepareOptions';

src/shared/ui/ManagedSelect/hooks/useSearch/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/shared/ui/ManagedSelect/hooks/useSearch/types.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/shared/ui/ManagedSelect/hooks/useSearch/useSearch.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)