Skip to content

Commit 838edfd

Browse files
authored
Fix: Unable to mark inner fields of a referenced section as required. (#197)
1 parent ac301ac commit 838edfd

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/formBuilder/FormBuilder.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,74 @@ describe('FormBuilder', () => {
623623
);
624624
mockEvent.mockClear();
625625
});
626+
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'],
632+
},
633+
},
634+
user_full_names: {
635+
'ui:order': ['first_names', 'last_names'],
636+
},
637+
'ui:order': ['user_full_names'],
638+
};
639+
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+
},
667+
},
668+
dependencies: {},
669+
required: [],
670+
type: 'object',
671+
};
672+
673+
const innerProps = {
674+
...props,
675+
schema: JSON.stringify(sectionJsonSchema),
676+
uiSchema: JSON.stringify(sectionUiSchema),
677+
};
678+
679+
const div = document.createElement('div');
680+
document.body.appendChild(div);
681+
const wrapper = mount(<FormBuilder {...innerProps} />, { attachTo: div });
682+
683+
const subFieldCheckbox = wrapper.find({ type: 'checkbox' });
684+
685+
const firstNamesCheckbox = subFieldCheckbox.at(0);
686+
687+
firstNamesCheckbox.simulate('change', { target: { checked: true } });
688+
689+
const updatedSchema = JSON.parse(mockEvent.mock.calls[0][0]);
690+
691+
expect(updatedSchema.properties.user_full_names.required).toEqual([
692+
'first_names',
693+
]);
694+
mockEvent.mockClear();
695+
});
626696
});

src/formBuilder/utils.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,18 @@ function generateSchemaElementFromElement(element: ElementProps) {
691691
element.schema !== undefined && element.schema.description !== undefined
692692
? element.schema.description
693693
: element.dataOptions.description;
694-
return {
694+
695+
let returnElement = {
695696
$ref: element.$ref,
696697
title: title,
697698
description: description,
698699
};
700+
701+
const length = element?.schema?.required?.length;
702+
if (length !== undefined && length > 0) {
703+
returnElement = { ...returnElement, required: element.schema.required };
704+
}
705+
return returnElement;
699706
} else if (element.propType === 'card') {
700707
if (element.dataOptions.category === 'section') {
701708
return {

src/formBuilder/utils.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,36 @@ describe('generateSchemaFromElementProps', () => {
527527

528528
expect(result.properties.exampleCard).toEqual(expectedSchemaElement);
529529
});
530+
531+
it('generates the correct JSON Schema from a compound element with the required prop', () => {
532+
const expectedSchemaElement = {
533+
$ref: '#/definitions/someDefinition',
534+
title: 'Input Field',
535+
description: 'This is an example description',
536+
required: ['field_one'],
537+
};
538+
539+
const result = generateSchemaFromElementProps(
540+
[
541+
{
542+
name: 'exampleCard',
543+
required: true,
544+
$ref: '#/definitions/someDefinition',
545+
dataOptions: {
546+
description: 'This is an example description',
547+
title: 'Input Field',
548+
},
549+
schema: {
550+
required: ['field_one'],
551+
},
552+
propType: 'card',
553+
},
554+
],
555+
DEFAULT_FORM_INPUTS,
556+
);
557+
558+
expect(result.properties.exampleCard).toEqual(expectedSchemaElement);
559+
});
530560
});
531561

532562
describe('generateUiSchemaFromElementProps', () => {

0 commit comments

Comments
 (0)