diff --git a/docs/src/components/PlainTextDropdown.tsx b/docs/src/components/PlainTextDropdown.tsx new file mode 100644 index 0000000..7fd4371 --- /dev/null +++ b/docs/src/components/PlainTextDropdown.tsx @@ -0,0 +1,50 @@ +import type { ForwardedRef, ReactNode } from "react" + +import useMenuTrigger from "@/hooks/useMenuTrigger" +import { mdiMenuDown } from "@mdi/js" +import { Icon } from "@mdi/react" +import { ButtonBase, ClickAwayListener, Menu, MenuList } from "@mui/material" +import cn from "clsx" +import { forwardRef, useImperativeHandle } from "react" + +interface PlainTextSelectProps { + className?: string + Label: ReactNode + children: ReactNode +} + +export interface PlainTextDropdownExposed { + close: () => void +} + +function PlainTextDropdown( + props: PlainTextSelectProps, + ref: ForwardedRef +) { + const { className, Label, children } = props + const { anchor, open, handleClick, handleClose } = useMenuTrigger() + + useImperativeHandle(ref, () => ({ + close: handleClose + })) + return ( + <> + + {Label} + + + + + + {children} + + + + + ) +} + +export default forwardRef(PlainTextDropdown) diff --git a/docs/src/components/PlainTextSelect.tsx b/docs/src/components/PlainTextSelect.tsx index 1a0877d..0f27077 100644 --- a/docs/src/components/PlainTextSelect.tsx +++ b/docs/src/components/PlainTextSelect.tsx @@ -1,49 +1,34 @@ import type { ReactNode } from "react" +import type { PlainTextDropdownExposed } from "@/components/PlainTextDropdown" -import useMenuTrigger from "@/hooks/useMenuTrigger" -import { mdiMenuDown } from "@mdi/js" -import { Icon } from "@mdi/react" -import { ButtonBase, ClickAwayListener, Menu, MenuItem, MenuList } from "@mui/material" -import cn from "clsx" -import React, { useState } from "react" +import PlainTextDropdown from "@/components/PlainTextDropdown" +import { MenuItem } from "@mui/material" +import { useRef, useState } from "react" interface PlainTextSelectProps { className?: string items: T[] renderLabel: (value: T | null) => ReactNode - children: (value: T) => React.ReactNode + children: (value: T) => ReactNode } export default function PlainTextSelect( { className, items, renderLabel, children }: PlainTextSelectProps ) { const [value, setValue] = useState(null) - const { anchor, open, handleClick, handleClose } = useMenuTrigger() + const dropdownRef = useRef(null) const handleMenuClick = (data: T | null) => { setValue(data) - handleClose() + dropdownRef.current?.close() } return ( - <> - - {renderLabel(value)} - - - - - - handleMenuClick(null)}>None - {items.map((item, index) => ( - handleMenuClick(item)}> - {children(item)} - - ))} - - - - + + handleMenuClick(null)}>None + {items.map((item, index) => ( + handleMenuClick(item)}> + {children(item)} + + ))} + ) }