import React, { useRef, useEffect, useState } from 'react'; interface ScrollPickerProps { values: string[] | number[]; selectedValue: string | number; onChange: (value: any) => void; label?: string; } export default function ScrollPicker({ values, selectedValue, onChange, label }: ScrollPickerProps) { const scrollRef = useRef(null); const isProgrammaticScroll = useRef(false); // Initialize and sync scroll position when selectedValue changes from outside useEffect(() => { const syncScroll = () => { const index = (values as any[]).indexOf(selectedValue); if (index !== -1 && scrollRef.current) { const itemHeight = 40; isProgrammaticScroll.current = true; scrollRef.current.scrollTop = index * itemHeight; setTimeout(() => { isProgrammaticScroll.current = false; }, 100); } }; // Small delay to ensure DOM is rendered, especially when dropdown first opens const timer = setTimeout(syncScroll, 50); return () => clearTimeout(timer); }, [selectedValue, values]); const handleScroll = () => { if (!scrollRef.current || isProgrammaticScroll.current) return; const itemHeight = 40; const index = Math.round(scrollRef.current.scrollTop / itemHeight); const newValue = values[index]; if (newValue !== undefined && newValue !== selectedValue) { onChange(newValue); } }; return (
{label && {label}}
{/* Selection Highlight */}
{values.map((v, i) => (
{v.toString().padStart(2, '0')}
))}
{/* Decorative Gradients */}
); }