turash/bugulma/frontend/hooks/ui/useMultiSelect.ts
Damir Mukimov 6347f42e20
Consolidate repositories: Remove nested frontend .git and merge into main repository
- Remove nested git repository from bugulma/frontend/.git
- Add all frontend files to main repository tracking
- Convert from separate frontend/backend repos to unified monorepo
- Preserve all frontend code and development history as tracked files
- Eliminate nested repository complexity for simpler development workflow

This creates a proper monorepo structure with frontend and backend
coexisting in the same repository for easier development and deployment.
2025-11-25 06:02:57 +01:00

60 lines
1.5 KiB
TypeScript

import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
export const useMultiSelect = (
options: string[],
selected: string[],
onChange: (selected: string[]) => void
) => {
const [isOpen, setIsOpen] = useState(false);
const [searchTerm, setSearchTerm] = useState('');
const wrapperRef = useRef<HTMLDivElement>(null);
const toggleOption = useCallback(
(option: string) => {
const newSelected = selected.includes(option)
? selected.filter((item) => item !== option)
: [...selected, option];
onChange(newSelected);
},
[selected, onChange]
);
const filteredOptions = useMemo(
() => options.filter((option) => option.toLowerCase().includes(searchTerm.toLowerCase())),
[options, searchTerm]
);
const handleClickOutside = useCallback((event: MouseEvent) => {
if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
}, []);
useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [handleClickOutside]);
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Escape') {
setIsOpen(false);
}
};
return useMemo(
() => ({
isOpen,
setIsOpen,
searchTerm,
setSearchTerm,
wrapperRef,
toggleOption,
filteredOptions,
handleKeyDown,
}),
[isOpen, searchTerm, toggleOption, filteredOptions]
);
};