-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathSearchBar.tsx
More file actions
202 lines (189 loc) · 5.72 KB
/
SearchBar.tsx
File metadata and controls
202 lines (189 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* eslint-disable react-hooks/exhaustive-deps */
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 { ClickAwayListener } from '../base/ClickAwayListener';
import { TextField } from '../base/TextField';
import { CloseIcon, SearchIcon } from '../icons';
import { useTheme } from '../theme';
import { TooltipIcon } from './TooltipIconButton';
const customTheme = (theme: Theme) =>
createTheme({
typography: {
fontFamily: theme.typography.fontFamily
},
components: {
MuiTextField: {
styleOverrides: {
root: {
'--TextField-brandBorderColor': theme.palette.border.strong,
'--TextField-brandBorderHoverColor': theme.palette.background.graphics?.default,
'--TextField-brandBorderFocusedColor': theme.palette.background.graphics?.default,
'& label.Mui-focused': {
color: 'var(--TextField-brandBorderFocusedColor)'
}
}
}
},
MuiOutlinedInput: {
styleOverrides: {
notchedOutline: {
borderColor: 'var(--TextField-brandBorderColor)'
},
root: {
[`&:hover .${outlinedInputClasses.notchedOutline}`]: {
borderColor: 'var(--TextField-brandBorderHoverColor)'
},
[`&.Mui-focused .${outlinedInputClasses.notchedOutline}`]: {
borderColor: 'var(--TextField-brandBorderFocusedColor)'
}
}
}
},
MuiInput: {
styleOverrides: {
root: {
color: theme.palette.text.default,
'&::before': {
borderBottom: '2px solid var(--TextField-brandBorderColor)'
},
'&:hover:not(.Mui-disabled, .Mui-error):before': {
borderBottom: '2px solid var(--TextField-brandBorderHoverColor)'
},
'&.Mui-focused:after': {
borderBottom: '2px solid var(--TextField-brandBorderFocusedColor)'
}
}
}
},
MuiButton: {
styleOverrides: {
root: {
lineHeight: 'auto'
}
}
}
}
});
export interface SearchBarProps {
onSearch: (searchText: string) => void;
style?: React.CSSProperties;
placeholder?: string;
onClear?: () => void;
expanded: boolean;
setExpanded: (expanded: boolean) => void;
'data-testid'?: string;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}
function SearchBar({
onSearch,
placeholder,
onClear,
expanded,
setExpanded,
'data-testid': testId = 'search-bar-wrapper',
onKeyDown
}: SearchBarProps): JSX.Element {
const [searchText, setSearchText] = React.useState('');
const searchRef = React.useRef<HTMLInputElement | null>(null);
const theme = useTheme();
// Debounce the onSearch function for normal typing
const debouncedOnSearch = useCallback(
debounce((value) => {
onSearch(value);
}, 300),
[onSearch]
);
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
const value = event.target.value;
setSearchText(value);
debouncedOnSearch(value);
};
const handleClearIconClick = (e: React.MouseEvent): void => {
e.stopPropagation();
debouncedOnSearch('');
setSearchText('');
setExpanded(false);
if (onClear) {
onClear();
}
};
const handleSearchIconClick = (e: React.MouseEvent): void => {
e.stopPropagation();
if (expanded) {
debouncedOnSearch('');
setSearchText('');
setExpanded(false);
} else {
setExpanded(true);
setTimeout(() => {
if (searchRef.current) {
searchRef.current.focus();
}
}, 300);
}
};
// ✅ New unified keyDown handler
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
// Call external onKeyDown if provided
if (onKeyDown) {
onKeyDown(event);
}
// Trigger onSearch immediately when Enter is pressed
if (event.key === 'Enter') {
onSearch(searchText);
}
};
return (
<ClickAwayListener
onClickAway={(event) => {
event.stopPropagation();
const isTable = (event.target as HTMLElement)?.closest('#ref');
if (searchText !== '') return;
if (isTable) {
handleClearIconClick(event as unknown as React.MouseEvent);
}
}}
>
<div style={{ display: 'flex' }} data-testid={testId}>
<ThemeProvider theme={customTheme(theme)}>
<TextField
variant="standard"
value={searchText}
onChange={handleSearchChange}
inputRef={searchRef}
placeholder={placeholder}
data-testid="searchbar-input"
onKeyDown={handleKeyDown} // <-- updated handler
style={{
width: expanded ? '150px' : '0',
opacity: expanded ? 1 : 0,
transition: 'width 0.3s ease, opacity 0.3s ease'
}}
/>
</ThemeProvider>
{expanded ? (
<TooltipIcon
title="Close"
onClick={handleClearIconClick}
icon={
<CloseIcon fill={theme.palette.icon.default} data-testid="searchbar-clear-button" />
}
arrow
/>
) : (
<TooltipIcon
title="Search"
onClick={handleSearchIconClick}
icon={
<SearchIcon fill={theme.palette.icon.default} data-testid="searchbar-search-button" />
}
arrow
/>
)}
</div>
</ClickAwayListener>
);
}
export default SearchBar;