diff --git a/packages/editor/src/components/ComponentForm/ComponentForm.tsx b/packages/editor/src/components/ComponentForm/ComponentForm.tsx index 2c52d11b..7da6ff33 100644 --- a/packages/editor/src/components/ComponentForm/ComponentForm.tsx +++ b/packages/editor/src/components/ComponentForm/ComponentForm.tsx @@ -87,9 +87,8 @@ export const ComponentForm: React.FC = props => { }); }); - const eventHandlers = - selectedComponent.traits.find(t => t.type === 'core/v1/event')?.properties.handlers || - ([] as any); + const eventHandlers = selectedComponent.traits.find(t => t.type === 'core/v1/event') + ?.properties.handlers; return ( diff --git a/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx b/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx index 66abecbe..a485822c 100644 --- a/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx +++ b/packages/editor/src/components/ComponentForm/EventTraitForm/EventHandlerForm.tsx @@ -12,10 +12,10 @@ import { Static } from '@sinclair/typebox'; import { CloseIcon } from '@chakra-ui/icons'; import { useFormik } from 'formik'; import { EventHandlerSchema } from '@meta-ui/runtime'; - import { registry } from '../../../metaUI'; import { useAppModel } from '../../../operations/useAppModel'; import { formWrapperCSS } from '../style'; +import produce from 'immer'; type Props = { eventTypes: string[]; @@ -47,10 +47,25 @@ export const EventHandlerForm: React.FC = props => { updateMethods(e.target.value); }; + // because parameters is object, so it has to be converted to string to edit + const initialValues = produce(handler, draft => { + draft.method.parameters = JSON.stringify(draft.method.parameters); + }); + const formik = useFormik({ - initialValues: handler, + initialValues, onSubmit: values => { - onChange(values); + try { + const newHandler = produce(values, draft => { + draft.method.parameters = JSON.parse(draft.method.parameters); + }); + onChange(newHandler); + } catch (e) { + console.error( + `Error happened when parsing method parameters. Cannot parse ${values.method.parameters} as JSON.` + ); + return; + } }, });