Skip to content

Commit 9172ea9

Browse files
committed
fix: form dynamic error
1 parent a9b6de0 commit 9172ea9

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

components/form/Form.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export type FormExpose = {
9393
resetFields: (name?: NamePath) => void;
9494
clearValidate: (name?: NamePath) => void;
9595
validateFields: (
96-
nameList?: NamePath[],
96+
nameList?: NamePath[] | string,
9797
options?: ValidateOptions,
9898
) => Promise<{
9999
[key: string]: any;
@@ -102,7 +102,7 @@ export type FormExpose = {
102102
[key: string]: any;
103103
};
104104
validate: (
105-
nameList?: NamePath[],
105+
nameList?: NamePath[] | string,
106106
options?: ValidateOptions,
107107
) => Promise<{
108108
[key: string]: any;

components/form/FormItem.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PropType, ExtractPropTypes, ComputedRef } from 'vue';
1+
import type { PropType, ExtractPropTypes, ComputedRef, Ref } from 'vue';
22
import {
33
watch,
44
defineComponent,
@@ -32,7 +32,7 @@ const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
3232
export type ValidateStatus = typeof ValidateStatuses[number];
3333

3434
export interface FieldExpose {
35-
fieldValue: ComputedRef<any>;
35+
fieldValue: Ref<any>;
3636
fieldId: ComputedRef<any>;
3737
fieldName: ComputedRef<any>;
3838
resetField: () => void;
@@ -132,13 +132,21 @@ export default defineComponent({
132132
return formName ? `${formName}_${mergedId}` : `${defaultItemNamePrefixCls}_${mergedId}`;
133133
}
134134
});
135-
const fieldValue = computed(() => {
135+
const getNewFieldValue = () => {
136136
const model = formContext.model.value;
137137
if (!model || !fieldName.value) {
138138
return;
139+
} else {
140+
return getPropByPath(model, namePath.value, true).v;
139141
}
140-
return getPropByPath(model, namePath.value, true).v;
141-
});
142+
};
143+
const fieldValue = ref(getNewFieldValue());
144+
watchEffect(
145+
() => {
146+
fieldValue.value = getNewFieldValue();
147+
},
148+
{ flush: 'post' },
149+
);
142150

143151
const initialValue = ref(cloneDeep(fieldValue.value));
144152
const mergedValidateTrigger = computed(() => {

0 commit comments

Comments
 (0)