import React, { useCallback, useEffect, useRef } from "react"; import { Select, SelectItem } from "../../Select"; import { MultiSelect, MultiSelectItem } from "../../MultiSelect"; import { Chip } from "../../Chip"; export interface VirtualTableSelectOption { value: string | number; label: string; color?: string; } export function VirtualTableSelect(props: { options: VirtualTableSelectOption[]; error?: Error; multiple?: boolean; disabled: boolean; small?: boolean; value: string | number | string[] | number[] | undefined; updateValue: (newValue: (string | number | string[] | number[] | null)) => void; focused: boolean; onBlur?: React.FocusEventHandler; }) { const { options, value, disabled, small = false, focused, updateValue, multiple = false } = props; const validValue = (Array.isArray(value) && multiple) || (!Array.isArray(value) && !multiple); const ref = useRef(null); useEffect(() => { if (ref.current && focused) { ref.current?.focus({ preventScroll: true }); } }, [focused, ref]); const onChange = useCallback((updatedValue: string | string[]) => { if (multiple) { const isNumber = options.some(o => typeof o.value === "number"); const newValue = (updatedValue as string[]).map((v) => { const opt = options.find(o => String(o.value) === v); return typeof opt?.value === "number" ? parseFloat(v) : v; }); if (isNumber) { updateValue(newValue as number[]); } else { updateValue(newValue as string[]); } } else { const opt = options.find(o => String(o.value) === updatedValue); updateValue(typeof opt?.value === "number" ? parseFloat(updatedValue as string) : (updatedValue as string || null)); } }, [multiple, updateValue, options]); const renderValue = (val?: string | number) => { const option = options.find(o => o.value === val); return ( {option ? option.label : String(val)} ); }; const handleOpenChange = useCallback((open: boolean) => { if (!open && ref.current) { setTimeout(() => { ref.current?.focus({ preventScroll: true }); }, 0); } }, []); return multiple ? ( String(v)) : []} onValueChange={onChange} onOpenChange={handleOpenChange} > {options.map((opt) => ( {opt.label} ))} ) : ( ); }