From 4973d996fc7f7bf0b9761dd89d90c6793cf4d908 Mon Sep 17 00:00:00 2001 From: Andrei Lukashenka Date: Tue, 17 Dec 2024 15:36:15 -0800 Subject: [PATCH] fix: ensure compatibility with object keys minification --- .../src/formly-json-schema.service.ts | 16 ++++++++-------- src/core/src/lib/components/formly.field.ts | 8 ++++---- src/core/src/lib/components/formly.form.ts | 8 ++++++-- src/core/src/lib/extensions/core/core.ts | 2 +- .../field-expression/field-expression.ts | 8 ++++++-- src/core/src/lib/models/config.ts | 8 ++++++-- src/core/src/lib/models/fieldconfig.cache.ts | 11 ++++++++--- src/core/src/lib/models/fieldconfig.ts | 4 ++-- src/core/src/lib/services/formly.builder.ts | 2 +- src/core/src/lib/services/formly.config.ts | 3 ++- .../src/lib/templates/field-template.type.ts | 2 +- src/core/src/lib/templates/formly.attributes.ts | 8 ++++---- src/core/src/lib/utils.ts | 2 +- 13 files changed, 50 insertions(+), 32 deletions(-) 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 b914179ee..9b098b5f3 100644 --- a/src/core/json-schema/src/formly-json-schema.service.ts +++ b/src/core/json-schema/src/formly-json-schema.service.ts @@ -235,7 +235,7 @@ export class FormlyJsonschema { } if (schema.hasOwnProperty('exclusiveMinimum')) { - field.props.exclusiveMinimum = schema.exclusiveMinimum; + field.props['exclusiveMinimum'] = schema.exclusiveMinimum; this.addValidator( field, 'exclusiveMinimum', @@ -244,7 +244,7 @@ export class FormlyJsonschema { } if (schema.hasOwnProperty('exclusiveMaximum')) { - field.props.exclusiveMaximum = schema.exclusiveMaximum; + field.props['exclusiveMaximum'] = schema.exclusiveMaximum; this.addValidator( field, 'exclusiveMaximum', @@ -371,7 +371,7 @@ export class FormlyJsonschema { } case 'array': { if (schema.hasOwnProperty('minItems')) { - field.props.minItems = schema.minItems; + field.props['minItems'] = schema.minItems; this.addValidator(field, 'minItems', ({ value }: AbstractControl) => { return isEmpty(value) || value.length >= schema.minItems; }); @@ -380,13 +380,13 @@ export class FormlyJsonschema { } } if (schema.hasOwnProperty('maxItems')) { - field.props.maxItems = schema.maxItems; + field.props['maxItems'] = schema.maxItems; this.addValidator(field, 'maxItems', ({ value }: AbstractControl) => { return isEmpty(value) || value.length <= schema.maxItems; }); } if (schema.hasOwnProperty('uniqueItems')) { - field.props.uniqueItems = schema.uniqueItems; + field.props['uniqueItems'] = schema.uniqueItems; this.addValidator(field, 'uniqueItems', ({ value }: AbstractControl) => { if (isEmpty(value) || !schema.uniqueItems) { return true; @@ -451,7 +451,7 @@ export class FormlyJsonschema { f.props.required = true; } if (items[length]) { - f.props.removable = false; + f.props['removable'] = false; } return f; @@ -462,7 +462,7 @@ export class FormlyJsonschema { } if (schema.hasOwnProperty('const')) { - field.props.const = schema.const; + field.props['const'] = schema.const; this.addValidator(field, 'const', ({ value }: AbstractControl) => value === schema.const); if (!field.type) { field.defaultValue = schema.const; @@ -474,7 +474,7 @@ export class FormlyJsonschema { const multiple = field.type === 'array'; field.type = 'enum'; - field.props.multiple = multiple; + field.props['multiple'] = multiple; field.props.options = enumOptions; const enumValues = enumOptions.map((o) => o.value); diff --git a/src/core/src/lib/components/formly.field.ts b/src/core/src/lib/components/formly.field.ts index cc04abb0b..20cb43241 100644 --- a/src/core/src/lib/components/formly.field.ts +++ b/src/core/src/lib/components/formly.field.ts @@ -161,13 +161,13 @@ export class FormlyField implements DoCheck, OnInit, OnChanges, AfterContentInit } private triggerHook(name: keyof FormlyHookConfig, changes?: SimpleChanges) { - if (name === 'onInit' || (name === 'onChanges' && changes.field && !changes.field.firstChange)) { + if (name === 'onInit' || (name === 'onChanges' && changes['field'] && !changes['field'].firstChange)) { this.valueChangesUnsubscribe(); this.valueChangesUnsubscribe = this.fieldChanges(this.field); } if (this.field?.hooks?.[name]) { - if (!changes || changes.field) { + if (!changes || changes['field']) { const r = this.field.hooks[name](this.field); if (isObservable(r) && ['onInit', 'afterContentInit', 'afterViewInit'].indexOf(name) !== -1) { const sub = r.subscribe(); @@ -176,8 +176,8 @@ export class FormlyField implements DoCheck, OnInit, OnChanges, AfterContentInit } } - if (name === 'onChanges' && changes.field) { - this.resetRefs(changes.field.previousValue); + if (name === 'onChanges' && changes['field']) { + this.resetRefs(changes['field'].previousValue); this.render(); } } diff --git a/src/core/src/lib/components/formly.form.ts b/src/core/src/lib/components/formly.form.ts index 48b932e97..df478e0c2 100644 --- a/src/core/src/lib/components/formly.form.ts +++ b/src/core/src/lib/components/formly.form.ts @@ -98,11 +98,15 @@ export class FormlyForm implements DoCheck, OnChanges, OnDestroy { } ngOnChanges(changes: SimpleChanges) { - if (changes.fields && this.form) { + if (changes['fields'] && this.form) { clearControl(this.form); } - if (changes.fields || changes.form || (changes.model && this._modelChangeValue !== changes.model.currentValue)) { + if ( + changes['fields'] || + changes['form'] || + (changes['model'] && this._modelChangeValue !== changes['model'].currentValue) + ) { this.valueChangesUnsubscribe(); this.builder.build(this.field); this.valueChangesUnsubscribe = this.valueChanges(); diff --git a/src/core/src/lib/extensions/core/core.ts b/src/core/src/lib/extensions/core/core.ts index a7d42dc89..6e774e3a6 100644 --- a/src/core/src/lib/extensions/core/core.ts +++ b/src/core/src/lib/extensions/core/core.ts @@ -146,7 +146,7 @@ export class CoreExtension implements FormlyExtension { if ( field.type !== 'formly-template' && - (field.template || field.expressions?.template || field.expressionProperties?.template) + (field.template || field.expressions?.['template'] || field.expressionProperties?.['template']) ) { field.type = 'formly-template'; } diff --git a/src/core/src/lib/extensions/field-expression/field-expression.ts b/src/core/src/lib/extensions/field-expression/field-expression.ts index a3e1555ef..aa70c3d1a 100644 --- a/src/core/src/lib/extensions/field-expression/field-expression.ts +++ b/src/core/src/lib/extensions/field-expression/field-expression.ts @@ -36,7 +36,11 @@ export class FieldExpressionExtension implements FormlyExtension { if (field.hideExpression) { observe(field, ['hideExpression'], ({ currentValue: expr }) => { - field._expressions.hide = this.parseExpressions(field, 'hide', typeof expr === 'boolean' ? () => expr : expr); + field._expressions['hide'] = this.parseExpressions( + field, + 'hide', + typeof expr === 'boolean' ? () => expr : expr, + ); }); } @@ -185,7 +189,7 @@ export class FieldExpressionExtension implements FormlyExtension { private changeHideState(field: FormlyFieldConfigCache, hide: boolean, resetOnHide: boolean) { if (field.fieldGroup) { field.fieldGroup - .filter((f: FormlyFieldConfigCache) => f && !f._expressions.hide) + .filter((f: FormlyFieldConfigCache) => f && !f._expressions['hide']) .forEach((f) => this.changeHideState(f, hide, resetOnHide)); } diff --git a/src/core/src/lib/models/config.ts b/src/core/src/lib/models/config.ts index b74daa365..db584a85e 100644 --- a/src/core/src/lib/models/config.ts +++ b/src/core/src/lib/models/config.ts @@ -1,10 +1,14 @@ -import { Type } from '@angular/core'; +import { ComponentRef, Type } from '@angular/core'; import { ValidationErrors, AbstractControl } from '@angular/forms'; import { Observable } from 'rxjs'; import { FormlyFieldConfig } from './fieldconfig'; import { FieldType } from './../templates/field.type'; import { FieldWrapper } from '../templates/field.wrapper'; +export declare interface FormlyFieldComponentRef { + _componentRef?: ComponentRef; +} + export interface FormlyExtension { priority?: number; @@ -13,7 +17,7 @@ export interface FormlyExtension; wrappers?: string[]; diff --git a/src/core/src/lib/models/fieldconfig.cache.ts b/src/core/src/lib/models/fieldconfig.cache.ts index 24ad907d9..ea89a2a6d 100644 --- a/src/core/src/lib/models/fieldconfig.cache.ts +++ b/src/core/src/lib/models/fieldconfig.cache.ts @@ -5,10 +5,15 @@ import { FieldType } from '../templates/field.type'; import { FormlyExtension } from './config'; import { FormlyFieldConfig, FormlyFormOptions } from './fieldconfig'; -export interface FormlyFieldConfigCache extends FormlyFieldConfig { +export declare interface FieldConfigWithErrors { + _fields?: FormlyFieldConfigCache[]; + _childrenErrors?: { [id: string]: Function }; +} + +export declare interface FormlyFieldConfigCache extends FormlyFieldConfig { form?: FormGroup | FormArray; model?: any; - formControl?: AbstractControl & { _fields?: FormlyFieldConfigCache[]; _childrenErrors?: { [id: string]: Function } }; + formControl?: AbstractControl & FieldConfigWithErrors; parent?: FormlyFieldConfigCache; options?: FormlyFormOptionsCache; shareFormControl?: boolean; @@ -34,7 +39,7 @@ export interface FormlyFieldConfigCache extends FormlyFieldConfig { }; } -export interface FormlyFormOptionsCache extends FormlyFormOptions { +export declare interface FormlyFormOptionsCache extends FormlyFormOptions { checkExpressions?: (field: FormlyFieldConfig, ingoreCache?: boolean) => void; _viewContainerRef?: ViewContainerRef; _injector?: Injector; diff --git a/src/core/src/lib/models/fieldconfig.ts b/src/core/src/lib/models/fieldconfig.ts index 29347fc50..f1382aa7a 100644 --- a/src/core/src/lib/models/fieldconfig.ts +++ b/src/core/src/lib/models/fieldconfig.ts @@ -20,7 +20,7 @@ type FieldExpressions = { [property: string]: FieldExpression } & { 'props.required'?: FieldExpression; }; -export interface FormlyFieldConfig { +export declare interface FormlyFieldConfig { /** * The key that relates to the model. This will link the field value to the model */ @@ -256,7 +256,7 @@ export interface FormlyHookConfig { onDestroy?: FormlyHookFn; } -export interface FormlyFormOptions { +export declare interface FormlyFormOptions { updateInitialValue?: (model?: any) => void; resetModel?: (model?: any) => void; formState?: any; diff --git a/src/core/src/lib/services/formly.builder.ts b/src/core/src/lib/services/formly.builder.ts index 60bbc7741..423fb2bee 100644 --- a/src/core/src/lib/services/formly.builder.ts +++ b/src/core/src/lib/services/formly.builder.ts @@ -25,7 +25,7 @@ export class FormlyFormBuilder { } build(field: FormlyFieldConfig) { - if (!this.config.extensions.core) { + if (!this.config.extensions['core']) { throw new Error('NgxFormly: missing `forRoot()` call. use `forRoot()` when registering the `FormlyModule`.'); } diff --git a/src/core/src/lib/services/formly.config.ts b/src/core/src/lib/services/formly.config.ts index 4db34ffc3..03f8736fe 100644 --- a/src/core/src/lib/services/formly.config.ts +++ b/src/core/src/lib/services/formly.config.ts @@ -9,6 +9,7 @@ import { ValidatorOption, WrapperOption, FormlyExtension, + FormlyFieldComponentRef, ValidationMessageOption, ExtensionOption, FormlyFieldConfigPresetProvider, @@ -152,7 +153,7 @@ export class FormlyConfig { /** @ignore @internal */ resolveFieldTypeRef(field: FormlyFieldConfigCache = {}): ComponentRef { - const type: TypeOption & { _componentRef?: ComponentRef } = this.getType(field.type); + const type: TypeOption & FormlyFieldComponentRef = this.getType(field.type); if (!type) { return null; } diff --git a/src/core/src/lib/templates/field-template.type.ts b/src/core/src/lib/templates/field-template.type.ts index ee0b51985..ed407b46f 100644 --- a/src/core/src/lib/templates/field-template.type.ts +++ b/src/core/src/lib/templates/field-template.type.ts @@ -13,7 +13,7 @@ export class FormlyTemplateType extends FieldType { if (this.field && this.field.template !== this.innerHtml.template) { this.innerHtml = { template: this.field.template, - content: this.props.safeHtml + content: this.props['safeHtml'] ? this.sanitizer.bypassSecurityTrustHtml(this.field.template) : this.field.template, }; diff --git a/src/core/src/lib/templates/formly.attributes.ts b/src/core/src/lib/templates/formly.attributes.ts index f2fac0984..7ab666d01 100644 --- a/src/core/src/lib/templates/formly.attributes.ts +++ b/src/core/src/lib/templates/formly.attributes.ts @@ -67,7 +67,7 @@ export class FormlyAttributes implements OnChanges, DoCheck, OnDestroy { } ngOnChanges(changes: SimpleChanges) { - if (changes.field) { + if (changes['field']) { this.field.name && this.setAttribute('name', this.field.name); this.uiEvents.listeners.forEach((listener) => listener()); this.uiEvents.events.forEach((eventName) => { @@ -94,8 +94,8 @@ export class FormlyAttributes implements OnChanges, DoCheck, OnDestroy { }); } - this.detachElementRef(changes.field.previousValue); - this.attachElementRef(changes.field.currentValue); + this.detachElementRef(changes['field'].previousValue); + this.attachElementRef(changes['field'].currentValue); if (this.fieldAttrElements.length === 1) { !this.id && this.field.id && this.setAttribute('id', this.field.id); this.focusObserver = observe(this.field, ['focus'], ({ currentValue }) => { @@ -104,7 +104,7 @@ export class FormlyAttributes implements OnChanges, DoCheck, OnDestroy { } } - if (changes.id) { + if (changes['id']) { this.setAttribute('id', this.id); } } diff --git a/src/core/src/lib/utils.ts b/src/core/src/lib/utils.ts index d27bda9c4..3a78c763d 100644 --- a/src/core/src/lib/utils.ts +++ b/src/core/src/lib/utils.ts @@ -227,7 +227,7 @@ export interface IObserver { setValue: (value: T, emitEvent?: boolean) => void; unsubscribe: Function; } -interface IObserveTarget { +declare interface IObserveTarget { [prop: string]: any; _observers?: { [prop: string]: {