Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/custom/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { outlinedInputClasses } from '@mui/material/OutlinedInput';
import { Theme, ThemeProvider, createTheme } from '@mui/material/styles';
import debounce from 'lodash/debounce';
import React, { useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import { ClickAwayListener } from '../base/ClickAwayListener';
import { TextField } from '../base/TextField';
import { CloseIcon, SearchIcon } from '../icons';
Expand Down Expand Up @@ -75,6 +75,7 @@ export interface SearchBarProps {
expanded: boolean;
setExpanded: (expanded: boolean) => void;
'data-testid'?: string;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}

function SearchBar({
Expand All @@ -83,10 +84,12 @@ function SearchBar({
onClear,
expanded,
setExpanded,
'data-testid': testId = 'search-bar-wrapper'
'data-testid': testId = 'search-bar-wrapper',
onKeyDown
}: SearchBarProps): JSX.Element {
const [searchText, setSearchText] = React.useState('');
const searchRef = React.useRef<HTMLInputElement | null>(null);
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const theme = useTheme();

// Debounce the onSearch function
Expand Down Expand Up @@ -129,15 +132,30 @@ function SearchBar({
}
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
onKeyDown?.(event);

if (event.defaultPrevented) {
return;
}

if (event.key === 'Enter') {
if (searchTimeoutRef.current) {
clearTimeout(searchTimeoutRef.current);
searchTimeoutRef.current = null;
}
onSearch(searchText);
}
Comment on lines +142 to +148
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

};

return (
<ClickAwayListener
onClickAway={(event) => {
event.stopPropagation();
const isTable = (event.target as HTMLElement)?.closest('#ref');

if (searchText !== '') {
return;
}
if (searchText !== '') return;

if (isTable) {
handleClearIconClick(event as unknown as React.MouseEvent);
}
Expand All @@ -148,10 +166,11 @@ function SearchBar({
<TextField
variant="standard"
value={searchText}
onChange={handleSearchChange} // Updated to use the new handler
onChange={handleSearchChange}
inputRef={searchRef}
placeholder={placeholder}
data-testid="searchbar-input"
onKeyDown={handleKeyDown}
style={{
width: expanded ? '150px' : '0',
opacity: expanded ? 1 : 0,
Expand Down
5 changes: 5 additions & 0 deletions src/custom/StyledSearchBar/StyledSearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { InputAdornmentEnd, StyledSearchInput } from './style';

interface SearchBarProps {
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
value?: string;
width?: string;
label?: string;
Expand All @@ -29,6 +30,7 @@ interface SearchBarProps {
*
* @param {Object} props - The component props.
* @param {function} [props.onChange] - Function to handle the change event when the search input value changes.
* @param {function} [props.onKeyDown] - Function to handle keyboard events on the search input.
* @param {string} [props.value] - The current value of the search input.
* @param {string} [props.label] - The label for the search input.
* @param {string} [props.placeholder] - The placeholder text for the search input.
Expand All @@ -41,6 +43,7 @@ interface SearchBarProps {
*/
function StyledSearchBar({
onChange,
onKeyDown,
value = '',
label,
sx,
Expand All @@ -49,6 +52,7 @@ function StyledSearchBar({
debounceTime = 300,
fullWidth = true
}: SearchBarProps): JSX.Element {

const theme = useTheme();
const [inputValue, setInputValue] = useState(value);

Expand Down Expand Up @@ -102,6 +106,7 @@ function StyledSearchBar({
onChange={handleChange}
sx={sx}
placeholder={placeholder ?? 'Search'}
onKeyDown={onKeyDown}
startAdornment={
<InputAdornment position="start">
<SearchIcon fill={theme.palette.background.neutral?.default} />
Expand Down
Loading