Skip to content

Commit 0f317d2

Browse files
authored
BUGFIX: Form builder is appending properties to ui schema with no uiSchema change (#217)
1 parent 66e7f88 commit 0f317d2

File tree

3 files changed

+52
-57
lines changed

3 files changed

+52
-57
lines changed

src/formBuilder/FormBuilder.test.js

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ describe('FormBuilder', () => {
161161
expect(mockEvent).toHaveBeenCalledTimes(0);
162162
createButton.simulate('click');
163163
expect(mockEvent).toHaveBeenCalledTimes(1);
164+
165+
const cardInputs = wrapper.first().find('input');
164166
mockEvent.mockClear();
165167
});
166168

@@ -624,73 +626,51 @@ describe('FormBuilder', () => {
624626
mockEvent.mockClear();
625627
});
626628

627-
it("should allow changing of a Section element's 'required' property", () => {
628-
const sectionUiSchema = {
629-
definitions: {
630-
full_names: {
631-
'ui:order': ['first_names', 'last_names'],
629+
it('should edit card slug and override ui:schema with updated slug', () => {
630+
let jsonSchema = {
631+
properties: {
632+
newInput1: {
633+
title: 'New Input 1',
634+
type: 'string',
632635
},
633636
},
634-
user_full_names: {
635-
'ui:order': ['first_names', 'last_names'],
636-
},
637-
'ui:order': ['user_full_names'],
638637
};
639638

640-
const sectionJsonSchema = {
641-
definitions: {
642-
full_names: {
643-
title: 'Full Names',
644-
type: 'object',
645-
description: 'This is a composite field',
646-
properties: {
647-
first_names: {
648-
title: 'First Names',
649-
type: 'string',
650-
},
651-
last_names: {
652-
title: 'Last Names',
653-
type: 'string',
654-
},
655-
},
656-
dependencies: {},
657-
required: [],
658-
},
659-
},
660-
properties: {
661-
user_full_names: {
662-
$ref: '#/definitions/full_names',
663-
title: 'User Full Names',
664-
description: 'Full names description',
665-
required: [],
666-
},
639+
let uiSchema = {
640+
'ui:order': ['newInput1'],
641+
newInput1: {
642+
'ui:column': '3',
667643
},
668-
dependencies: {},
669-
required: [],
670-
type: 'object',
671644
};
672645

673646
const innerProps = {
674647
...props,
675-
schema: JSON.stringify(sectionJsonSchema),
676-
uiSchema: JSON.stringify(sectionUiSchema),
648+
649+
schema: JSON.stringify(jsonSchema),
650+
uischema: JSON.stringify(uiSchema),
651+
onChange: (newSchema, newUiSchema) => {
652+
jsonSchema = newSchema;
653+
uiSchema = newUiSchema;
654+
},
677655
};
678656

679657
const div = document.createElement('div');
680658
document.body.appendChild(div);
681659
const wrapper = mount(<FormBuilder {...innerProps} />, { attachTo: div });
660+
const cardInputs = wrapper.find('.card-container').first().find('input');
682661

683-
const subFieldCheckbox = wrapper.find({ type: 'checkbox' });
684-
685-
const firstNamesCheckbox = subFieldCheckbox.at(0);
686-
687-
firstNamesCheckbox.simulate('change', { target: { checked: true } });
662+
cardInputs.at(0).simulate('blur', {
663+
target: { value: 'nameA' },
664+
});
688665

689-
const updatedSchema = JSON.parse(mockEvent.mock.calls[0][0]);
666+
const expected = {
667+
'ui:order': ['nameA'],
668+
nameA: {
669+
'ui:column': '3',
670+
},
671+
};
690672

691-
expect(updatedSchema.properties.user_full_names.required).toEqual([
692-
'first_names',
693-
]);
673+
expect(JSON.parse(uiSchema)).toEqual(expected);
694674
mockEvent.mockClear();
695675
});
696676
});

src/formBuilder/Section.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ export default function Section({
512512
onClose={() => setModalOpen(false)}
513513
onChange={(newComponentProps: { [string]: any }) => {
514514
onDependentsChange(newComponentProps.dependents);
515-
516515
onChange(schema, {
517516
...uischema,
518517
'ui:column': newComponentProps['ui:column'],

src/formBuilder/utils.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,10 @@ export function generateUiSchemaFromElementProps(
839839
uiSchema[element.name][uiOption] = element.uiOptions[uiOption];
840840
}
841841
});
842-
} else if (element.propType === 'section' && element.uischema) {
842+
} else if (
843+
element.propType === 'section' &&
844+
Object.keys(element.uischema).length > 0
845+
) {
843846
uiSchema[element.name] = element.uischema;
844847
}
845848
});
@@ -871,16 +874,29 @@ export function updateSchemas(
871874
},
872875
) {
873876
const { schema, uischema, onChange, definitionUi } = parameters;
874-
875-
const newUiSchema = Object.assign(
876-
{ ...uischema },
877-
generateUiSchemaFromElementProps(elementArr, definitionUi),
878-
);
879877
const newSchema = Object.assign(
880878
{ ...schema },
881879
generateSchemaFromElementProps(elementArr),
882880
);
883881

882+
const existingUiSchema: {
883+
[string]: any,
884+
definitions?: { [string]: any },
885+
...
886+
} = Object.entries(uischema)
887+
.filter(([key, _value]) => {
888+
return key in newSchema.properties || key.startsWith('ui:');
889+
})
890+
.reduce((accumulator, currentValue) => {
891+
const [key, value] = currentValue;
892+
return { ...accumulator, [key]: value };
893+
}, {});
894+
895+
const newUiSchema = Object.assign(
896+
{ ...existingUiSchema },
897+
generateUiSchemaFromElementProps(elementArr, definitionUi),
898+
);
899+
884900
// mandate that the type is an object if not already done
885901
newSchema.type = 'object';
886902
onChange(newSchema, newUiSchema);

0 commit comments

Comments
 (0)