import type { PropsWithChildren } from 'react'; import { useCallback, useState, useRef } from 'react'; import classNames from 'classnames'; import type { Placement, State as PopperState } from '@popperjs/core'; import Overlay from 'react-overlays/Overlay'; import ArrowDropDownIcon from '@/material-icons/400-24px/arrow_drop_down.svg?react'; import type { SelectItem } from 'mastodon/components/dropdown_selector'; import { DropdownSelector } from 'mastodon/components/dropdown_selector'; import { Icon } from 'mastodon/components/icon'; interface DropdownProps { value: string; options: SelectItem[]; disabled?: boolean; onChange: (value: string) => void; placement?: Placement; } const Dropdown: React.FC = ({ value, options, disabled, onChange, placement: initialPlacement = 'bottom-end', }) => { const activeElementRef = useRef(null); const containerRef = useRef(null); const [isOpen, setOpen] = useState(false); const [placement, setPlacement] = useState(initialPlacement); const handleToggle = useCallback(() => { if ( isOpen && activeElementRef.current && activeElementRef.current instanceof HTMLElement ) { activeElementRef.current.focus({ preventScroll: true }); } setOpen(!isOpen); }, [isOpen, setOpen]); const handleMouseDown = useCallback(() => { if (!isOpen) activeElementRef.current = document.activeElement; }, [isOpen]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { switch (e.key) { case ' ': case 'Enter': if (!isOpen) activeElementRef.current = document.activeElement; break; } }, [isOpen], ); const handleClose = useCallback(() => { if ( isOpen && activeElementRef.current && activeElementRef.current instanceof HTMLElement ) activeElementRef.current.focus({ preventScroll: true }); setOpen(false); }, [isOpen]); const handleOverlayEnter = useCallback( (state: Partial) => { if (state.placement) setPlacement(state.placement); }, [setPlacement], ); const valueOption = options.find((item) => item.value === value); return (
{({ props, placement }) => (
)}
); }; interface Props { value: string; options: SelectItem[]; disabled?: boolean; onChange: (value: string) => void; } export const SelectWithLabel: React.FC> = ({ value, options, disabled, children, onChange, }) => { return ( ); };