Skip to content

Commit

Permalink
fix(form): fix visibleIf to correctly trigger reset (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Oct 17, 2023
1 parent d25db1d commit d80f8fb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/form/src/model/array.property.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Injector } from '@angular/core';

import { AlainSFConfig } from '@delon/util/config';
import { deepCopy } from '@delon/util/other';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
Expand All @@ -13,6 +15,7 @@ import { SchemaValidatorFactory } from '../validator.factory';

export class ArrayProperty extends PropertyGroup {
constructor(
injector: Injector,
private formPropertyFactory: FormPropertyFactory,
schemaValidatorFactory: SchemaValidatorFactory,
schema: SFSchema,
Expand All @@ -22,7 +25,7 @@ export class ArrayProperty extends PropertyGroup {
path: string,
options: AlainSFConfig
) {
super(schemaValidatorFactory, schema, ui, formData, parent, path, options);
super(injector, schemaValidatorFactory, schema, ui, formData, parent, path, options);
this.properties = [];
}

Expand Down
8 changes: 8 additions & 0 deletions packages/form/src/model/form.property.factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Injector } from '@angular/core';

import { AlainConfigService, AlainSFConfig } from '@delon/util/config';

import { ArrayProperty } from './array.property';
Expand All @@ -16,6 +18,7 @@ import { SchemaValidatorFactory } from '../validator.factory';
export class FormPropertyFactory {
private options: AlainSFConfig;
constructor(
private injector: Injector,
private schemaValidatorFactory: SchemaValidatorFactory,
cogSrv: AlainConfigService
) {
Expand Down Expand Up @@ -78,6 +81,7 @@ export class FormPropertyFactory {
case 'integer':
case 'number':
newProperty = new NumberProperty(
this.injector,
this.schemaValidatorFactory,
schema,
ui,
Expand All @@ -89,6 +93,7 @@ export class FormPropertyFactory {
break;
case 'string':
newProperty = new StringProperty(
this.injector,
this.schemaValidatorFactory,
schema,
ui,
Expand All @@ -100,6 +105,7 @@ export class FormPropertyFactory {
break;
case 'boolean':
newProperty = new BooleanProperty(
this.injector,
this.schemaValidatorFactory,
schema,
ui,
Expand All @@ -111,6 +117,7 @@ export class FormPropertyFactory {
break;
case 'object':
newProperty = new ObjectProperty(
this.injector,
this,
this.schemaValidatorFactory,
schema,
Expand All @@ -123,6 +130,7 @@ export class FormPropertyFactory {
break;
case 'array':
newProperty = new ArrayProperty(
this.injector,
this,
this.schemaValidatorFactory,
schema,
Expand Down
11 changes: 9 additions & 2 deletions packages/form/src/model/form.property.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BehaviorSubject, combineLatest, Observable, distinctUntilChanged, map } from 'rxjs';
import { Injector, NgZone } from '@angular/core';
import { BehaviorSubject, combineLatest, Observable, distinctUntilChanged, map, take } from 'rxjs';

import { AlainSFConfig } from '@delon/util/config';
import { NzFormStatusService } from 'ng-zorro-antd/core/form';
Expand Down Expand Up @@ -33,6 +34,7 @@ export abstract class FormProperty {
propertyId?: string;

constructor(
private injector: Injector,
schemaValidatorFactory: SchemaValidatorFactory,
schema: SFSchema,
ui: SFUISchema | SFUISchemaItem,
Expand Down Expand Up @@ -312,7 +314,12 @@ export abstract class FormProperty {
this._visibilityChanges.next(visible);
// 渲染时需要重新触发 reset
if (visible) {
this.resetValue(this.value, true);
this.injector
.get(NgZone)
.onStable.pipe(take(1))
.subscribe(() => {
this.resetValue(this.value, true);
});
}
return this;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/form/src/model/object.property.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Injector } from '@angular/core';

import { AlainSFConfig } from '@delon/util/config';
import type { NzSafeAny } from 'ng-zorro-antd/core/types';

Expand All @@ -17,6 +19,7 @@ export class ObjectProperty extends PropertyGroup {
}

constructor(
injector: Injector,
private formPropertyFactory: FormPropertyFactory,
schemaValidatorFactory: SchemaValidatorFactory,
schema: SFSchema,
Expand All @@ -26,7 +29,7 @@ export class ObjectProperty extends PropertyGroup {
path: string,
options: AlainSFConfig
) {
super(schemaValidatorFactory, schema, ui, formData, parent, path, options);
super(injector, schemaValidatorFactory, schema, ui, formData, parent, path, options);
this.createProperties();
}

Expand Down
6 changes: 4 additions & 2 deletions packages/form/src/sf.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Component,
EventEmitter,
Inject,
Injector,
Input,
OnChanges,
OnDestroy,
Expand Down Expand Up @@ -41,10 +42,11 @@ import { SchemaValidatorFactory } from './validator.factory';
import { WidgetFactory } from './widget.factory';

export function useFactory(
injector: Injector,
schemaValidatorFactory: SchemaValidatorFactory,
cogSrv: AlainConfigService
): FormPropertyFactory {
return new FormPropertyFactory(schemaValidatorFactory, cogSrv);
return new FormPropertyFactory(injector, schemaValidatorFactory, cogSrv);
}

@Component({
Expand All @@ -56,7 +58,7 @@ export function useFactory(
{
provide: FormPropertyFactory,
useFactory,
deps: [SchemaValidatorFactory, AlainConfigService]
deps: [Injector, SchemaValidatorFactory, AlainConfigService]
},
TerminatorService
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('form: widget: autocomplete', () => {
}
}
})
.time(100)
.dc(100)
.typeChar(typeValue)
.checkCount('nz-auto-option', config.uiEmailSuffixes!.length)
.click('nz-auto-option')
Expand Down
5 changes: 3 additions & 2 deletions packages/form/src/widgets/checkbox/checkbox.widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('form: widget: checkbox', () => {
expect(page.getEl('.ant-checkbox-checked').nextSibling!.textContent).toBe('item2');
}));

it('#visibleIf', () => {
it('#visibleIf', fakeAsync(() => {
page
.newSchema({
properties: {
Expand All @@ -47,8 +47,9 @@ describe('form: widget: checkbox', () => {
})
.checkCount(chekcWrapCls, 1)
.click(chekcWrapCls)
.dc(100)
.checkCount(chekcWrapCls, 2);
});
}));

it('should be ingore title when not array data', () => {
const title = 'test';
Expand Down

0 comments on commit d80f8fb

Please sign in to comment.