Skip to content

Commit

Permalink
fix(json-schema): take acocunt of total matched oneOf props (#2196)
Browse files Browse the repository at this point in the history
fix #2195
  • Loading branch information
aitboudad committed Apr 22, 2020
1 parent 3234118 commit 9367216
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Expand Up @@ -971,6 +971,27 @@ describe('Service: FormlyJsonschema', () => {
(f.options as any)._checkField(f.parent);
expect(model).toEqual({ bar: 'bar' });
});

it('should render the selected oneOf field (with more matched fields)', () => {
const { fieldGroup: [f] } = formlyJsonschema.toFieldConfig({
type: 'object',
oneOf: [
{ properties: { foo1: { type: 'string' } } },
{
properties: {
foo1: { type: 'string' },
bar: { type: 'string' },
},
},
],
});
const model: any = { foo1: 'test', bar: 'test' };
builder.buildForm(new FormGroup({}), [f], model, { _initialModel: { foo1: 'test', bar: 'test' } } as any);
const [, { fieldGroup: [f1, f2] }] = f.fieldGroup;

expect(f1.hide).toBeTruthy();
expect(f2.hide).toBeFalsy();
});
});

describe('anyOf', () => {
Expand Down
13 changes: 7 additions & 6 deletions src/core/json-schema/src/formly-json-schema.service.ts
Expand Up @@ -24,12 +24,12 @@ function isConst(schema: JSONSchema7) {
return schema.hasOwnProperty('const') || (schema.enum && schema.enum.length === 1);
}

function isEmptyFieldModel(field: FormlyFieldConfig): boolean {
function totalMatchedFields(field: FormlyFieldConfig): number {
if (field.key && !field.fieldGroup) {
return getFieldInitialValue(field) === undefined;
return getFieldInitialValue(field) !== undefined ? 1 : 0;
}

return field.fieldGroup.every(f => isEmptyFieldModel(f));
return field.fieldGroup.reduce((s, f) => totalMatchedFields(f) + s, 0);
}

function isFieldValid(field: FormlyFieldConfig): boolean {
Expand Down Expand Up @@ -328,12 +328,13 @@ export class FormlyJsonschema {
.map((f, i) => [f, i] as [FormlyFieldConfig, number])
.filter(([f]) => isFieldValid(f))
.sort(([f1], [f2]) => {
const isDefaultModel = isEmptyFieldModel(f1);
if (isDefaultModel === isEmptyFieldModel(f2)) {
const matchedFields1 = totalMatchedFields(f1);
const matchedFields2 = totalMatchedFields(f2);
if (matchedFields1 === matchedFields2) {
return 0;
}

return isDefaultModel ? 1 : -1;
return matchedFields2 > matchedFields1 ? 1 : -1;
})
.map(([, i]) => i)
;
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/lib/components/formly.field.spec.ts
Expand Up @@ -336,7 +336,7 @@ describe('FormlyField Component', () => {
expect(getInputField(fixture.nativeElement).getAttribute('placeholder')).toEqual('Title');
});

fit('should update template options of OnPush FieldType #2191', async () => {
it('should update template options of OnPush FieldType #2191', async () => {
const options$ = timer(0).pipe(map(() => [{ value: 5, label: 'Option 5' }]), shareReplay(1));

const field: FormlyFieldConfig = {
Expand Down

0 comments on commit 9367216

Please sign in to comment.