import React, { useState } from 'react';
import { JsonForms } from '@jsonforms/react';
import { materialCells, materialRenderers } from '@jsonforms/material-renderers';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import JsonRefs from 'json-refs';
import mySchemaWithReferences from 'myschema.json';
const yourRemoteSchemaResolver = {
order: 1,
canRead: function(file) {
return file.url.indexOf('yourRemoteSchemaIdentifier') !== -1;
},
read: function() {
return JSON.stringify(yourSchemaObject);
},
};
const refParserOptions = {
dereference: {
circular: false
},
resolve: {
foo: yourRemoteSchemaResolver
}
function App() {
const [data, setData] = useState(initialData);
const [resolvedSchema, setSchema] = useState();
useEffect(() => {
$RefParser.dereference(mySchemaWithReferences, refParserOptions).then(res => setSchema(res.$schema));
JsonRefs.resolveRefs(mySchemaWithReferences).then(res => setSchema(res.resolved));
},[]);
if(resolvedSchema === undefined) {
return <div> Loading... </div>
}
return (
<JsonForms
schema={resolvedSchema}
uischema={uischema}
data={data}
renderers={materialRenderers}
cells={materialCells}
onChange={({ data, _errors }) => setData(data)}
/>
);
}