Skip to content

Commit

Permalink
Fix dynamic child enum not to save value
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusyerli1 committed Oct 5, 2023
1 parent 920f3c9 commit 5ef7edf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
50 changes: 28 additions & 22 deletions core/app/common/src/lib/record/field.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -176,7 +185,7 @@ export interface Field {
itemFormArray?: FormArray;
validators?: ValidatorFn[];
asyncValidators?: AsyncValidatorFn[];
valueSubject?: BehaviorSubject<FieldValue>;
valueSubject?: Subject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
fieldDependencies?: string[];
attributeDependencies?: AttributeDependency[];
Expand All @@ -201,7 +210,7 @@ export class BaseField implements Field {
validators?: ValidatorFn[];
asyncValidators?: AsyncValidatorFn[];
attributes?: FieldAttributeMap;
valueSubject?: BehaviorSubject<FieldValue>;
valueSubject?: Subject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
fieldDependencies: string[] = [];
attributeDependencies: AttributeDependency[] = [];
Expand All @@ -213,7 +222,7 @@ export class BaseField implements Field {
protected valueObjectArrayState?: ObjectMap[];

constructor() {
this.valueSubject = new BehaviorSubject<FieldValue>({} as FieldValue);
this.valueSubject = new Subject<FieldValue>();
this.valueChanges$ = this.valueSubject.asObservable();
}

Expand All @@ -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();
}
Expand All @@ -236,28 +243,35 @@ 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 {
return this.valueObjectState;
}

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 {
return this.valueObjectArrayState;
}

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() {
Expand All @@ -268,11 +282,3 @@ export class BaseField implements Field {
})
}
}

export interface FieldValue {
value?: string;
valueList?: string[];
valueObject?: any;
}


35 changes: 20 additions & 15 deletions core/app/core/src/lib/fields/base/base-enum.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<div class="dropdownenum">
<select [formControl]="field.formControl" class="custom-select custom-select-sm">
<ng-container *ngIf="this.options && this.options.length">
<option class="d-none" disabled value=""></option>
<option *ngFor="let item of this.options;"
class="{{getId(item)}}"
[ngValue]="item.value">
Expand Down

0 comments on commit 5ef7edf

Please sign in to comment.