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
25 changes: 20 additions & 5 deletions src/custom/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface SearchBarProps {
expanded: boolean;
setExpanded: (expanded: boolean) => void;
'data-testid'?: string;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}

function SearchBar({
Expand All @@ -84,7 +85,8 @@ 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);
Expand Down Expand Up @@ -130,15 +132,27 @@ function SearchBar({
}
};


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

if (onKeyDown) {
onKeyDown(event);
}


if (event.key === 'Enter') {
onSearch(searchText);
}
};

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 @@ -149,10 +163,11 @@ function SearchBar({
<TextField
variant="standard"
value={searchText}
onChange={handleSearchChange} // Updated to use the new handler
onChange={handleSearchChange}// Updated to use the new handler
inputRef={searchRef}
placeholder={placeholder}
data-testid="searchbar-input"
onKeyDown={handleKeyDown}
style={{
width: expanded ? '150px' : '0',
opacity: expanded ? 1 : 0,
Expand Down
35 changes: 20 additions & 15 deletions src/custom/StyledSearchBar/StyledSearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface SearchBarProps {
sx?: SxProps<Theme>;
endAdornment?: React.ReactNode;
debounceTime?: number;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}

/**
Expand All @@ -38,8 +39,10 @@ function StyledSearchBar({
sx,
placeholder,
endAdornment,
debounceTime = 300
debounceTime = 300,
onKeyDown
}: SearchBarProps): JSX.Element {

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

Expand Down Expand Up @@ -87,20 +90,22 @@ function StyledSearchBar({

return (
<StyledSearchInput
type="search"
label={label}
fullWidth
value={inputValue}
onChange={handleChange}
sx={sx}
placeholder={placeholder ?? 'Search'}
startAdornment={
<InputAdornment position="start">
<SearchIcon fill={theme.palette.background.neutral?.default} />
</InputAdornment>
}
endAdornment={<InputAdornmentEnd position="end">{endAdornment}</InputAdornmentEnd>}
/>
type="search"
label={label}
fullWidth
value={inputValue}
onChange={handleChange}
sx={sx}
placeholder={placeholder ?? 'Search'}
onKeyDown={onKeyDown}
startAdornment={
<InputAdornment position="start">
<SearchIcon fill={theme.palette.background.neutral?.default} />
</InputAdornment>
}
endAdornment={<InputAdornmentEnd position="end">{endAdornment}</InputAdornmentEnd>}
/>

);
}

Expand Down