Hi,
when trying to add a generic wrapper around JsonForm form fields, I encounter problems for propagating the right props to the underlying components.
The idea is to add a custom renderer that, depending on available properties in the schema, will add a wrapping div around the form field with some custom classes.
The problem is that when using the withJsonFormsControlProps
HOC, the schema prop of the underlying component, rendered by using the JsonFormsDispatch
component, needs to replaced by the root schema in order to keep all the custom props for other custom renderers.
But when doing this, the default prop is lost in the process, because no defaults are being rendered any more.
Could you please tell me how to use withJsonFormControlProps
and JsonFormsDispatch
properly to achieve what I’m looking for? Thanks! Here is my current implementation of the Wrapper renderer:
const WrapperRenderer = (props: ControlProps) => {
const hasProp = (p: string) => props.schema.hasOwnProperty(p);
const isEssential = hasProp('isEssential');
const inCalculation = hasProp('inCalculation');
const classes = useStyles();
return (
<div
className={clsx(classes.wrapped, {
[classes.essential]: isEssential,
[classes.calculation]: inCalculation,
[classes.both]: isEssential && inCalculation,
})}
>
<JsonFormsDispatch {...props} schema={props.rootSchema} renderers={renderers(['wrapper'])} />
</div>
);
};
export default withJsonFormsControlProps(WrapperRenderer);
export const isWrapperControl = and(
uiTypeIs('Control'),
schemaMatches(
(schema) =>
!isEmpty(schema) &&
(schema.hasOwnProperty('isEssential') || schema.hasOwnProperty('inCalculation')),
),
);
export const tester = rankWith(10000, isWrapperControl);
Some example schema:
export const example: JsonFormSchemas = {
schema: {
type: 'object',
properties: {
firstName: {
type: 'string',
default: 'John', // displayed correctly
readOnly: true, // correctly being disabled
},
lastName: {
type: 'string',
isEssential: true,
default: 'Doe', // NOT being displayed correctly (because wrapped by Wrapper component)
readOnly: true, // NOT being disabled
},
density: {
type: 'integer',
unit: '<sup>kg</sup>⁄<sub>m<sup>3</sup></sub>',
isEssential: true,
inCalculation: true,
default: 15, // NOT being used
minimum: 10,
maximum: 20,
},
density2: {
type: 'integer',
unit: '<sup>kg</sup>⁄<sub>m<sup>3</sup></sub>',
inCalculation: true,
default: 15, // NOT being used
minimum: 10,
maximum: 20,
},
},
required: ['lastName', 'density'],
},
data: {},
};
Now, when I remove the withJsonFormsControlProps
HOC, the readOnly
and disabled
prop are being used correctly, but the wrapper classes are not set, because the schema of the Wrapper renderer is replaced by the root schema.