react-jsonschema-form-conditionals
react-jsonschema-form-conditionals copied to clipboard
Support formContext in conditions
I'm trying to make a PR to add support for the formContext
in the rule conditions. Here is the new test I've added to your codebase. It's proving to be much more difficult (or rather, hacky) than I expected.
test("FormConditionals - onChange called with corrected schema when formContext changes", () => {
const changed = sinon.spy(() => {});
const RULES = [
{
conditions: {
'formContext.foo': "empty",
},
event: {
type: "remove",
params: {
field: ["lastName", "name"],
},
},
},
];
const props = { schema, uiSchema: {}, rules: RULES, Engine };
const formContext = { foo: 1 };
const wrapper = mount(
<FormWithConditionals {...props} formData={{ firstName: "A" }} formContext={formContext} onChange={changed} />
);
wrapper.setProps({ formContext: { foo: undefined } });
return new Promise(resolve => setTimeout(resolve, 500)).then(() => {
const expSchema = {
type: "object",
properties: {
firstName: { type: "string" },
},
};
expect(changed.called).toEqual(true);
expect(changed.calledOnce).toEqual(true);
expect(changed.getCall(0).args[0].schema).toEqual(expSchema);
});
});
Any chance you could give me your high level two cents on how you might implement this and I could cross check it with what I have? I'll open a WIP PR so you can see where I'm currently at.