import React, { useState, useRef, useEffect } from 'react'; import { createPortal } from 'react-dom'; import { ChevronDownIcon, ArrowPathIcon, CalendarDaysIcon, ClockIcon, } from '@heroicons/react/24/outline'; import { RecurrenceType } from '../../entities/Task'; import { useTranslation } from 'react-i18next'; interface RecurrenceDropdownProps { value: RecurrenceType; onChange: (value: RecurrenceType) => void; } const RecurrenceDropdown: React.FC = ({ value, onChange, }) => { const { t } = useTranslation(); const recurrenceOptions = [ { value: 'none', label: t('recurrence.none', 'No repeat'), icon: ( ), }, { value: 'daily', label: t('recurrence.daily', 'Daily'), icon: ( ), }, { value: 'weekly', label: t('recurrence.weekly', 'Weekly'), icon: ( ), }, { value: 'monthly', label: t('recurrence.monthly', 'Monthly'), icon: ( ), }, { value: 'monthly_weekday', label: t('recurrence.monthlyWeekday', 'Monthly on weekday'), icon: ( ), }, { value: 'monthly_last_day', label: t('recurrence.monthlyLastDay', 'Monthly on last day'), icon: ( ), }, ]; const [isOpen, setIsOpen] = useState(false); const [position, setPosition] = useState({ top: 0, left: 0, width: 0, openUpward: false, }); const dropdownRef = useRef(null); const menuRef = useRef(null); const handleToggle = () => { if (!isOpen && dropdownRef.current) { const rect = dropdownRef.current.getBoundingClientRect(); const spaceBelow = window.innerHeight - rect.bottom; const spaceAbove = rect.top; const menuHeight = 240; const openUpward = spaceAbove > spaceBelow && spaceBelow < menuHeight; setPosition({ top: openUpward ? rect.top - menuHeight - 8 : rect.bottom + 8, left: rect.left, width: rect.width, openUpward, }); } setIsOpen(!isOpen); }; const handleClickOutside = (event: MouseEvent) => { if ( dropdownRef.current && !dropdownRef.current.contains(event.target as Node) ) { setIsOpen(false); } }; const handleSelect = (recurrence: RecurrenceType) => { onChange(recurrence); setIsOpen(false); }; useEffect(() => { if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } else { document.removeEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isOpen]); const selectedRecurrence = recurrenceOptions.find((r) => r.value === value); return (
{isOpen && createPortal(
{recurrenceOptions.map((recurrence) => ( ))}
, document.body )}
); }; export default RecurrenceDropdown;