mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-17 18:44:02 +01:00
fix: compute not firing
This commit is contained in:
@@ -1,48 +1,21 @@
|
||||
import { Box, Stack, useTheme } from '@mui/material';
|
||||
import SettingsIcon from '@mui/icons-material/Settings';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import React, { ReactNode, useContext } from 'react';
|
||||
import React, { ReactNode } from 'react';
|
||||
import { FormikProps, FormikValues, useFormikContext } from 'formik';
|
||||
import ToolOptionGroups, { ToolOptionGroup } from './ToolOptionGroups';
|
||||
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
||||
|
||||
export type UpdateField<T> = <Y extends keyof T>(field: Y, value: T[Y]) => void;
|
||||
|
||||
const FormikListenerComponent = <T,>({
|
||||
input,
|
||||
compute
|
||||
}: {
|
||||
input: any;
|
||||
compute: (optionsValues: T, input: any) => void;
|
||||
}) => {
|
||||
const { values } = useFormikContext<T>();
|
||||
const { showSnackBar } = useContext(CustomSnackBarContext);
|
||||
|
||||
React.useEffect(() => {
|
||||
try {
|
||||
compute(values, input);
|
||||
} catch (exception: unknown) {
|
||||
if (exception instanceof Error) showSnackBar(exception.message, 'error');
|
||||
else console.error(exception);
|
||||
}
|
||||
}, [values, input, showSnackBar]);
|
||||
|
||||
return null; // This component doesn't render anything
|
||||
};
|
||||
|
||||
export type GetGroupsType<T> = (
|
||||
formikProps: FormikProps<T> & { updateField: UpdateField<T> }
|
||||
) => ToolOptionGroup[];
|
||||
|
||||
export default function ToolOptions<T extends FormikValues>({
|
||||
children,
|
||||
compute,
|
||||
input,
|
||||
getGroups
|
||||
}: {
|
||||
children?: ReactNode;
|
||||
compute: (optionsValues: T, input: any) => void;
|
||||
input?: any;
|
||||
getGroups: GetGroupsType<T> | null;
|
||||
}) {
|
||||
const theme = useTheme();
|
||||
@@ -74,7 +47,6 @@ export default function ToolOptions<T extends FormikValues>({
|
||||
</Stack>
|
||||
<Box mt={2}>
|
||||
<Stack direction={'row'} spacing={2}>
|
||||
<FormikListenerComponent<T> compute={compute} input={input} />
|
||||
<ToolOptionGroups
|
||||
groups={getGroups({ ...formikContext, updateField }) ?? []}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user