Hi,
I’m trying to populate a select in a second form based on the data entered in the first one. The middleware has been very helpful for this, however I’m struggling with the lifecycle.
If you populate the data in Top and then add a row in Bottom it works as expected.
However if you add a row in Bottom and then add or edit a row in Top the options aren’t changed, even though the schema has been.
This is also the case if it’s all done in one schema.
Is there a way to trigger the selects to re-render when the schema is changed? Is there another way to approach this?
import {
materialRenderers,
materialCells,
} from "@jsonforms/material-renderers";
import { JsonForms } from "@jsonforms/react";
export default function ExampleForm(props) {
const firstSchema = {
type: "object",
properties: {
top: {
type: "array",
items: {
type: "object",
properties: {
firstField: {
type: "string",
enum: ["foo", "bar", "baz"],
},
secondField: {
type: "string",
},
},
},
},
},
};
const secondSchema = {
type: "object",
properties: {
bottom: {
type: "array",
items: {
type: "object",
properties: {
thirdField: {
type: "string",
enum: ["None"],
},
fourthField: {
type: "string",
},
},
},
},
},
};
return (
<>
<JsonForms
schema={firstSchema}
renderers={materialRenderers}
cells={materialCells}
middleware={(state, action, reducer) => {
if (action.type === "jsonforms/UPDATE") {
const newState = reducer(state, action);
secondSchema.properties.bottom.items.properties.thirdField.enum =
newState.data.top.map(
({ firstField, secondField }) =>
`${firstField}: ${secondField}`,
);
return newState;
} else {
return reducer(state, action);
}
}}
/>
<JsonForms
schema={secondSchema}
renderers={materialRenderers}
cells={materialCells}
/>
</>
);
}