import React, { useRef } from 'react' import { CheckIcon, ChevronDownIcon, ChevronUpIcon, } from '@heroicons/react/24/outline' import * as Select from '@radix-ui/react-select' type SelectorChevronDirection = 'up' | 'down' interface Props { width?: number value: string options: string[] chevronDirection?: SelectorChevronDirection autoFocusAfterClose?: boolean onChange: (value: string) => void disabled?: boolean } const Selector = (props: Props) => { const { width, value, chevronDirection, options, autoFocusAfterClose, onChange, disabled, } = props const contentRef = useRef(null) const onOpenChange = (open: boolean) => { if (!open) { if (!autoFocusAfterClose) { // 如果使用 Select.Content 的 onCloseAutoFocus 来取消 focus(防止空格继续打开这个 select) // 会导致其它快捷键失效,原因未知 window.setTimeout(() => { contentRef?.current?.blur() }, 100) } } } return ( e.preventDefault()} disabled={disabled} > {chevronDirection === 'up' ? : } {options.map(val => ( {val} ))} ) } const selectorDefaultProps = { chevronDirection: 'down', autoFocusAfterClose: true, disabled: false, } Selector.defaultProps = selectorDefaultProps export default Selector