## Description of Changes - Removed unused `React` default imports across multiple frontend components. - Updated imports to only include required React hooks and types (e.g., `useState`, `useEffect`, `Suspense`, `createContext`). - Ensured consistency with React 17+ JSX transform, where default `React` import is no longer required. - This cleanup reduces bundle size slightly and aligns code with modern React best practices. --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. Co-authored-by: Reece Browne <74901996+reecebrowne@users.noreply.github.com>
157 lines
4.8 KiB
TypeScript
157 lines
4.8 KiB
TypeScript
import { 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<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);
|
|
return selected ? `${selected.group} (${selected.label})` : value.toUpperCase();
|
|
}, [value, options, placeholder]);
|
|
|
|
const handleOptionSelect = (selectedValue: string) => {
|
|
onChange(selectedValue);
|
|
setDropdownOpened(false);
|
|
};
|
|
|
|
return (
|
|
<Popover
|
|
opened={dropdownOpened}
|
|
onDismiss={() => setDropdownOpened(false)}
|
|
position="bottom-start"
|
|
withArrow
|
|
shadow="sm"
|
|
disabled={disabled}
|
|
closeOnEscape={true}
|
|
trapFocus
|
|
>
|
|
<Popover.Target>
|
|
<UnstyledButton
|
|
name={name}
|
|
data-testid={name}
|
|
onClick={() => 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]
|
|
}}
|
|
>
|
|
<Group justify="space-between">
|
|
<Text size="sm" c={value ? undefined : 'dimmed'}>
|
|
{selectedLabel}
|
|
</Text>
|
|
<KeyboardArrowDownIcon
|
|
style={{
|
|
fontSize: '1rem',
|
|
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: Math.min(350, parseInt(minWidth.replace('rem', '')) * 16),
|
|
maxWidth: '90vw',
|
|
maxHeight: '40vh',
|
|
overflow: 'auto',
|
|
backgroundColor: colorScheme === 'dark' ? theme.colors.dark[7] : theme.white,
|
|
border: `0.0625rem solid ${colorScheme === 'dark' ? theme.colors.dark[4] : theme.colors.gray[4]}`,
|
|
}}
|
|
>
|
|
<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" style={{ flexWrap: 'wrap' }}>
|
|
{groupOptions.map((option) => (
|
|
<Button
|
|
key={option.value}
|
|
data-testid={`format-option-${option.value}`}
|
|
variant={value === option.value ? "filled" : "outline"}
|
|
size="sm"
|
|
onClick={() => handleOptionSelect(option.value)}
|
|
disabled={option.enabled === false}
|
|
style={{
|
|
fontSize: '0.75rem',
|
|
height: '2rem',
|
|
padding: '0 0.75rem',
|
|
flexShrink: 0
|
|
}}
|
|
>
|
|
{option.label}
|
|
</Button>
|
|
))}
|
|
</Group>
|
|
</Box>
|
|
))}
|
|
</Stack>
|
|
</Popover.Dropdown>
|
|
</Popover>
|
|
);
|
|
};
|
|
|
|
export default GroupedFormatDropdown;
|