Skip to content

Commit

Permalink
Merge branch 'v5'
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed May 20, 2022
2 parents c96b213 + 99b8057 commit a44f2a5
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 50 deletions.
67 changes: 67 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,73 @@ All notable changes to this project will be documented in this file. See [standa
* **material:** removed double tag closing in datepicker type ([#2327](https://github.com/ngx-formly/ngx-formly/issues/2327)) ([7969ac9](https://github.com/ngx-formly/ngx-formly/commit/7969ac913420e4b7b829c230f33e9ac44a8f4236))
* sync v5 branch ([2acc4ea](https://github.com/ngx-formly/ngx-formly/commit/2acc4eaccb481df6f2931744a9a5cf00fbf2abd1))

<a name="5.12.2"></a>
## [5.12.2](https://github.com/ngx-formly/ngx-formly/compare/v5.12.1...v5.12.2) (2022-05-19)


### Performance Improvements

* **core:** avoid rebuild all fields on FieldArray update ([#3301](https://github.com/ngx-formly/ngx-formly/issues/3301)) ([2f17e73](https://github.com/ngx-formly/ngx-formly/commit/2f17e73))



<a name="5.12.1"></a>
## [5.12.1](https://github.com/ngx-formly/ngx-formly/compare/v5.12.0...v5.12.1) (2022-04-22)


### Bug Fixes

* **core:** emit valueChanges when fieldArray length is decreasing ([#3279](https://github.com/ngx-formly/ngx-formly/issues/3279)) ([169bc62](https://github.com/ngx-formly/ngx-formly/commit/169bc62)), closes [#3250](https://github.com/ngx-formly/ngx-formly/issues/3250)
* **core:** improve eval array notation in expressions ([#3278](https://github.com/ngx-formly/ngx-formly/issues/3278)) ([17c0bd6](https://github.com/ngx-formly/ngx-formly/commit/17c0bd6)), closes [#3264](https://github.com/ngx-formly/ngx-formly/issues/3264)



<a name="5.12.0"></a>
# [5.12.0](https://github.com/ngx-formly/ngx-formly/compare/v5.11.2...v5.12.0) (2022-03-30)


### Bug Fixes

* **json-schema:** improve oneOf selection of mixed type ([#3248](https://github.com/ngx-formly/ngx-formly/issues/3248)) ([39d41a3](https://github.com/ngx-formly/ngx-formly/commit/39d41a3)), closes [#3245](https://github.com/ngx-formly/ngx-formly/issues/3245)


### Features

* **core:** export FormlySelectOptionsPipe ([#3249](https://github.com/ngx-formly/ngx-formly/issues/3249)) ([e93ccff](https://github.com/ngx-formly/ngx-formly/commit/e93ccff)), closes [#3236](https://github.com/ngx-formly/ngx-formly/issues/3236)
* **json-schema:** support oneOf for non-object type ([#3152](https://github.com/ngx-formly/ngx-formly/issues/3152)) ([f3a7e70](https://github.com/ngx-formly/ngx-formly/commit/f3a7e70)), closes [#2528](https://github.com/ngx-formly/ngx-formly/issues/2528)



<a name="5.11.2"></a>
## [5.11.2](https://github.com/ngx-formly/ngx-formly/compare/v5.11.1...v5.11.2) (2022-03-16)


### Bug Fixes

* **core:** allow override typing for field property in FieldArrayType ([#3230](https://github.com/ngx-formly/ngx-formly/issues/3230)) ([3aa5537](https://github.com/ngx-formly/ngx-formly/commit/3aa5537)), closes [#3218](https://github.com/ngx-formly/ngx-formly/issues/3218)



<a name="5.11.1"></a>
## [5.11.1](https://github.com/ngx-formly/ngx-formly/compare/v5.11.0...v5.11.1) (2022-03-13)


### Bug Fixes

* **core:** mark defaultOptions as optional ([c71d799](https://github.com/ngx-formly/ngx-formly/commit/c71d799))



<a name="5.11.0"></a>
# [5.11.0](https://github.com/ngx-formly/ngx-formly/compare/v5.10.33...v5.11.0) (2022-03-13)


### Features

* **core:** add support of strictTemplates in FieldType ([#3218](https://github.com/ngx-formly/ngx-formly/issues/3218)) ([99e7e2b](https://github.com/ngx-formly/ngx-formly/commit/99e7e2b))



<a name="5.10.33"></a>
## [5.10.33](https://github.com/ngx-formly/ngx-formly/compare/v5.10.32...v5.10.33) (2022-02-22)

Expand Down
43 changes: 43 additions & 0 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,49 @@ describe('Service: FormlyJsonschema', () => {
expect(foo2Field.hide).toBeFalse();
});

it('should support oneOf using mixed type', () => {
const { field } = renderComponent({
model: { foo: [] },
schema: {
type: 'object',
oneOf: [{ properties: { foo: { type: 'object' } } }, { properties: { foo: { type: 'array' } } }],
},
});

const [
,
{
fieldGroup: [foo1Field, foo2Field],
},
] = field.fieldGroup[0].fieldGroup;

expect(foo1Field.hide).toBeTruthy();
expect(foo2Field.hide).toBeFalsy();
});

it('should support oneOf for a non-object type', () => {
const { field } = renderComponent({
model: { foo: 2 },
schema: {
type: 'object',
properties: {
foo: {
oneOf: [{ type: 'string' }, { type: 'integer' }],
},
},
},
});

const [
,
{
fieldGroup: [foo1Field, foo2Field],
},
] = field.fieldGroup[0].fieldGroup[0].fieldGroup;
expect(foo1Field.hide).toBeTruthy();
expect(foo2Field.hide).toBeFalsy();
});

it('should support nested oneOf', () => {
const { field } = renderComponent({
model: { foo: 2 },
Expand Down
75 changes: 64 additions & 11 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { JSONSchema7, JSONSchema7Definition } from 'json-schema';
import { AbstractControl, FormGroup } from '@angular/forms';
import { AbstractControl, FormArray, FormGroup } from '@angular/forms';
import {
ɵreverseDeepMerge as reverseDeepMerge,
ɵgetFieldValue as getFieldValue,
Expand Down Expand Up @@ -61,14 +61,25 @@ function totalMatchedFields(field: FormlyFieldConfig): number {
}

const total = field.fieldGroup.reduce((s, f) => totalMatchedFields(f) + s, 0);
return total === 0 ? (field.key && getFieldValue(field) !== undefined ? 1 : 0) : total;
if (total === 0 && hasKey(field)) {
const value = getFieldValue(field);
if (
value === null ||
(value !== undefined && ((field.fieldArray && Array.isArray(value)) || (!field.fieldArray && isObject(value))))
) {
return 1;
}
}

return total;
}

interface IOptions extends FormlyJsonschemaOptions {
schema: FormlyJSONSchema7;
resetOnHide?: boolean;
shareFormControl?: boolean;
ignoreDefault?: boolean;
strict?: boolean;
readOnly?: boolean;
key?: FormlyFieldConfig['key'];
}
Expand Down Expand Up @@ -106,6 +117,33 @@ export class FormlyJsonschema {
field.resetOnHide = true;
}

if (key && options.strict) {
this.addValidator(field, 'type', (c: AbstractControl, f: FormlyFieldConfig) => {
const value = getFieldValue(f);
if (value != null) {
switch (field.type) {
case 'string': {
return typeof value === 'string';
}
case 'integer': {
return isInteger(value);
}
case 'number': {
return typeof value === 'number';
}
case 'object': {
return isObject(value);
}
case 'array': {
return Array.isArray(value);
}
}
}

return true;
});
}

if (options.shareFormControl === false) {
field.shareFormControl = false;
}
Expand Down Expand Up @@ -379,6 +417,13 @@ export class FormlyJsonschema {
];
}

if (schema.oneOf && !field.type) {
delete field.key;
field.fieldGroup = [
this.resolveMultiSchema('oneOf', <JSONSchema7[]>schema.oneOf, { ...options, key, shareFormControl: false }),
];
}

// map in possible formlyConfig options from the widget property
if (schema.widget?.formlyConfig) {
field = this.mergeFields(field, schema.widget.formlyConfig);
Expand Down Expand Up @@ -462,19 +507,23 @@ export class FormlyJsonschema {
const control = f.parent.parent.fieldGroup[0].formControl;
if (control.value === -1 || forceUpdate) {
let value = f.parent.fieldGroup
.map((f, i) => [f, i] as [FormlyFieldConfig, number])
.filter(([f, i]) => {
return this.isFieldValid(f, schemas[i], options);
})
.sort(([f1], [f2]) => {
.map(
(f, i) =>
[f, i, this.isFieldValid(f, schemas[i], options)] as [FormlyFieldConfig, number, boolean],
)
.sort(([f1, , f1Valid], [f2, , f2Valid]) => {
if (f1Valid !== f2Valid) {
return f2Valid ? 1 : -1;
}

const matchedFields1 = totalMatchedFields(f1);
const matchedFields2 = totalMatchedFields(f2);
if (matchedFields1 === matchedFields2) {
if (f1.props.disabled === f2.props.disabled) {
return 0;
}

return f1.props.disabled ? 1 : -1;
return matchedFields2 > matchedFields1 ? 1 : -1;
}

return matchedFields2 > matchedFields1 ? 1 : -1;
Expand Down Expand Up @@ -617,10 +666,14 @@ export class FormlyJsonschema {
}

private isFieldValid(field: FormlyFieldConfig, schema: JSONSchema7, options: IOptions): boolean {
const model = field.model ? clone(field.model) : field.fieldArray ? [] : {};
const { form } = field.options.build({
form: new FormGroup({}),
fieldGroup: [this._toFieldConfig(schema, { ...options, resetOnHide: true, ignoreDefault: true, map: null })],
model: field.model ? clone(field.model) : field.fieldArray ? [] : {},
form: Array.isArray(model) ? new FormArray([]) : new FormGroup({}),
fieldGroup: [
this._toFieldConfig(schema, { ...options, resetOnHide: true, ignoreDefault: true, map: null, strict: true }),
],
model,
options: {},
});

return form.valid;
Expand Down
2 changes: 1 addition & 1 deletion src/core/select/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { FormlySelectModule } from './select.module';
export { FormlySelectOptionsPipe as ɵFormlySelectOptionsPipe, FormlySelectOption } from './select-options.pipe';
export { FormlySelectOptionsPipe, FormlySelectOption } from './select-options.pipe';
4 changes: 2 additions & 2 deletions src/core/src/lib/components/formly.form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type IFormlyFormInputs = Partial<{
modelChange: (m: any) => void;
}>;

const renderComponent = (inputs: IFormlyFormInputs, config: any = {}) => {
export const renderComponent = (inputs: IFormlyFormInputs, config: any = {}) => {
inputs = {
form: new FormGroup({}),
model: {},
Expand Down Expand Up @@ -483,7 +483,7 @@ describe('FormlyForm Component', () => {
{ extras: { immutable: true } },
);

let titleField;
let titleField: FormlyFieldConfig;
setInputs({
model: { title: 'foo' },
fields: [{ key: 'title', hooks: { onInit: (f) => (titleField = f) } }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,19 @@ describe('FieldExpressionExtension', () => {
it('should supports array notation in expression property', () => {
const field = buildField({
model: [],
props: {
options: [
{ label: 'bar', value: 'bar' },
{ label: 'baz', value: 'baz' },
],
},
expressions: {
'model[0]': '1',
'model["1"]': '2',
"model['2']": '3',
"props['prop1']": '1',
'props["prop.2"]': '2',
'props.options[1].label': '"123"',
},
});

Expand Down

0 comments on commit a44f2a5

Please sign in to comment.