Skip to content

Commit

Permalink
fix(zbugs): Fix lint warning in combobox (#3413)
Browse files Browse the repository at this point in the history
ESlint was complaining about exhaustive dependencies

Use useClickOutside hook to reduce code duplication.
  • Loading branch information
arv authored Dec 18, 2024
1 parent 26db271 commit ddf3520
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions apps/zbugs/src/components/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {
type KeyboardEvent,
memo,
type RefObject,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import DropdownArrow from '../assets/icons/dropdown-arrow.svg?react';
import {useClickOutside} from '../hooks/use-click-outside.js';
import {umami} from '../umami.js';
import styles from './combobox.module.css';
import {fuzzySearch} from './fuzzySearch.js';
Expand Down Expand Up @@ -49,18 +51,21 @@ function Combobox<T>({

const selectedItem = items.find(option => option.value === value);

const setMenuOpen = (b: boolean) => {
if (b !== isOpen) {
// Only track if state changes
if (b) {
openTimeRef.current = Date.now();
umami.track('Combobox opened'); // Track open action
} else {
umami.track('Combobox closed'); // Track close action
const setMenuOpen = useCallback(
(b: boolean) => {
if (b !== isOpen) {
// Only track if state changes
if (b) {
openTimeRef.current = Date.now();
umami.track('Combobox opened'); // Track open action
} else {
umami.track('Combobox closed'); // Track close action
}
setIsOpen(b);
}
setIsOpen(b);
}
};
},
[isOpen],
);

const toggleDropdown = () => {
setMenuOpen(!isOpen);
Expand All @@ -73,19 +78,12 @@ function Combobox<T>({
}
}, [isOpen]);

useEffect(() => {
const handleMouseDown = (event: MouseEvent) => {
if (
!inputRef.current?.contains(event.target as Element) &&
!listboxRef.current?.contains(event.target as Element)
) {
setMenuOpen(false);
}
};

document.addEventListener('mousedown', handleMouseDown);
return () => document.removeEventListener('mousedown', handleMouseDown);
}, []);
useClickOutside(
[inputRef, listboxRef],
useCallback(() => {
setMenuOpen(false);
}, [setMenuOpen]),
);

const selectIndex = (index: number) => {
setSelectedIndex(index);
Expand Down

0 comments on commit ddf3520

Please sign in to comment.