Skip to content

Commit

Permalink
hotfix: Mouse hover gets ignored if it's not movinga and the user is …
Browse files Browse the repository at this point in the history
…typing (#202)
  • Loading branch information
devcshort authored Oct 18, 2024
1 parent fbfa521 commit 893d22b
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/shared/components/ui/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ export type AutocompleteProps = {
autoSelectIndex?: number;
};

const useMouseMovement = () => {
const [isMoving, setIsMoving] = useState(false);

useEffect(() => {
let timeoutId;

const handleMouseMove = () => {
setIsMoving(true);

clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
setIsMoving(false);
}, 100);
};

window.addEventListener('mousemove', handleMouseMove);
return () => {
window.removeEventListener('mousemove', handleMouseMove);
clearTimeout(timeoutId);
};
}, []);

return isMoving;
};

export function Autocomplete(props: AutocompleteProps) {
const {
inputProps,
Expand All @@ -59,6 +84,7 @@ export function Autocomplete(props: AutocompleteProps) {
value: inputValue,
...rest
} = props;
const isMouseMoving = useMouseMovement();
const [lastManualInput, setLastManualInput] = useState('');
const [uniqueId, setUnqiueId] = useState('');
const [open, setOpen] = useState(false);
Expand Down Expand Up @@ -349,6 +375,7 @@ export function Autocomplete(props: AutocompleteProps) {
const handleOptionMouseEnter = useCallback(
(index: number) => {
return () => {
if (!isMouseMoving) return;
const nextOption = rest.options[index];
const selectionValue = nextOption?.value;
setCurrentIndex(index);
Expand All @@ -357,7 +384,7 @@ export function Autocomplete(props: AutocompleteProps) {
setIsHovering(true);
};
},
[rest.options, setInputSelectionPoint, setValue],
[rest.options, setInputSelectionPoint, setValue, isMouseMoving],
);

const handleOptionMouseExit = useCallback(() => {
Expand Down

0 comments on commit 893d22b

Please sign in to comment.