I have copied the array control internals from both the Vanilla and Material UI packages to test how it is working. When I call the addItem
function within my component nothing happens. The state isn’t updated at all, the forms onChange handler isn’t called either. Can I get some more information on how this addItem is exactly working because it doesn’t seem to be able to update my forms data at all and there are no error message or feedback on what could possible be wrong.
import range from 'lodash/range';
import React, { useMemo } from 'react';
import {
ArrayControlProps,
Helpers,
JsonFormsRendererRegistryEntry,
composePaths,
createDefaultValue,
findUISchema,
isObjectArrayControl,
rankWith
} from '@jsonforms/core';
import { JsonFormsDispatch, withJsonFormsArrayControlProps } from '@jsonforms/react';
const ArrayControl: React.FC<ArrayControlProps> = ({
addItem,
data,
path,
renderers,
schema,
uischema,
uischemas
}) => {
const childUiSchema = useMemo(
() => findUISchema(uischemas || [], schema, uischema.scope, path),
[uischemas, schema, uischema.scope, path]
);
const labelDescription = Helpers.createLabelDescriptionFrom(uischema, schema);
const label = labelDescription.show ? labelDescription.text : '';
return (
<div>
<fieldset>
<legend>
<button
onClick={() => {
addItem(path, createDefaultValue(schema));
}}
>
+
</button>
<label className={'array.label'}>{label}</label>
</legend>
<div>
{data ? (
range(0, data.length).map(index => {
const childPath = composePaths(path, `${index}`);
return (
<JsonFormsDispatch
key={childPath}
path={childPath}
renderers={renderers}
schema={schema}
uischema={childUiSchema || uischema}
/>
);
})
) : (
<p>No data</p>
)}
</div>
</fieldset>
</div>
);
};
const ArrayControlRenderer = withJsonFormsArrayControlProps(ArrayControl);
const renderer: JsonFormsRendererRegistryEntry = {
renderer: ArrayControlRenderer,
tester: rankWith(
2,
isObjectArrayControl
)
};
export default renderer;
[original thread by Chad Johnson]