import React, { useState, useMemo } from "react"; import { Stack, Text, Group, Button, Box, Popover, UnstyledButton, useMantineTheme, useMantineColorScheme } from "@mantine/core"; import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; interface FormatOption { value: string; label: string; group: string; enabled?: boolean; } interface GroupedFormatDropdownProps { value?: string; placeholder?: string; options: FormatOption[]; onChange: (value: string) => void; disabled?: boolean; minWidth?: string; name?: string; } const GroupedFormatDropdown = ({ value, placeholder = "Select an option", options, onChange, disabled = false, minWidth = "18.75rem", name }: GroupedFormatDropdownProps) => { const [dropdownOpened, setDropdownOpened] = useState(false); const theme = useMantineTheme(); const { colorScheme } = useMantineColorScheme(); const groupedOptions = useMemo(() => { const groups: Record = {}; options.forEach(option => { if (!groups[option.group]) { groups[option.group] = []; } groups[option.group].push(option); }); return groups; }, [options]); const selectedLabel = useMemo(() => { if (!value) return placeholder; const selected = options.find(opt => opt.value === value); return selected ? `${selected.group} (${selected.label})` : value.toUpperCase(); }, [value, options, placeholder]); const handleOptionSelect = (selectedValue: string) => { onChange(selectedValue); setDropdownOpened(false); }; return ( setDropdownOpened(false)} position="bottom-start" withArrow shadow="sm" disabled={disabled} closeOnEscape={true} trapFocus > setDropdownOpened(!dropdownOpened)} disabled={disabled} style={{ padding: '0.5rem 0.75rem', border: `0.0625rem solid ${theme.colors.gray[4]}`, borderRadius: theme.radius.sm, backgroundColor: disabled ? theme.colors.gray[1] : colorScheme === 'dark' ? theme.colors.dark[6] : theme.white, cursor: disabled ? 'not-allowed' : 'pointer', width: '100%', color: disabled ? colorScheme === 'dark' ? theme.colors.dark[1] : theme.colors.dark[7] : colorScheme === 'dark' ? theme.colors.dark[0] : theme.colors.dark[9] }} > {selectedLabel} {Object.entries(groupedOptions).map(([groupName, groupOptions]) => ( {groupName} {groupOptions.map((option) => ( ))} ))} ); }; export default GroupedFormatDropdown;