/* * @Author: ZhaoYing * @Date: 2026-02-03 15:40:13 * @Last Modified by: ZhaoYing * @Last Modified time: 2026-04-03 20:19:34 */ import { useState, useRef, useEffect, useLayoutEffect, type FC } from 'react' import { createPortal } from 'react-dom' import clsx from 'clsx'; import { Flex, Space, Checkbox } from 'antd' import { useTranslation } from 'react-i18next'; import type { Suggestion } from '../Editor/plugin/AutocompletePlugin' interface VariableSelectProps { options: Suggestion[]; value?: string | string[]; allowClear?: boolean; filterBooleanType?: boolean; multiple?: boolean; size?: 'small' | 'middle' | 'large'; placeholder?: string; variant?: 'outlined' | 'borderless'; className?: string; onChange?: (value: string | string[], option: Suggestion | Suggestion[] | undefined) => void; } const VariableSelect: FC = ({ placeholder, options, value, allowClear = true, onChange, size = 'middle', filterBooleanType = false, multiple = false, variant = 'outlined', className, }) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); const [search, setSearch] = useState(''); const [expandedParent, setExpandedParent] = useState(null); const [dropdownPos, setDropdownPos] = useState({ top: 0, left: 0, width: 0 }); const [childPanelPos, setChildPanelPos] = useState({ top: 0, right: 0 }); const containerRef = useRef(null); const dropdownRef = useRef(null); const itemRefs = useRef>(new Map()); const CHILD_PANEL_HEIGHT = 280; // max-h-60 (240) + header (~40) // Calculate dropdown position (runs synchronously after DOM paint to avoid flicker) useLayoutEffect(() => { if (!open || !containerRef.current) return; const triggerRect = containerRef.current.getBoundingClientRect(); const MARGIN = 8; const width = triggerRect.width; // Set initial width/left immediately; top will be refined once dropdownRef is available if (!dropdownRef.current) { setDropdownPos({ top: triggerRect.bottom + MARGIN, left: triggerRect.left, width }); return; } const dropdownHeight = dropdownRef.current.offsetHeight; const dropdownWidth = dropdownRef.current.offsetWidth; const left = Math.min(triggerRect.left, window.innerWidth - dropdownWidth - 10); const spaceBelow = window.innerHeight - triggerRect.bottom - MARGIN; const spaceAbove = triggerRect.top - MARGIN; const top = (spaceBelow >= dropdownHeight || spaceBelow >= spaceAbove) ? triggerRect.bottom + MARGIN : Math.max(MARGIN, triggerRect.top - dropdownHeight - MARGIN); setDropdownPos({ top, left, width }); }, [open, search, Array.isArray(value) ? value.length : 0]); const filteredOptions = filterBooleanType ? options.filter(o => o.dataType !== 'boolean') : options; const allSuggestions = filteredOptions.flatMap(o => o.children ? [o, ...o.children] : [o]); const suggestionMap = new Map(allSuggestions.map(s => [`{{${s.value}}}`, s])); const selectedValues = multiple ? (Array.isArray(value) ? value : []) : []; const selectedSuggestion = !multiple && value ? suggestionMap.get(value as string) : undefined; const parentOfSelected = !multiple && value ? filteredOptions.find(o => o.children?.some(c => `{{${c.value}}}` === value)) : undefined; const groupedSuggestions = filteredOptions.reduce((groups: Record, s) => { const nodeId = s.nodeData.id as string; if (!groups[nodeId]) groups[nodeId] = []; groups[nodeId].push(s); return groups; }, {}); const filteredGroups = search ? Object.entries(groupedSuggestions).reduce((acc: Record, [nodeId, suggestions]) => { const matched = suggestions.filter(s => s.label.toLowerCase().includes(search.toLowerCase()) || s.value.toLowerCase().includes(search.toLowerCase()) || s.children?.some(c => c.label.toLowerCase().includes(search.toLowerCase())) ); if (matched.length) acc[nodeId] = matched; return acc; }, {}) : groupedSuggestions; useEffect(() => { if (!open) return; const updatePos = () => { if (!containerRef.current || !dropdownRef.current) return; const triggerRect = containerRef.current.getBoundingClientRect(); const dropdownHeight = dropdownRef.current.offsetHeight; const dropdownWidth = dropdownRef.current.offsetWidth; const MARGIN = 8; const left = Math.min(triggerRect.left, window.innerWidth - dropdownWidth - 10); const spaceBelow = window.innerHeight - triggerRect.bottom - MARGIN; const spaceAbove = triggerRect.top - MARGIN; let top: number; if (spaceBelow >= dropdownHeight || spaceBelow >= spaceAbove) { top = triggerRect.bottom + MARGIN; } else { top = triggerRect.top - dropdownHeight - MARGIN; if (top < MARGIN) top = MARGIN; } setDropdownPos(prev => ({ ...prev, top, left })); }; document.addEventListener('scroll', updatePos, true); return () => document.removeEventListener('scroll', updatePos, true); }, [open]); useEffect(() => { if (!open) return; const handler = (e: MouseEvent) => { const target = e.target as Node; const childPanel = document.getElementById('variable-select-child-panel'); if ( !containerRef.current?.contains(target) && !dropdownRef.current?.contains(target) && !childPanel?.contains(target) ) { setOpen(false); setSearch(''); setExpandedParent(null); setChildPanelPos({ top: 0, right: 0 }); } }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, [open]); const handleSelect = (suggestion: Suggestion) => { if (multiple) { const key = `{{${suggestion.value}}}`; const next = selectedValues.includes(key) ? selectedValues.filter(v => v !== key) : [...selectedValues, key]; const nextOptions = next.map(v => suggestionMap.get(v)).filter(Boolean) as Suggestion[]; onChange?.(next, nextOptions); } else { onChange?.(`{{${suggestion.value}}}`, suggestion); setOpen(false); setSearch(''); setExpandedParent(null); } }; const handleClear = (e: React.MouseEvent) => { e.stopPropagation(); onChange?.(multiple ? [] : '', multiple ? [] : undefined); }; const updateChildPos = (key: string) => { const el = itemRefs.current.get(key); if (el) { const rect = el.getBoundingClientRect(); const spaceBelow = window.innerHeight - rect.top - 10; const top = spaceBelow >= CHILD_PANEL_HEIGHT ? rect.top : Math.max(10, window.innerHeight - CHILD_PANEL_HEIGHT - 10); setChildPanelPos({ top, right: window.innerWidth - rect.left + 8 }); } }; const sep = /; const isConversation = (parentOfSelected ?? selectedSuggestion)?.group === 'CONVERSATION' || (selectedSuggestion ? filteredOptions.some(o => o.group === 'CONVERSATION' && o.children?.some(c => `{{${c.value}}}` === value)) : false); const nodeData = (parentOfSelected ?? selectedSuggestion)?.nodeData; return (
{/* Trigger */}
setOpen(o => !o)} > {multiple ? ( selectedValues.length > 0 ? ( {selectedValues.map(v => { const s = suggestionMap.get(v); if (!s) return null; const parent = filteredOptions.find(o => o.children?.some(c => `{{${c.value}}}` === v)); const nd = s.nodeData; const isConv = (parent ?? s)?.group === 'CONVERSATION' || filteredOptions.some(o => o.group === 'CONVERSATION' && o.children?.some(c => `{{${c.value}}}` === v)); return ( {!isConv && nd?.icon &&
} {!isConv && nd?.name && {nd.name}{sep}} {parent ? <>{parent.label}{sep}{s.label} : s.label} { e.stopPropagation(); handleSelect(s); }} >✕ ); })} ) : ( {placeholder} ) ) : selectedSuggestion ? ( {!isConversation && nodeData?.icon &&
} {!isConversation && nodeData?.name && {nodeData.name}{sep}} {parentOfSelected ? <>{parentOfSelected.label}{sep}{selectedSuggestion.label} : selectedSuggestion.label} ) : ( {placeholder} )} {allowClear && ( 0 : !!selectedSuggestion) ? 'rb:opacity-100 rb:cursor-pointer' : 'rb:opacity-0 rb:pointer-events-none' )} onClick={handleClear} >✕ )}
{/* Dropdown via portal */} {open && createPortal(
{Object.entries(filteredGroups).map(([nodeId, suggestions]) => { const nd = suggestions[0].nodeData; return (
{nd.icon &&
} {nd.name} {suggestions.map(s => { const isSelected = multiple ? selectedValues.includes(`{{${s.value}}}`) : `{{${s.value}}}` === value; const isExpanded = expandedParent?.key === s.key; const hasChildren = !!s.children?.length; return ( { if (el) itemRefs.current.set(s.key, el); }} className="rb:mx-3! rb:pl-3! rb:pr-3! rb:py-1.5! rb:rounded-lg!" align="center" justify="space-between" style={{ cursor: s.disabled ? 'not-allowed' : 'pointer', background: isSelected || isExpanded ? '#f0f8ff' : 'white', opacity: s.disabled ? 0.5 : 1, }} onClick={() => { if (s.disabled) return; if (hasChildren) { updateChildPos(s.key); setExpandedParent(prev => prev?.key === s.key ? null : s); } handleSelect(s); }} onMouseEnter={() => { if (hasChildren) { updateChildPos(s.key); setExpandedParent(s); } else { setExpandedParent(null); } }} > {multiple && ( )} {`{x}`} {s.label} {s.dataType && {s.dataType}} {hasChildren &&
}
); })}
); })} {Object.keys(filteredGroups).length === 0 && (
{t('workflow.variableSelect.empty', '暂无变量')}
)}
, document.body )} {/* Child panel via portal — escapes overflow clipping */} {open && expandedParent?.children?.length && createPortal(
setExpandedParent(expandedParent)} >
!expandedParent.disabled && handleSelect(expandedParent)} > {expandedParent.nodeData.name}.{expandedParent.label} {expandedParent.dataType}
{expandedParent.children.map(child => { const isSelected = multiple ? selectedValues.includes(`{{${child.value}}}`) : `{{${child.value}}}` === value; const hasGrandChildren = !!child.children?.length; return ( !child.disabled && handleSelect(child)} > {multiple && ( )} {child.label} {child.dataType && {child.dataType}} {hasGrandChildren && } ); })}
, document.body )}
); }; export default VariableSelect