Stirling-PDF/frontend/src/components/tools/convert/GroupedFormatDropdown.tsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-07-23 12:01:40 +01:00
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;
2025-07-23 16:41:27 +01:00
enabled?: boolean;
2025-07-23 12:01:40 +01:00
}
interface GroupedFormatDropdownProps {
value?: string;
placeholder?: string;
options: FormatOption[];
onChange: (value: string) => void;
disabled?: boolean;
minWidth?: string;
2025-07-28 13:58:43 +01:00
name?: string;
2025-07-23 12:01:40 +01:00
}
const GroupedFormatDropdown = ({
value,
placeholder = "Select an option",
options,
onChange,
disabled = false,
2025-07-28 13:58:43 +01:00
minWidth = "18.75rem",
name
2025-07-23 12:01:40 +01:00
}: GroupedFormatDropdownProps) => {
const [dropdownOpened, setDropdownOpened] = useState(false);
const theme = useMantineTheme();
const { colorScheme } = useMantineColorScheme();
const groupedOptions = useMemo(() => {
const groups: Record<string, FormatOption[]> = {};
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);
2025-07-23 14:30:53 +01:00
return selected ? `${selected.group} (${selected.label})` : value.toUpperCase();
2025-07-23 12:01:40 +01:00
}, [value, options, placeholder]);
const handleOptionSelect = (selectedValue: string) => {
onChange(selectedValue);
setDropdownOpened(false);
};
return (
<Popover
opened={dropdownOpened}
onDismiss={() => setDropdownOpened(false)}
2025-07-23 12:01:40 +01:00
position="bottom-start"
withArrow
shadow="sm"
disabled={disabled}
closeOnEscape={true}
trapFocus
2025-07-23 12:01:40 +01:00
>
<Popover.Target>
<UnstyledButton
2025-07-28 13:58:43 +01:00
name={name}
data-testid={name}
2025-07-23 12:01:40 +01:00
onClick={() => setDropdownOpened(!dropdownOpened)}
disabled={disabled}
style={{
padding: '0.5rem 0.75rem',
border: `0.0625rem solid ${theme.colors.gray[4]}`,
2025-07-23 12:01:40 +01:00
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%',
2025-07-30 14:24:23 +01:00
color: disabled
? colorScheme === 'dark' ? theme.colors.dark[1] : theme.colors.dark[7]
: colorScheme === 'dark' ? theme.colors.dark[0] : theme.colors.dark[9]
2025-07-23 12:01:40 +01:00
}}
>
<Group justify="space-between">
<Text size="sm" c={value ? undefined : 'dimmed'}>
{selectedLabel}
</Text>
<KeyboardArrowDownIcon
style={{
fontSize: '1rem',
2025-07-23 12:01:40 +01:00
transform: dropdownOpened ? 'rotate(180deg)' : 'rotate(0deg)',
transition: 'transform 0.2s ease',
color: colorScheme === 'dark' ? theme.colors.dark[2] : theme.colors.gray[6]
}}
/>
</Group>
</UnstyledButton>
</Popover.Target>
<Popover.Dropdown
style={{
minWidth,
backgroundColor: colorScheme === 'dark' ? theme.colors.dark[7] : theme.white,
border: `0.0625rem solid ${colorScheme === 'dark' ? theme.colors.dark[4] : theme.colors.gray[4]}`,
2025-07-23 12:01:40 +01:00
}}
>
<Stack gap="md">
{Object.entries(groupedOptions).map(([groupName, groupOptions]) => (
<Box key={groupName}>
<Text
size="sm"
fw={600}
c={colorScheme === 'dark' ? 'dark.2' : 'gray.6'}
mb="xs"
>
{groupName}
</Text>
<Group gap="xs">
{groupOptions.map((option) => (
<Button
key={option.value}
2025-07-28 13:58:43 +01:00
data-testid={`format-option-${option.value}`}
2025-07-23 12:01:40 +01:00
variant={value === option.value ? "filled" : "outline"}
size="sm"
onClick={() => handleOptionSelect(option.value)}
2025-07-23 16:41:27 +01:00
disabled={option.enabled === false}
2025-07-23 12:01:40 +01:00
style={{
fontSize: '0.75rem',
height: '2rem',
padding: '0 0.75rem'
2025-07-23 12:01:40 +01:00
}}
>
{option.label}
</Button>
))}
</Group>
</Box>
))}
</Stack>
</Popover.Dropdown>
</Popover>
);
};
export default GroupedFormatDropdown;