Skip to content

Commit

Permalink
fix: debouncing
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Amrutiya <[email protected]>
  • Loading branch information
amitamrutiya committed Oct 18, 2024
1 parent bf8f47b commit c0de7c9
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/custom/StyledSearchBar/StyledSearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SxProps, Theme } from '@mui/material';
import { debounce } from 'lodash';
import React, { useCallback, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { InputAdornment } from '../../base';
import { SearchIcon } from '../../icons';
import { useTheme } from '../../theme';
Expand All @@ -14,6 +14,7 @@ interface SearchBarProps {
placeholder?: string;
sx?: SxProps<Theme>;
endAdornment?: React.ReactNode;
debounceTime?: number;
}

/**
Expand All @@ -26,6 +27,7 @@ interface SearchBarProps {
* @param {string} [props.placeholder] - The placeholder text for the search input.
* @param {Object} [props.sx] - The style object for the search input.
* @param {React.ReactNode} [props.endAdornment] - The element to display at the end of the search input.
* @param {number} [props.debounceTime] - The debounce time for the input change handler.
*
* @returns {JSX.Element} The rendered StyledSearchBar component.
*/
Expand All @@ -35,25 +37,39 @@ function StyledSearchBar({
label,
sx,
placeholder,
endAdornment
endAdornment,
debounceTime = 300
}: SearchBarProps): JSX.Element {
const theme = useTheme();
const [inputValue, setInputValue] = useState(value);
const [inputValue, setInputValue] = useState(value ?? '');

const debouncedOnChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
debounce(() => {
if (onChange) {
onChange(event);
}
}, 300)();
},
[onChange]
);
useEffect(() => {
if (value !== undefined && value !== inputValue) {
setInputValue(value);
}
}, [value]);

Check warning on line 50 in src/custom/StyledSearchBar/StyledSearchBar.tsx

View workflow job for this annotation

GitHub Actions / lint (16)

React Hook useEffect has a missing dependency: 'inputValue'. Either include it or remove the dependency array

Check warning on line 50 in src/custom/StyledSearchBar/StyledSearchBar.tsx

View workflow job for this annotation

GitHub Actions / lint (18)

React Hook useEffect has a missing dependency: 'inputValue'. Either include it or remove the dependency array

Check warning on line 50 in src/custom/StyledSearchBar/StyledSearchBar.tsx

View workflow job for this annotation

GitHub Actions / lint (20)

React Hook useEffect has a missing dependency: 'inputValue'. Either include it or remove the dependency array

useEffect(() => {
const handler = debounce((newValue: string) => {
if (onChange) {
const syntheticEvent = {
target: { value: newValue },
persist: () => {}
} as React.ChangeEvent<HTMLInputElement>;

onChange(syntheticEvent);
}
}, debounceTime);

handler(inputValue);

return () => {
handler.cancel();
};
}, [inputValue, onChange, debounceTime]);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);
debouncedOnChange(event);
};

return (
Expand Down

0 comments on commit c0de7c9

Please sign in to comment.