Skip to content

Commit

Permalink
fix(core): toggle control when hide changed programmatically (#1888)
Browse files Browse the repository at this point in the history
fix #1817, fix #1742
  • Loading branch information
aitboudad committed Oct 30, 2019
1 parent fb8485d commit fdb1cbe
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/core/src/lib/components/formly.field.config.ts
Expand Up @@ -254,6 +254,7 @@ export interface FormlyFormOptionsCache extends FormlyFormOptions {
_buildForm?: () => void;
_componentFactoryResolver?: ComponentFactoryResolver;
_injector?: Injector;
_hiddenFieldsForCheck?: FormlyFieldConfigCache[];
}
export interface FormlyFormOptions {
updateInitialValue?: () => void;
Expand Down
Expand Up @@ -88,10 +88,28 @@ describe('FieldExpressionExtension', () => {
expect(fields[0].hide).toBeFalsy();
expect(fields[0].fieldGroup[0].hide).toBeTruthy();

expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledTimes(3);
subscription.unsubscribe();
});

it('should toggle field control when hide changed programmatically', () => {
const fields: FormlyFieldConfig[] = [
{ hide: false, key: 'foo'},
{ hide: true, key: 'bar'},
];
builder.buildForm(form, fields, {}, options);

expect(form.get('foo')).not.toBeNull();
expect(form.get('bar')).toBeNull();

fields[0].hide = true;
fields[1].hide = false;
options._checkField({ formControl: form, fieldGroup: fields, options });

expect(form.get('foo')).toBeNull();
expect(form.get('bar')).not.toBeNull();
});

it('should update field visibility within field arrays', () => {
const fields: FormlyFieldConfig[] = [
{
Expand Down
14 changes: 10 additions & 4 deletions src/core/src/lib/extensions/field-expression/field-expression.ts
@@ -1,5 +1,5 @@
import { FormlyFieldConfig, FormlyValueChangeEvent, FormlyFieldConfigCache } from '../../components/formly.field.config';
import { isObject, isNullOrUndefined, isFunction, defineHiddenProp } from '../../utils';
import { isObject, isNullOrUndefined, isFunction, defineHiddenProp, wrapProperty } from '../../utils';
import { evalExpression, evalStringExpression, evalExpressionValueSetter } from './utils';
import { Observable } from 'rxjs';
import { FormlyExtension } from '../../services/formly.config';
Expand Down Expand Up @@ -75,6 +75,15 @@ export class FieldExpressionExtension implements FormlyExtension {
field.hideExpression,
parent && parent.hideExpression ? () => parent.hide : undefined,
);
} else if (field.key) {
wrapProperty(field, 'hide', (newValue, oldValue) => {
if (
(oldValue === undefined && newValue === true)
|| (oldValue !== undefined && newValue !== oldValue)
) {
field.options._hiddenFieldsForCheck.push(field);
}
});
}
}

Expand All @@ -99,9 +108,6 @@ export class FieldExpressionExtension implements FormlyExtension {

private _checkField(field: FormlyFieldConfigCache, ignoreCache = false) {
const options = field.options as { _hiddenFieldsForCheck: FormlyFieldConfigCache[] };
if (!field.parent) {
options._hiddenFieldsForCheck = [];
}

let markForCheck = false;
field.fieldGroup.forEach(f => {
Expand Down
4 changes: 4 additions & 0 deletions src/core/src/lib/services/formly.form.builder.ts
Expand Up @@ -56,6 +56,10 @@ export class FormlyFormBuilder {
defineHiddenProp(options, '_injector', this.injector);
}

if (!options._hiddenFieldsForCheck) {
options._hiddenFieldsForCheck = [];
}

if (!options._markForCheck) {
options._markForCheck = (field) => {
if (field._componentRefs) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/lib/utils.ts
Expand Up @@ -149,7 +149,7 @@ export function defineHiddenProp(field: any, prop: string, defaultValue: any) {
field[prop] = defaultValue;
}

export function wrapProperty<T = any>(field, prop, setFn: (newVal: T, oldVal?: T) => void) {
export function wrapProperty<T = any>(field: any, prop: string, setFn: (newVal: T, oldVal?: T) => void) {
let value = field[prop];
setFn(value);

Expand Down

0 comments on commit fdb1cbe

Please sign in to comment.