diff --git a/package-lock.json b/package-lock.json index f140f18b..51cf5208 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "name": "@layer5/sistent", "version": "0.14.11", "dependencies": { + "lodash": "^4.17.21", "@types/lodash": "^4.17.7" }, "devDependencies": { @@ -17,6 +18,7 @@ "@reduxjs/toolkit": "^2.2.5", "@testing-library/react": "^14.1.2", "@types/jest": "^29.5.11", + "@types/lodash": "^4.17.7", "@types/react": "^18.2.45", "@types/react-dom": "^18.2.18", "@types/react-redux": "^7.1.33", diff --git a/package.json b/package.json index 7ace0c0a..8d1e1786 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@reduxjs/toolkit": "^2.2.5", "@testing-library/react": "^14.1.2", "@types/jest": "^29.5.11", + "@types/lodash": "^4.17.7", "@types/react": "^18.2.45", "@types/react-dom": "^18.2.18", "@types/react-redux": "^7.1.33", @@ -114,5 +115,8 @@ }, "publishConfig": { "access": "public" + }, + "dependencies": { + "lodash": "^4.17.21" } } diff --git a/src/custom/SearchBar.tsx b/src/custom/SearchBar.tsx index 3f68868d..db72eaf4 100644 --- a/src/custom/SearchBar.tsx +++ b/src/custom/SearchBar.tsx @@ -1,6 +1,8 @@ +/* eslint-disable react-hooks/exhaustive-deps */ import { outlinedInputClasses } from '@mui/material/OutlinedInput'; import { Theme, ThemeProvider, createTheme, useTheme } from '@mui/material/styles'; -import React from 'react'; +import debounce from 'lodash/debounce'; +import React, { useCallback } from 'react'; import { ClickAwayListener } from '../base/ClickAwayListener'; import { TextField } from '../base/TextField'; import { CloseIcon, SearchIcon } from '../icons'; @@ -75,11 +77,22 @@ function SearchBar({ const searchRef = React.useRef(null); const theme = useTheme(); + // Debounce the onSearch function + const debouncedOnSearch = useCallback( + debounce((value) => { + onSearch(value); + }, 300), + [onSearch] + ); + const handleSearchChange = (event: React.ChangeEvent): void => { - setSearchText(event.target.value); + const value = event.target.value; + setSearchText(value); + debouncedOnSearch(value); }; const handleClearIconClick = (): void => { + debouncedOnSearch(''); setSearchText(''); setExpanded(false); if (onClear) { @@ -89,6 +102,7 @@ function SearchBar({ const handleSearchIconClick = (): void => { if (expanded) { + debouncedOnSearch(''); setSearchText(''); setExpanded(false); } else { @@ -101,12 +115,6 @@ function SearchBar({ } }; - // const handleClickAway = (): void => { - // if (expanded) { - // setExpanded(false); - // } - // }; - return ( { @@ -125,10 +133,7 @@ function SearchBar({ ) => { - handleSearchChange(e); - onSearch(e.target.value); - }} + onChange={handleSearchChange} // Updated to use the new handler inputRef={searchRef} placeholder={placeholder} style={{