import React, { useState, useRef, useEffect } from 'react'; import { ChevronDown } from 'lucide-react'; interface CustomSelectProps { value: string; onValueChange: (value: string) => void; placeholder?: string; className?: string; disabled?: boolean; children: React.ReactNode; } interface CustomSelectItemProps { value: string; children: React.ReactNode; className?: string; } interface CustomSelectTriggerProps { children: React.ReactNode; className?: string; disabled?: boolean; } interface CustomSelectContentProps { children: React.ReactNode; className?: string; } const CustomSelectContext = React.createContext<{ isOpen: boolean; setIsOpen: (open: boolean) => void; selectedValue: string; setSelectedValue: (value: string) => void; onValueChange: (value: string) => void; disabled?: boolean; } | null>(null); export const CustomSelect: React.FC = ({ value, onValueChange, placeholder, className = '', disabled = false, children }) => { const [isOpen, setIsOpen] = useState(false); const [selectedValue, setSelectedValue] = useState(value); useEffect(() => { setSelectedValue(value); }, [value]); const handleValueChange = (newValue: string) => { if (disabled) return; setSelectedValue(newValue); onValueChange(newValue); setIsOpen(false); }; return (
{children}
); }; export const CustomSelectTrigger: React.FC = ({ children, className = '', disabled = false }) => { const context = React.useContext(CustomSelectContext); if (!context) { throw new Error('CustomSelectTrigger must be used within CustomSelect'); } const { isOpen, setIsOpen, disabled: contextDisabled } = context; const isDisabled = disabled || contextDisabled; return ( ); }; export const CustomSelectContent: React.FC = ({ children, className = '' }) => { const context = React.useContext(CustomSelectContext); if (!context) { throw new Error('CustomSelectContent must be used within CustomSelect'); } const { isOpen, disabled } = context; if (!isOpen || disabled) return null; return (
{children}
); }; export const CustomSelectItem: React.FC = ({ value, children, className = '' }) => { const context = React.useContext(CustomSelectContext); if (!context) { throw new Error('CustomSelectItem must be used within CustomSelect'); } const { selectedValue, onValueChange, disabled } = context; return (
!disabled && onValueChange(value)} > {children} {selectedValue === value && ( )}
); }; export const CustomSelectValue: React.FC<{ children?: React.ReactNode }> = ({ children }) => { const context = React.useContext(CustomSelectContext); if (!context) { throw new Error('CustomSelectValue must be used within CustomSelect'); } const { selectedValue } = context; return {children || selectedValue}; };