diff --git a/core/app/common/src/lib/record/field.model.ts b/core/app/common/src/lib/record/field.model.ts index 11c9d3daa0..fb45f3b029 100644 --- a/core/app/common/src/lib/record/field.model.ts +++ b/core/app/common/src/lib/record/field.model.ts @@ -25,12 +25,15 @@ */ import {SearchCriteriaFieldFilter} from '../views/list/search-criteria.model'; -import {BehaviorSubject, Observable} from 'rxjs'; +import {Observable, Subject} from 'rxjs'; import {AsyncValidatorFn, FormArray, FormControl, ValidatorFn} from '@angular/forms'; import {Record} from './record.model'; import {FieldLogicMap} from '../actions/field-logic-action.model'; import {ObjectMap} from '../types/object-map'; import {ViewMode} from '../views/view.model'; +import {isEqual} from 'lodash-es'; +import {deepClone} from '../utils/object-utils'; + export interface Option { value: string; @@ -151,6 +154,12 @@ export interface AttributeDependency { attribute: string; } +export interface FieldValue { + value?: string; + valueList?: string[]; + valueObject?: any; +} + export interface Field { type: string; value?: string; @@ -176,7 +185,7 @@ export interface Field { itemFormArray?: FormArray; validators?: ValidatorFn[]; asyncValidators?: AsyncValidatorFn[]; - valueSubject?: BehaviorSubject; + valueSubject?: Subject; valueChanges$?: Observable; fieldDependencies?: string[]; attributeDependencies?: AttributeDependency[]; @@ -201,7 +210,7 @@ export class BaseField implements Field { validators?: ValidatorFn[]; asyncValidators?: AsyncValidatorFn[]; attributes?: FieldAttributeMap; - valueSubject?: BehaviorSubject; + valueSubject?: Subject; valueChanges$?: Observable; fieldDependencies: string[] = []; attributeDependencies: AttributeDependency[] = []; @@ -213,7 +222,7 @@ export class BaseField implements Field { protected valueObjectArrayState?: ObjectMap[]; constructor() { - this.valueSubject = new BehaviorSubject({} as FieldValue); + this.valueSubject = new Subject(); this.valueChanges$ = this.valueSubject.asObservable(); } @@ -223,9 +232,7 @@ export class BaseField implements Field { set value(value: string) { const changed = value !== this.valueState; - this.valueState = value; - if (changed) { this.emitValueChanges(); } @@ -236,10 +243,11 @@ export class BaseField implements Field { } set valueList(value: string[]) { - - this.valueListState = value; - - this.emitValueChanges(); + const changed = !isEqual(value, this.valueListState) + this.valueListState = deepClone(value); + if (changed) { + this.emitValueChanges(); + } } get valueObject(): any { @@ -247,8 +255,11 @@ export class BaseField implements Field { } set valueObject(value: any) { - this.valueObjectState = value; - this.emitValueChanges(); + const changed = !isEqual(value, this.valueObjectState) + this.valueObjectState = deepClone(value); + if (changed) { + this.emitValueChanges(); + } } get valueObjectArray(): any { @@ -256,8 +267,11 @@ export class BaseField implements Field { } set valueObjectArray(value: ObjectMap[]) { - this.valueObjectArrayState = value; - this.emitValueChanges(); + const changed = !isEqual(value, this.valueObjectArrayState) + this.valueObjectArrayState = deepClone(value); + if (changed) { + this.emitValueChanges(); + } } protected emitValueChanges() { @@ -268,11 +282,3 @@ export class BaseField implements Field { }) } } - -export interface FieldValue { - value?: string; - valueList?: string[]; - valueObject?: any; -} - - diff --git a/core/app/core/src/lib/fields/base/base-enum.component.ts b/core/app/core/src/lib/fields/base/base-enum.component.ts index cad0652b0c..f0eaf00828 100644 --- a/core/app/core/src/lib/fields/base/base-enum.component.ts +++ b/core/app/core/src/lib/fields/base/base-enum.component.ts @@ -300,23 +300,28 @@ export class BaseEnumComponent extends BaseFieldComponent implements OnInit, OnD } protected subscribeToParentValueChanges(parentEnum: Field): void { - if (parentEnum.formControl) { - this.subs.push(parentEnum.formControl.valueChanges.subscribe(values => { - - if (typeof values === 'string') { - values = [values]; - } - - // Reset selected values on Form Control - this.field.value = ''; - this.field.formControl.setValue(''); + this.subs.push(parentEnum.valueChanges$.subscribe(fieldValue => { + let valueList = []; + if (fieldValue.valueList && fieldValue.valueList.length) { + valueList = fieldValue.valueList; + } else if(parentEnum.value) { + const value = fieldValue.value ?? '' + valueList = [value] + } else { + valueList = []; + } - // Rebuild available enum options - this.options = this.filterMatchingOptions(values); + // Rebuild available enum options + this.options = this.filterMatchingOptions(valueList); + this.setDependentValue(); + this.initValue(); + })); + } - this.initValue(); - })); - } + protected setDependentValue(): void { + // Reset selected values on Form Control + this.field.value = ''; + this.field.formControl.setValue(''); } } diff --git a/core/app/core/src/lib/fields/dropdownenum/templates/edit/dropdownenum.component.html b/core/app/core/src/lib/fields/dropdownenum/templates/edit/dropdownenum.component.html index 8e2ca04159..f0e1579a07 100644 --- a/core/app/core/src/lib/fields/dropdownenum/templates/edit/dropdownenum.component.html +++ b/core/app/core/src/lib/fields/dropdownenum/templates/edit/dropdownenum.component.html @@ -28,6 +28,7 @@