import React from 'react' import { Dropdown } from 'mdui' import useEventListener from '../useEventListener.ts' interface Args extends React.HTMLAttributes { title: string icon: string disabled?: boolean updater: (value: unknown) => void selections: { [id: string]: string } defaultCheckedId: string } export default function SelectPreference({ title, icon, updater, selections, defaultCheckedId, disabled }: Args) { const [checkedId, setCheckedId] = React.useState(defaultCheckedId) const dropDownRef = React.useRef(null) const [isDropDownOpen, setDropDownOpen] = React.useState(false) useEventListener(dropDownRef, 'closed', () => { setDropDownOpen(false) }) return setDropDownOpen(!isDropDownOpen)}> {title} { e.stopPropagation() setDropDownOpen(false) }}> { Object.keys(selections).map((id) => // @ts-ignore: selected 确实存在, 但是并不对外公开使用 { setCheckedId(id) updater(id) }}>{selections[id]} ) } {selections[checkedId]} }