fix: after deleting the form item, you will also get the form value (#4481)

* fix: after deleting the form item, you will also get the form value
This commit is contained in:
Vben
2024-09-23 22:59:58 +08:00
committed by GitHub
parent 1197efea26
commit f815dcf3ae
10 changed files with 92 additions and 60 deletions

View File

@@ -43,11 +43,11 @@ function getDefaultState(): VbenFormProps {
}
export class FormApi {
private prevState: null | VbenFormProps = null;
// private api: Pick<VbenFormProps, 'handleReset' | 'handleSubmit'>;
public form = {} as FormActions;
isMounted = false;
// private prevState!: ModalState;
isMounted = false;
public state: null | VbenFormProps = null;
stateHandler: StateHandler;
@@ -66,7 +66,9 @@ export class FormApi {
},
{
onUpdate: () => {
this.prevState = this.state;
this.state = this.store.state;
this.updateState();
},
},
);
@@ -87,6 +89,24 @@ export class FormApi {
return this.form;
}
private updateState() {
const currentSchema = this.state?.schema ?? [];
const prevSchema = this.prevState?.schema ?? [];
// 进行了删除schema操作
if (currentSchema.length < prevSchema.length) {
const currentFields = new Set(
currentSchema.map((item) => item.fieldName),
);
const deletedSchema = prevSchema.filter(
(item) => !currentFields.has(item.fieldName),
);
for (const schema of deletedSchema) {
this.form?.setFieldValue(schema.fieldName, undefined);
}
}
}
// 如果需要多次更新状态,可以使用 batch 方法
batchStore(cb: () => void) {
this.store.batch(cb);