Skip to content

Commit

Permalink
fix(json-schema): allow adjusting field Array before build step (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed Nov 12, 2019
1 parent b00fb0d commit 3ae0e8a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
7 changes: 3 additions & 4 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ describe('Service: FormlyJsonschema', () => {
defaultValue: undefined,
templateOptions: { ...emmptyTemplateOptions },
fieldArray: childConfig,
fieldGroup: [],
};

expect(config).toEqual(baseConfig);
Expand All @@ -184,10 +183,11 @@ describe('Service: FormlyJsonschema', () => {

expect(config.type).toEqual('array');
expect(config.fieldArray).toEqual(childConfig);
expect(config.fieldGroup).toBeUndefined();
// TODO: is this the best way to test this?
// artificially increase the length of the fieldGroup
// since the getter that is defined is based on that.
config.fieldGroup.push(null);
config.fieldGroup = [null];
expect(config.fieldArray).toEqual(childConfig2);
config.fieldGroup.push(null);
expect(config.fieldArray).toEqual({});
Expand All @@ -213,7 +213,7 @@ describe('Service: FormlyJsonschema', () => {
// TODO: is this the best way to test this?
// artificially increase the length of the fieldGroup
// since the getter that is defined is based on that.
config.fieldGroup.push(null);
config.fieldGroup = [null];
expect(config.fieldArray).toEqual(childConfig2);
config.fieldGroup.push(null);
// should return the additional items schema when the fieldGroup's length
Expand Down Expand Up @@ -635,7 +635,6 @@ describe('Service: FormlyJsonschema', () => {
type: 'array',
defaultValue: undefined,
templateOptions: emmptyTemplateOptions,
fieldGroup: [],
fieldArray: jasmine.any(Object) as any,
};

Expand Down
6 changes: 3 additions & 3 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ export class FormlyJsonschema {

// TODO: remove isEnum check once adding an option to skip extension
if (!this.isEnum(schema)) {
field.fieldGroup = [];
Object.defineProperty(field, 'fieldArray', {
get: () => {
if (!Array.isArray(schema.items)) {
// When items is a single schema, the additionalItems keyword is meaningless, and it should not be used.
return this._toFieldConfig(<JSONSchema7> schema.items, options);
}

const itemSchema = schema.items[field.fieldGroup.length]
? schema.items[field.fieldGroup.length]
const length = field.fieldGroup ? field.fieldGroup.length : 0;
const itemSchema = schema.items[length]
? schema.items[length]
: schema.additionalItems;

return itemSchema
Expand Down
5 changes: 0 additions & 5 deletions src/core/src/lib/extensions/field-form/field-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import { registerControl } from './utils';
/** @experimental */
export class FieldFormExtension implements FormlyExtension {
onPopulate(field: FormlyFieldConfigCache) {
// TODO: add an option to skip extension
if (field.fieldArray) {
return;
}

if (field.key) {
this.addFormControl(field);
}
Expand Down
17 changes: 5 additions & 12 deletions src/core/src/lib/templates/field-array.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FormArray } from '@angular/forms';
import { FieldType } from './field.type';
import { clone, isNullOrUndefined, assignModelValue, getKeyPath } from '../utils';
import { FormlyFormBuilder } from '../services/formly.form.builder';
import { FormlyFieldConfig, FormlyFieldConfigCache } from '../components/formly.field.config';
import { FormlyFieldConfig } from '../components/formly.field.config';
import { FORMLY_CONFIG, FormlyExtension } from '../services/formly.config';
import { registerControl, unregisterControl } from '../extensions/field-form/utils';

Expand All @@ -22,6 +22,10 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FormlyFieldCo
}

onPopulate(field: FormlyFieldConfig) {
if (!field.formControl) {
registerControl(field, new FormArray([], { updateOn: field.modelOptions.updateOn }));
}

field.fieldGroup = field.fieldGroup || [];

const length = field.model ? field.model.length : 0;
Expand All @@ -38,17 +42,6 @@ export abstract class FieldArrayType<F extends FormlyFieldConfig = FormlyFieldCo
}
}

postPopulate(field: FormlyFieldConfigCache) {
if (field.formControl) {
return;
}

registerControl(field, new FormArray(
field.fieldGroup.map(f => f.formControl),
{ updateOn: field.modelOptions.updateOn },
));
}

add(i?: number, initialModel?: any) {
i = isNullOrUndefined(i) ? this.field.fieldGroup.length : i;
if (!this.model) {
Expand Down

0 comments on commit 3ae0e8a

Please sign in to comment.