'use client'; import { cn } from '@/lib'; import React, { useState } from 'react'; import rightIcon from '@/assets/components/go_right.svg'; import Image from 'next/image'; import { Select as SelectPrimitive } from 'radix-ui'; import { useControllableValue } from 'ahooks'; import { InputLeft } from '.'; const { Root, Trigger, Portal, Content, Viewport, Item, ItemText } = SelectPrimitive; const View: React.FC< { icon?: any; text?: string; expand?: boolean; } & React.HTMLAttributes > = (props) => { const { icon, text, expand, ...rest } = props; return (
right
); }; type SelectProps = { options?: { label: string; value: string; }[]; value?: string; defaultValue?: string; onChange?: (value: string) => void; render?: (option: { label: string; value: string }) => React.ReactNode; placeholder?: string; icon?: any; contentClassName?: string; // 自定义下拉框样式 defaultOpen?: boolean; // 默认是否打开 onOpenChange?: (open: boolean) => void; // 打开/关闭回调 zIndex?: number; } & React.HTMLAttributes; function Select(props: SelectProps) { const { options = [], render, placeholder = '请选择', icon, className, contentClassName, zIndex, } = props; // 使用 useControllableValue 管理状态,支持受控和非受控模式 const [value, setValue] = useControllableValue(props); const [open, setOpen] = useState(false); // 找到当前选中项的 label const selectedLabel = options.find((opt) => opt.value === value)?.label; return ( setOpen(!open)} > {/* Trigger - 使用 View 组件 */} {/* 下拉内容 */} {options.map((option) => ( {render ? render(option) : option.label} ))} ); } Select.View = View; export default Select;