Skip to content

Commit

Permalink
doc: form add demo (#5070)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tangjinzhou authored Dec 23, 2021
1 parent 1c9cfd1 commit 7aae6f6
Show file tree
Hide file tree
Showing 21 changed files with 1,662 additions and 298 deletions.
17 changes: 10 additions & 7 deletions components/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type FormExpose = {
resetFields: (name?: NamePath) => void;
clearValidate: (name?: NamePath) => void;
validateFields: (
nameList?: NamePath[],
nameList?: NamePath[] | string,
options?: ValidateOptions,
) => Promise<{
[key: string]: any;
Expand All @@ -102,7 +102,7 @@ export type FormExpose = {
[key: string]: any;
};
validate: (
nameList?: NamePath[],
nameList?: NamePath[] | string,
options?: ValidateOptions,
) => Promise<{
[key: string]: any;
Expand Down Expand Up @@ -145,7 +145,12 @@ const Form = defineComponent({
}
return true;
});

const validateMessages = computed(() => {
return {
...defaultValidateMessages,
...props.validateMessages,
};
});
const formClassName = computed(() =>
classNames(prefixCls.value, {
[`${prefixCls.value}-${props.layout}`]: true,
Expand Down Expand Up @@ -267,10 +272,7 @@ const Form = defineComponent({
// Add field validate rule in to promise list
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
const promise = field.validateRules({
validateMessages: {
...defaultValidateMessages,
...props.validateMessages,
},
validateMessages: validateMessages.value,
...options,
});

Expand Down Expand Up @@ -376,6 +378,7 @@ const Form = defineComponent({
onValidate: (name, status, errors) => {
emit('validate', name, status, errors);
},
validateMessages,
});

watch(
Expand Down
46 changes: 28 additions & 18 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PropType, ExtractPropTypes, ComputedRef } from 'vue';
import type { PropType, ExtractPropTypes, ComputedRef, Ref } from 'vue';
import {
watch,
defineComponent,
Expand Down Expand Up @@ -32,7 +32,7 @@ const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
export type ValidateStatus = typeof ValidateStatuses[number];

export interface FieldExpose {
fieldValue: ComputedRef<any>;
fieldValue: Ref<any>;
fieldId: ComputedRef<any>;
fieldName: ComputedRef<any>;
resetField: () => void;
Expand Down Expand Up @@ -132,13 +132,15 @@ export default defineComponent({
return formName ? `${formName}_${mergedId}` : `${defaultItemNamePrefixCls}_${mergedId}`;
}
});
const fieldValue = computed(() => {
const getNewFieldValue = () => {
const model = formContext.model.value;
if (!model || !fieldName.value) {
return;
} else {
return getPropByPath(model, namePath.value, true).v;
}
return getPropByPath(model, namePath.value, true).v;
});
};
const fieldValue = computed(() => getNewFieldValue());

const initialValue = ref(cloneDeep(fieldValue.value));
const mergedValidateTrigger = computed(() => {
Expand Down Expand Up @@ -184,9 +186,20 @@ export default defineComponent({
watchEffect(() => {
validateState.value = props.validateStatus;
});

const messageVariables = computed(() => {
let variables: Record<string, string> = {};
if (typeof props.label === 'string') {
variables.label = props.label;
} else if (props.name) {
variables.label = String(name);
}
if (props.messageVariables) {
variables = { ...variables, ...props.messageVariables };
}
return variables;
});
const validateRules = (options: ValidateOptions) => {
const { validateFirst = false, messageVariables } = props;
const { validateFirst = false } = props;
const { triggerName } = options || {};

let filteredRules = rulesRef.value;
Expand All @@ -207,9 +220,12 @@ export default defineComponent({
namePath.value,
fieldValue.value,
filteredRules as RuleObject[],
options,
{
validateMessages: formContext.validateMessages.value,
...options,
},
validateFirst,
messageVariables,
messageVariables.value,
);
validateState.value = 'validating';
errors.value = [];
Expand Down Expand Up @@ -285,12 +301,6 @@ export default defineComponent({
resetField,
});

// instead useProvideFormItemContext onFieldChange
watch(fieldValue, () => {
if (props.autoLink) {
onFieldChange();
}
});
useProvideFormItemContext(
{
id: fieldId,
Expand All @@ -300,9 +310,9 @@ export default defineComponent({
}
},
onFieldChange: () => {
// if (props.autoLink) {
// onFieldChange();
// }
if (props.autoLink) {
onFieldChange();
}
},
clearValidate,
},
Expand Down
Loading

0 comments on commit 7aae6f6

Please sign in to comment.