Skip to content

Commit 7aae6f6

Browse files
authored
doc: form add demo (#5070)
* doc: add form demo * fix: form dynamic error * doc: add form demo * revert: formitem auto validate #4955 * fix: input not update when set undefined * doc: add form demo * fix: form validate for number tyope, close #5064 * fix: form validateMessages not work * doc: add form demo
1 parent 1c9cfd1 commit 7aae6f6

21 files changed

+1662
-298
lines changed

components/form/Form.tsx

+10-7
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;
@@ -145,7 +145,12 @@ const Form = defineComponent({
145145
}
146146
return true;
147147
});
148-
148+
const validateMessages = computed(() => {
149+
return {
150+
...defaultValidateMessages,
151+
...props.validateMessages,
152+
};
153+
});
149154
const formClassName = computed(() =>
150155
classNames(prefixCls.value, {
151156
[`${prefixCls.value}-${props.layout}`]: true,
@@ -267,10 +272,7 @@ const Form = defineComponent({
267272
// Add field validate rule in to promise list
268273
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
269274
const promise = field.validateRules({
270-
validateMessages: {
271-
...defaultValidateMessages,
272-
...props.validateMessages,
273-
},
275+
validateMessages: validateMessages.value,
274276
...options,
275277
});
276278

@@ -376,6 +378,7 @@ const Form = defineComponent({
376378
onValidate: (name, status, errors) => {
377379
emit('validate', name, status, errors);
378380
},
381+
validateMessages,
379382
});
380383

381384
watch(

components/form/FormItem.tsx

+28-18
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,15 @@ 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 = computed(() => getNewFieldValue());
142144

143145
const initialValue = ref(cloneDeep(fieldValue.value));
144146
const mergedValidateTrigger = computed(() => {
@@ -184,9 +186,20 @@ export default defineComponent({
184186
watchEffect(() => {
185187
validateState.value = props.validateStatus;
186188
});
187-
189+
const messageVariables = computed(() => {
190+
let variables: Record<string, string> = {};
191+
if (typeof props.label === 'string') {
192+
variables.label = props.label;
193+
} else if (props.name) {
194+
variables.label = String(name);
195+
}
196+
if (props.messageVariables) {
197+
variables = { ...variables, ...props.messageVariables };
198+
}
199+
return variables;
200+
});
188201
const validateRules = (options: ValidateOptions) => {
189-
const { validateFirst = false, messageVariables } = props;
202+
const { validateFirst = false } = props;
190203
const { triggerName } = options || {};
191204

192205
let filteredRules = rulesRef.value;
@@ -207,9 +220,12 @@ export default defineComponent({
207220
namePath.value,
208221
fieldValue.value,
209222
filteredRules as RuleObject[],
210-
options,
223+
{
224+
validateMessages: formContext.validateMessages.value,
225+
...options,
226+
},
211227
validateFirst,
212-
messageVariables,
228+
messageVariables.value,
213229
);
214230
validateState.value = 'validating';
215231
errors.value = [];
@@ -285,12 +301,6 @@ export default defineComponent({
285301
resetField,
286302
});
287303

288-
// instead useProvideFormItemContext onFieldChange
289-
watch(fieldValue, () => {
290-
if (props.autoLink) {
291-
onFieldChange();
292-
}
293-
});
294304
useProvideFormItemContext(
295305
{
296306
id: fieldId,
@@ -300,9 +310,9 @@ export default defineComponent({
300310
}
301311
},
302312
onFieldChange: () => {
303-
// if (props.autoLink) {
304-
// onFieldChange();
305-
// }
313+
if (props.autoLink) {
314+
onFieldChange();
315+
}
306316
},
307317
clearValidate,
308318
},

0 commit comments

Comments
 (0)