Skip to content

Commit

Permalink
moved to top
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrin2005 committed Feb 26, 2025
1 parent fae10e3 commit 332ce75
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/components/Common/SearchByMultipleFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
}
}, [open]);

useEffect(() => {
if (autoFocus) {
inputRef.current?.focus();
}
}, [autoFocus, open, selectedOptionIndex]);

useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
Expand All @@ -154,9 +160,11 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
setSearchValue("");
}
}

if (e.key === "Backspace" && searchValue.length === 0) {
e.preventDefault();
}

if (open) {
if (e.key === "ArrowDown") {
setFocusedIndex((prevIndex) =>
Expand All @@ -179,12 +187,6 @@ const SearchByMultipleFields: React.FC<SearchByMultipleFieldsProps> = ({
return () => document.removeEventListener("keydown", handleKeyDown);
}, [focusedIndex, open, handleOptionChange, options, searchValue]);

useEffect(() => {
if (autoFocus) {
inputRef.current?.focus();
}
}, [autoFocus, open, selectedOptionIndex]);

useEffect(() => {
if (selectedOption.value !== searchValue) {
setSearchValue(selectedOption.value || "");
Expand Down

0 comments on commit 332ce75

Please sign in to comment.