import { CSSTransition } from "react-transition-group"; import styles from "./Select.module.scss"; import { IoMdArrowDropup, IoMdArrowDropdown } from "react-icons/io"; import * as React from "react"; import { SelectContainer } from "./SelectContainer"; import classNames from "classnames"; import { IconButton } from "../IconButton"; export interface SelectProps extends React.ComponentPropsWithoutRef<"div"> { /** * The content for the Select */ children: React.ReactNode; /** * If the Select is open * * @default false */ isOpen?: boolean; /** * The callback fired when the Select closes */ onClose: () => void; /** * The callback fired when the Select opens */ onOpen: () => void; /** * The placeholder for the Select */ placeholder?: string; /** * The initial selected item's index for the Select */ selected?: number; } /** * @public * * @description * * The Select component displays a dropdown of selectable options. */ export const Select: React.FC = (props) => { const { className, children, isOpen = false, onClose, onOpen, placeholder, selected, ...rest } = props; const [selectedIndex, setSelectedIndex] = React.useState(selected); const [value, setValue] = React.useState(); const selectRef = React.useRef(null); const triggerRef = React.useRef(null); React.useEffect(() => { if (selectedIndex !== undefined) { setValue( React.Children.toArray(children)[selectedIndex] as string ); } }, [selectedIndex]); const onSetSelectedIndex = (index: number) => { setSelectedIndex(index); }; return (
{ if (isOpen) { onClose(); } else { onOpen(); } }} > {value ??
{placeholder}
}
{isOpen ? ( } onClick={onClose} ref={triggerRef} /> ) : ( } onClick={onOpen} ref={triggerRef} /> )} {children}
); }; Select.displayName = "Select";