'use client'; import * as SelectPrimitive from '@radix-ui/react-select'; import { Check, ChevronDown, ChevronUp, X } from 'lucide-react'; import * as React from 'react'; import { cn } from '@/lib/utils'; import { ControllerRenderProps } from 'react-hook-form'; import { FormControl } from '@/components/ui/form'; import { forwardRef, useCallback, useEffect } from 'react'; const Select = SelectPrimitive.Root; const SelectGroup = SelectPrimitive.Group; const SelectValue = SelectPrimitive.Value; const SelectTrigger = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { onReset?: () => void; allowClear?: boolean; } >(({ className, children, value, onReset, allowClear, ...props }, ref) => ( span]:line-clamp-1', className, )} {...props} > {children} { event.stopPropagation(); }} > {value && allowClear ? ( ) : ( )} )); SelectTrigger.displayName = SelectPrimitive.Trigger.displayName; const SelectScrollUpButton = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName; const SelectScrollDownButton = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectScrollDownButton.displayName = SelectPrimitive.ScrollDownButton.displayName; const SelectContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, position = 'popper', ...props }, ref) => ( {children} )); SelectContent.displayName = SelectPrimitive.Content.displayName; const SelectLabel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectLabel.displayName = SelectPrimitive.Label.displayName; const SelectItem = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( {children} )); SelectItem.displayName = SelectPrimitive.Item.displayName; const SelectSeparator = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectSeparator.displayName = SelectPrimitive.Separator.displayName; export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue, }; export type RAGFlowSelectOptionType = { label: React.ReactNode; value: string; disabled?: boolean; }; export type RAGFlowSelectGroupOptionType = { label: React.ReactNode; options: RAGFlowSelectOptionType[]; }; export type RAGFlowSelectProps = Partial & { FormControlComponent?: typeof FormControl; options?: (RAGFlowSelectOptionType | RAGFlowSelectGroupOptionType)[]; allowClear?: boolean; placeholder?: React.ReactNode; contentProps?: React.ComponentPropsWithoutRef; } & SelectPrimitive.SelectProps; /** * * Reference: * https://github.com/shadcn-ui/ui/discussions/638 * https://github.com/radix-ui/primitives/discussions/2645#discussioncomment-8343397 * * @export * @param {(Partial & { * FormControlComponent?: typeof FormControl; * })} { * value: initialValue, * onChange, * FormControlComponent, * } * @return {*} */ export const RAGFlowSelect = forwardRef< React.ElementRef, RAGFlowSelectProps >(function ( { value: initialValue, onChange, FormControlComponent, options = [], allowClear, placeholder, contentProps = {}, }, ref, ) { const [key, setKey] = React.useState(+new Date()); const [value, setValue] = React.useState(undefined); const FormControlWidget = FormControlComponent ? FormControlComponent : ({ children }: React.PropsWithChildren) =>
{children}
; const handleChange = useCallback( (val?: string) => { setValue(val); onChange?.(val); }, [onChange], ); const handleReset = useCallback(() => { handleChange(undefined); setKey(+new Date()); }, [handleChange]); useEffect(() => { setValue((preValue) => { if (preValue !== initialValue) { return initialValue; } return preValue; }); }, [initialValue]); return ( ); }); RAGFlowSelect.displayName = 'RAGFlowSelect';