Skip to content

Commit bc609b3

Browse files
authoredNov 23, 2022
fix(from): fix can't detect change when call setValue (#1554)
1 parent d6aa219 commit bc609b3

10 files changed

+32
-7
lines changed
 

‎packages/form/docs/qa.en-US.md

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ statusProperty.widget.reset('2');
108108
// statusProperty.widget.detectChanges();
109109
```
110110

111+
If just only update a element value, then:
112+
113+
```ts
114+
this.sf.getProperty('/name')?.setValue('new name');
115+
```
116+
111117
## Why can't verify `required`
112118

113119
Added [strict](https://ajv.js.org/options.html#strict-mode-options) mode from Ajv 7.x, and the default is `true`, when the most basic `required` is not correct When verifying, the high probability is that the Schema contains information that does not conform to the Json Schema format. This can be verified through the `debug` mode:

‎packages/form/docs/qa.zh-CN.md

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ statusProperty.widget.reset('2');
108108
// statusProperty.widget.detectChanges();
109109
```
110110

111+
如果单纯更新某个元素数据,则:
112+
113+
```ts
114+
this.sf.getProperty('/name')?.setValue('new name');
115+
```
116+
111117
## 为什么无法校验 `required`
112118

113119
从 Ajv 7.x 新增 [strict](https://ajv.js.org/options.html#strict-mode-options) 模式,并且默认为 `true`,当最基本的 `required` 都无法正确校验时,那大概率就是因为 Schema 包含了不符合 Json Schema 格式的信息,可以通过 `debug` 模式验证这一点:

‎packages/form/src/model/array.property.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class ArrayProperty extends PropertyGroup {
4141
this.properties = [];
4242
this.clearErrors();
4343
this.resetProperties(value);
44+
this.cd(onlySelf);
4445
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
4546
}
4647

‎packages/form/src/model/atomic.property.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export abstract class AtomicProperty extends FormProperty {
66

77
setValue(value: SFValue, onlySelf: boolean): void {
88
this._value = value;
9+
this.cd(onlySelf);
910
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
1011
}
1112

@@ -17,7 +18,10 @@ export abstract class AtomicProperty extends FormProperty {
1718

1819
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
1920

20-
if (this.widget) this.widget.reset(value);
21+
if (this.widget) {
22+
this.widget.reset(value);
23+
this.cd(onlySelf);
24+
}
2125
}
2226

2327
_hasValue(): boolean {

‎packages/form/src/model/form.property.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export abstract class FormProperty {
121121
*/
122122
abstract _updateValue(): void;
123123

124+
cd(onlySelf: boolean = false): void {
125+
this.widget?.detectChanges(onlySelf);
126+
}
127+
124128
/**
125129
* 更新值且校验数据
126130
*/
@@ -213,7 +217,7 @@ export abstract class FormProperty {
213217
if (customErrors instanceof Observable) {
214218
customErrors.subscribe(res => {
215219
this.setCustomErrors(errors, res);
216-
this.widget.detectChanges();
220+
this.cd(false);
217221
});
218222
return;
219223
}
@@ -376,8 +380,8 @@ export abstract class FormProperty {
376380

377381
updateFeedback(status: NzFormControlStatusType = ''): void {
378382
this.ui.feedback = status;
379-
this.widget.injector.get(NzFormStatusService).formStatusChanges.next({ status, hasFeedback: !!status });
380-
this.widget.detectChanges();
383+
this.widget?.injector.get(NzFormStatusService).formStatusChanges.next({ status, hasFeedback: !!status });
384+
this.cd(true);
381385
}
382386
}
383387

‎packages/form/src/model/number.property.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class NumberProperty extends AtomicProperty {
1515
}
1616
}
1717
this._value = value;
18+
this.cd(onlySelf);
1819
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
1920
}
2021
}

‎packages/form/src/model/object.property.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class ObjectProperty extends PropertyGroup {
5858
(properties[propertyId] as FormProperty).setValue(value[propertyId], true);
5959
}
6060
}
61+
this.cd(onlySelf);
6162
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
6263
}
6364

@@ -69,6 +70,7 @@ export class ObjectProperty extends PropertyGroup {
6970
properties[propertyId].resetValue(value[propertyId], true);
7071
}
7172
}
73+
this.cd(onlySelf);
7274
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
7375
}
7476

‎packages/form/src/model/string.property.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class StringProperty extends AtomicProperty {
88

99
setValue(value: SFValue, onlySelf: boolean): void {
1010
this._value = value == null ? '' : value;
11+
this.cd(onlySelf);
1112
this.updateValueAndValidity({ onlySelf, emitValueEvent: true });
1213
}
1314
}

‎packages/form/src/widget.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export abstract class Widget<T extends FormProperty, UIT extends SFUISchemaItem>
4040
}
4141

4242
get l(): LocaleData {
43-
return this.formProperty.root.widget.sfComp!.locale;
43+
return this.formProperty.root.widget!.sfComp!.locale;
4444
}
4545

4646
get oh(): SFOptionalHelp {
@@ -95,7 +95,7 @@ export abstract class Widget<T extends FormProperty, UIT extends SFUISchemaItem>
9595
if (onlySelf) {
9696
this.cd.markForCheck();
9797
} else {
98-
this.formProperty.root.widget.cd.markForCheck();
98+
this.formProperty.root.widget?.cd.markForCheck();
9999
}
100100
}
101101

‎packages/form/src/widgets/custom/custom.widget.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('form: widget: custom', () => {
1212
};
1313

1414
function detectChanges(path: string = '/a'): SFPage {
15-
context.comp.rootProperty!.searchProperty(path)!.widget.detectChanges();
15+
context.comp.rootProperty!.searchProperty(path)?.cd(true);
1616
fixture.detectChanges();
1717
return page;
1818
}

0 commit comments

Comments
 (0)
Please sign in to comment.