import React, { memo } from 'react'; import { IToolbarItem, IToolbarProps } from './toolbar-item'; import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger } from '../ui/dropdown-menu'; import { Button } from '../ui/button'; import { ToolbarItemContainer } from './container'; import { produce } from 'immer'; export interface ToolbarSelectButtonItem extends IToolbarItem { options: { key: T; icon: ( props: React.SVGProps & { title?: string | undefined; titleId?: string | undefined; } ) => React.ReactNode; label: string; /** @default false */ disabled?: boolean; }[]; value: T; onSelect: (value: T) => void; } const ToolbarSelectButton = memo>(function ToolbarSelectButton(props) { const { item, openedKey, setOpenedKey } = props; const { key, icon: Icon, disabled, options, value, onSelect, styles } = item; const id = `${key}::button`; const opened = openedKey === id; const currentOption = options.find((opt) => opt.key === value); // the button shows the selected option itself; the item icon is the fallback when nothing matches const ButtonIcon = currentOption?.icon ?? Icon; return ( (open ? setOpenedKey(id) : setOpenedKey(''))}> { if (currentOption) { draft.label = `${draft.label}: ${currentOption.label}`; } })} > e.preventDefault()}> {options.map((option) => { const OptionIcon = option.icon; return ( {option.label} ); })} ); }); export default ToolbarSelectButton;