Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
HIDDEN_MATCHER_PROVIDER,
REQUIRED_MATCHER_PROVIDER,
} from '@ng-dynamic-forms/core';
import { Subject } from 'rxjs';

import { getMockFormBuilderService } from '../../testing/form-builder-service.mock';
import {
Expand Down Expand Up @@ -99,6 +100,29 @@ describe('DSDynamicTypeBindRelationService test suite', () => {
expect(subscriptions).toHaveSize(1);
});

describe('Test control reset on MATCH_VISIBLE', () => {
it('should reset control when MATCH_VISIBLE becomes false', () => {
const testModel = mockInputWithTypeBindModel;
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');

const control = new UntypedFormControl();
control.setValue('test value');

const typeModel: any = (service as any).formBuilderService.getTypeBindModel();

typeModel.valueChanges = new Subject();

typeModel.value = 'boundType';

service.subscribeRelations(testModel, control);

typeModel.value = 'anotherType';
typeModel.valueChanges.next('anotherType');

expect(control.value).toBeNull();
});
});

it('Expect hasMatch to be true (ie. this should be hidden)', () => {
const testModel = mockInputWithTypeBindModel;
testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
DynamicFormControlModel,
DynamicFormControlRelation,
DynamicFormRelationService,
MATCH_VISIBLE,
OR_OPERATOR,
} from '@ng-dynamic-forms/core';
import { Subscription } from 'rxjs';
Expand Down Expand Up @@ -205,6 +206,10 @@ export class DsDynamicTypeBindRelationService {
if (relation !== undefined) {
const hasMatch = this.matchesCondition(relation, matcher);
matcher.onChange(hasMatch, model, control, this.injector);
// When field becomes hidden, reset its FormControl to clear stale values
if (relation.match === MATCH_VISIBLE && !hasMatch && hasValue(control)) {
control.reset();
}
}
});
}
Expand Down
Loading