Files
omni-tools/src/components/options/ToolOptions.tsx

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-06-21 20:57:05 +01:00
import { Box, Stack, useTheme } from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import Typography from '@mui/material/Typography';
2025-03-14 17:06:38 +00:00
import React, { ReactNode } from 'react';
2025-03-10 04:13:10 +00:00
import { FormikProps, FormikValues, useFormikContext } from 'formik';
2024-06-26 07:47:17 +01:00
import ToolOptionGroups, { ToolOptionGroup } from './ToolOptionGroups';
2024-06-21 20:24:00 +01:00
2025-02-27 01:26:48 +00:00
export type UpdateField<T> = <Y extends keyof T>(field: Y, value: T[Y]) => void;
2024-06-27 21:25:11 +01:00
2025-02-27 01:26:48 +00:00
export type GetGroupsType<T> = (
formikProps: FormikProps<T> & { updateField: UpdateField<T> }
) => ToolOptionGroup[];
2025-03-10 04:13:10 +00:00
2024-06-26 07:47:17 +01:00
export default function ToolOptions<T extends FormikValues>({
children,
2025-03-10 04:13:10 +00:00
getGroups
2024-06-26 07:47:17 +01:00
}: {
children?: ReactNode;
2025-03-08 06:43:11 +00:00
getGroups: GetGroupsType<T> | null;
2024-06-26 07:47:17 +01:00
}) {
2024-06-21 20:57:05 +01:00
const theme = useTheme();
2025-03-10 04:13:10 +00:00
const formikContext = useFormikContext<T>();
// Early return if no groups to display
if (!getGroups) {
return null;
}
const updateField: UpdateField<T> = (field, value) => {
formikContext.setFieldValue(field as string, value);
};
2025-03-09 01:50:09 +00:00
return (
<Box
sx={{
mb: 2,
borderRadius: 2,
padding: 2,
2025-03-31 01:27:44 +00:00
backgroundColor: 'background.lightSecondary',
2025-03-10 04:13:10 +00:00
boxShadow: '2'
2025-03-09 01:50:09 +00:00
}}
mt={2}
>
<Stack direction={'row'} spacing={1} alignItems={'center'}>
<SettingsIcon />
<Typography fontSize={22}>Tool options</Typography>
</Stack>
<Box mt={2}>
2025-03-10 04:13:10 +00:00
<Stack direction={'row'} spacing={2}>
<ToolOptionGroups
groups={getGroups({ ...formikContext, updateField }) ?? []}
/>
{children}
</Stack>
2024-06-26 07:47:17 +01:00
</Box>
2025-03-09 01:50:09 +00:00
</Box>
);
2024-06-21 20:24:00 +01:00
}