Skip to content

Commit

Permalink
fix(core): custom onChange for dynamic
Browse files Browse the repository at this point in the history
Sets the parent's key to the new model
  • Loading branch information
soup-in-boots committed Apr 28, 2024
1 parent af2df12 commit ce754e5
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/core/src/components/mapper/dynamic.jsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import debug from 'debug';
import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import { useActionsFor } from '@forml/hooks';

import { useRenderingContext } from '@forml/hooks';

const log = debug('forml:core:dynamic');

export default function makeDynamic(SchemaForm) {
function GeneratedDynamic(props) {
const { form: parent, value } = props;
const form = parent.generate(value);
return <BaseDynamic {...props} parent={parent} form={form} />;
const { form: parent } = props;
return <BaseDynamic {...props} parent={parent} form={parent.generate} />;
}
function StaticDynamic(props) {
const { form: parent } = props;
const form = parent.generate;
return <BaseDynamic {...props} parent={parent} form={form} />;
}
function BaseDynamic(props) {
const { form, parent, value } = props;
const { form, parent, schema, value } = props;
const actions = useActionsFor(parent.key);
const ctx = useRenderingContext();
const { decorator, mapper, localizer } = ctx;

const onChange = useCallback(
(event, value) => {
const nextModel = actions.setValue(value);
props.onChange(event, nextModel);
},
[props.onChange, actions]
);

return (
<SchemaForm
{...props}
schema={schema}
decorator={decorator}
mapper={mapper}
localizer={localizer}
form={form}
model={value}
onChange={onChange}
parent={parent}
/>
);
Expand Down

0 comments on commit ce754e5

Please sign in to comment.