diff --git a/src/core/json-schema/src/formly-json-schema.service.spec.ts b/src/core/json-schema/src/formly-json-schema.service.spec.ts index 39d2a196e..9665eda7c 100644 --- a/src/core/json-schema/src/formly-json-schema.service.spec.ts +++ b/src/core/json-schema/src/formly-json-schema.service.spec.ts @@ -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', () => { diff --git a/src/core/json-schema/src/formly-json-schema.service.ts b/src/core/json-schema/src/formly-json-schema.service.ts index cf50c9862..14f68e2d8 100644 --- a/src/core/json-schema/src/formly-json-schema.service.ts +++ b/src/core/json-schema/src/formly-json-schema.service.ts @@ -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 { @@ -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) ; diff --git a/src/core/src/lib/components/formly.field.spec.ts b/src/core/src/lib/components/formly.field.spec.ts index 3962ab6cb..0c5e261b6 100644 --- a/src/core/src/lib/components/formly.field.spec.ts +++ b/src/core/src/lib/components/formly.field.spec.ts @@ -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 = {