-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathStyledSearchBar.tsx
More file actions
112 lines (100 loc) · 3.29 KB
/
StyledSearchBar.tsx
File metadata and controls
112 lines (100 loc) · 3.29 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
import { SxProps, Theme } from '@mui/material';
import { debounce } from 'lodash';
import React, { useEffect, useMemo, useState } from 'react';
import { InputAdornment } from '../../base';
import { SearchIcon } from '../../icons';
import { useTheme } from '../../theme';
import { InputAdornmentEnd, StyledSearchInput } from './style';
interface SearchBarProps {
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
value?: string;
width?: string;
label?: string;
placeholder?: string;
sx?: SxProps<Theme>;
endAdornment?: React.ReactNode;
debounceTime?: number;
onKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
}
/**
* StyledSearchBar component renders a search input field with customizable properties.
*
* @param {Object} props - The component props.
* @param {function} [props.onChange] - Function to handle the change event when the search input value changes.
* @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.
* @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.
*/
function StyledSearchBar({
onChange,
value = '',
label,
sx,
placeholder,
endAdornment,
debounceTime = 300,
onKeyDown
}: SearchBarProps): JSX.Element {
const theme = useTheme();
const [inputValue, setInputValue] = useState(value);
// Update local state when controlled value changes
useEffect(() => {
if (value !== inputValue) {
setInputValue(value);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
// Create synthetic event helper
const createSyntheticEvent = (value: string): React.ChangeEvent<HTMLInputElement> =>
({
target: { value },
persist: () => {}
}) as React.ChangeEvent<HTMLInputElement>;
// Memoize the debounced handler
const debouncedOnChange = useMemo(
() =>
debounce((newValue: string) => {
onChange?.(createSyntheticEvent(newValue));
}, debounceTime),
[onChange, debounceTime]
);
useEffect(() => {
if (!onChange) return;
if (inputValue === '') {
onChange(createSyntheticEvent(inputValue));
} else {
debouncedOnChange(inputValue);
}
return () => {
debouncedOnChange.cancel();
};
}, [inputValue, onChange, debouncedOnChange]);
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const newValue = event.target.value;
setInputValue(newValue);
};
return (
<StyledSearchInput
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>}
/>
);
}
export default StyledSearchBar;