Skip to content

Commit

Permalink
editor: fix hidden field with value from the model
Browse files Browse the repository at this point in the history
Some fields containing values given with the input model should be visible
after the editor initialization. This is not the case with a recent version of
ngx formly. This is due to the use of bad hack `setTimeout` at several
places in the code. As ngx formly made the think cleaner these hacks have
been removed.

* Cleans hidden field list at the component destruction instead of
initialization.
* Reloads the JSONSchema once the model has changed.

Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>
  • Loading branch information
jma committed Aug 13, 2020
1 parent 6002671 commit 3b81ad3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export class AddFieldEditorComponent implements OnInit, OnDestroy {
private _translateService: TranslateService
) {
this.hiddenFields$ = this._editorService.hiddenFields$;
this._subscribers.push(
this.hiddenFields$.subscribe(fields => this.typeaheadFields = fields));

this.typeaheadFields$ = new Observable((observer: Subscriber<string>) => {
// Runs on every search
observer.next(this.value);
Expand All @@ -73,6 +72,8 @@ export class AddFieldEditorComponent implements OnInit, OnDestroy {
*/
ngOnDestroy() {
this._subscribers.forEach(s => s.unsubscribe());
// avoid duplicate when switching between page
this._editorService.clearHiddenFields();
}

/**
Expand Down Expand Up @@ -105,8 +106,9 @@ export class AddFieldEditorComponent implements OnInit, OnDestroy {
* Component init
*/
ngOnInit() {
// avoid duplicate when switching between page
this._editorService.clearHiddenFields();
this._subscribers.push(
this.hiddenFields$.subscribe(fields => this.typeaheadFields = fields)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ArrayTypeComponent extends FieldArrayType implements OnInit {
this.isChildrenObject = this.field.fieldArray.type === 'object';
this.isChildrenArray = this.field.fieldArray.type === 'array';

// reset the number of elements in the array when the array id hidden
// reset the number of elements in the array when the array is hidden
this.field.options.fieldChanges.subscribe(changes => {
const minItems = this.field.templateOptions.minItems ? this.field.templateOptions.minItems : 0;
if (
Expand All @@ -62,6 +62,7 @@ export class ArrayTypeComponent extends FieldArrayType implements OnInit {
const numberOfItemsToRemove = this.field.fieldGroup.length - minItems;
// remove the extra elements
// force removing the elements in the next event loop else this cause errors when removing multiple values
// TODO: try to remove this hack
setTimeout(() => {
for (let i = 0; i < numberOfItemsToRemove; i++) {
this.remove(0);
Expand Down
15 changes: 8 additions & 7 deletions projects/rero/ng-core/src/lib/record/editor/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
* @param changes: the changed properties
*/
ngOnChanges(changes: SimpleChanges): void {
if (changes.model && !changes.model.isFirstChange) { // Model has changed
if (changes.model && !changes.model.isFirstChange()) { // Model has changed
this._setModel(changes.model.currentValue);
// needed to select the right value for existing data in the multi select
// components such as oneOf
if (this.schema) {
this.setSchema(this.schema);
}
}
}

Expand Down Expand Up @@ -287,6 +292,8 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
// reorder all object properties
this.schema = orderedJsonSchema(schema);
this.options = {};
// remove hidden field list in case of a previous setSchema call
this._editorService.clearHiddenFields();

// form configuration
const fields = [
Expand Down Expand Up @@ -359,16 +366,12 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
model = removeEmptyValues(model);
const modelEmpty = isEmpty(model);
if (!modelEmpty && (field.hide === true)) {
setTimeout(() => {
field.hide = false;
this._editorService.removeHiddenField(field);
});
}
if (modelEmpty && (field.templateOptions.hide === true && field.hide === undefined)) {
setTimeout(() => {
field.hide = true;
this._editorService.addHiddenField(field);
});
}
}

Expand Down Expand Up @@ -432,11 +435,9 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
* Populate the field to add to the TOC
*/
getTocFields() {
setTimeout(() => {
if (this.fields && this.fields.length > 0) {
this.tocFields = this.fields[0].fieldGroup.filter(f => f.hide !== true);
}
});
}

/**
Expand Down

0 comments on commit 3b81ad3

Please sign in to comment.