2020-10-15 09:58:05 +00:00
|
|
|
import { defineComponent, inject, provide, PropType, computed } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2019-01-12 03:33:27 +00:00
|
|
|
import warning from '../_util/warning';
|
|
|
|
import FormItem from './FormItem';
|
2020-10-15 09:58:05 +00:00
|
|
|
import { getSlot } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-07-28 09:10:06 +00:00
|
|
|
import { getNamePath, containsNamePath } from './utils/valueUtil';
|
|
|
|
import { defaultValidateMessages } from './utils/messages';
|
|
|
|
import { allPromiseFinish } from './utils/asyncUtil';
|
|
|
|
import { toArray } from './utils/typeUtil';
|
2020-09-02 14:44:16 +00:00
|
|
|
import isEqual from 'lodash-es/isEqual';
|
2020-07-28 09:10:06 +00:00
|
|
|
import scrollIntoView from 'scroll-into-view-if-needed';
|
2020-10-15 09:58:05 +00:00
|
|
|
import initDefaultProps from '../_util/props-util/initDefaultProps';
|
|
|
|
import { tuple, VueNode } from '../_util/type';
|
|
|
|
import { ColProps } from '../grid/col';
|
|
|
|
import { InternalNamePath, NamePath, ValidateOptions } from './interface';
|
2018-05-05 09:00:51 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
export type ValidationRule = {
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validation error message */
|
2020-10-15 09:58:05 +00:00
|
|
|
message?: VueNode;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** built-in validation type, available options: https://github.com/yiminghe/async-validator#type */
|
2020-10-15 09:58:05 +00:00
|
|
|
type?: string;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** indicates whether field is required */
|
2020-10-15 09:58:05 +00:00
|
|
|
required?: boolean;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** treat required fields that only contain whitespace as errors */
|
2020-10-15 09:58:05 +00:00
|
|
|
whitespace?: boolean;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validate the exact length of a field */
|
2020-10-15 09:58:05 +00:00
|
|
|
len?: number;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validate the min length of a field */
|
2020-10-15 09:58:05 +00:00
|
|
|
min?: number;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validate the max length of a field */
|
2020-10-15 09:58:05 +00:00
|
|
|
max?: number;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validate the value from a list of possible values */
|
2020-10-15 09:58:05 +00:00
|
|
|
enum?: string | string[];
|
2018-05-05 09:00:51 +00:00
|
|
|
/** validate from a regular expression */
|
2020-10-15 09:58:05 +00:00
|
|
|
pattern?: RegExp;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** transform a value before validation */
|
2020-10-15 09:58:05 +00:00
|
|
|
transform?: (value: any) => any;
|
2018-05-05 09:00:51 +00:00
|
|
|
/** custom validate function (Note: callback must be called) */
|
2020-10-15 09:58:05 +00:00
|
|
|
validator?: (rule: any, value: any, callback: any, source?: any, options?: any) => any;
|
|
|
|
|
2020-10-19 08:43:10 +00:00
|
|
|
trigger?: string;
|
2020-10-15 09:58:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const FormProps = {
|
|
|
|
layout: PropTypes.oneOf(tuple('horizontal', 'inline', 'vertical')),
|
2020-10-19 08:43:10 +00:00
|
|
|
labelCol: { type: Object as PropType<ColProps> },
|
|
|
|
wrapperCol: { type: Object as PropType<ColProps> },
|
2020-10-15 09:58:05 +00:00
|
|
|
colon: PropTypes.looseBool,
|
|
|
|
labelAlign: PropTypes.oneOf(tuple('left', 'right')),
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
hideRequiredMark: PropTypes.looseBool,
|
|
|
|
model: PropTypes.object,
|
2020-10-19 08:43:10 +00:00
|
|
|
rules: { type: Object as PropType<ValidationRule[]> },
|
2020-10-15 09:58:05 +00:00
|
|
|
validateMessages: PropTypes.object,
|
|
|
|
validateOnRuleChange: PropTypes.looseBool,
|
|
|
|
// 提交失败自动滚动到第一个错误字段
|
|
|
|
scrollToFirstError: PropTypes.looseBool,
|
|
|
|
onFinish: PropTypes.func,
|
|
|
|
onFinishFailed: PropTypes.func,
|
|
|
|
name: PropTypes.string,
|
2020-10-19 08:43:10 +00:00
|
|
|
validateTrigger: { type: [String, Array] as PropType<string | string[]> },
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-05 09:00:51 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
function isEqualName(name1: any, name2: any) {
|
2020-07-28 09:10:06 +00:00
|
|
|
return isEqual(toArray(name1), toArray(name2));
|
|
|
|
}
|
2018-05-05 09:00:51 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
const Form = defineComponent({
|
2018-05-05 09:00:51 +00:00
|
|
|
name: 'AForm',
|
2020-07-28 09:10:06 +00:00
|
|
|
inheritAttrs: false,
|
2018-05-05 09:00:51 +00:00
|
|
|
props: initDefaultProps(FormProps, {
|
|
|
|
layout: 'horizontal',
|
|
|
|
hideRequiredMark: false,
|
2020-03-07 11:45:13 +00:00
|
|
|
colon: true,
|
2018-05-05 09:00:51 +00:00
|
|
|
}),
|
|
|
|
Item: FormItem,
|
2019-01-27 13:47:27 +00:00
|
|
|
created() {
|
2020-07-28 09:10:06 +00:00
|
|
|
provide('FormContext', this);
|
2019-01-27 13:47:27 +00:00
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
setup(props) {
|
2018-05-05 09:00:51 +00:00
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-15 09:58:05 +00:00
|
|
|
fields: [],
|
|
|
|
form: undefined,
|
|
|
|
lastValidatePromise: null,
|
2020-10-19 08:43:10 +00:00
|
|
|
vertical: computed(() => props.layout === 'vertical'),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-05 09:00:51 +00:00
|
|
|
},
|
2018-12-07 13:27:47 +00:00
|
|
|
watch: {
|
2020-07-28 09:10:06 +00:00
|
|
|
rules() {
|
|
|
|
if (this.validateOnRuleChange) {
|
|
|
|
this.validateFields();
|
|
|
|
}
|
2018-12-07 13:27:47 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-05 09:00:51 +00:00
|
|
|
methods: {
|
2020-10-15 09:58:05 +00:00
|
|
|
addField(field: any) {
|
2020-07-28 09:10:06 +00:00
|
|
|
if (field) {
|
|
|
|
this.fields.push(field);
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
removeField(field: any) {
|
2020-07-28 09:10:06 +00:00
|
|
|
if (field.fieldName) {
|
|
|
|
this.fields.splice(this.fields.indexOf(field), 1);
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
handleSubmit(e: Event) {
|
2020-07-28 09:10:06 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.$emit('submit', e);
|
|
|
|
const res = this.validateFields();
|
|
|
|
res
|
|
|
|
.then(values => {
|
|
|
|
this.$emit('finish', values);
|
|
|
|
})
|
|
|
|
.catch(errors => {
|
|
|
|
this.handleFinishFailed(errors);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getFieldsByNameList(nameList) {
|
|
|
|
const provideNameList = !!nameList;
|
|
|
|
const namePathList = provideNameList ? toArray(nameList).map(getNamePath) : [];
|
|
|
|
if (!provideNameList) {
|
|
|
|
return this.fields;
|
|
|
|
} else {
|
|
|
|
return this.fields.filter(
|
|
|
|
field => namePathList.findIndex(namePath => isEqualName(namePath, field.fieldName)) > -1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
resetFields(name: NamePath) {
|
2020-07-28 09:10:06 +00:00
|
|
|
if (!this.model) {
|
|
|
|
warning(false, 'Form', 'model is required for resetFields to work.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getFieldsByNameList(name).forEach(field => {
|
|
|
|
field.resetField();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clearValidate(name) {
|
|
|
|
this.getFieldsByNameList(name).forEach(field => {
|
|
|
|
field.clearValidate();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleFinishFailed(errorInfo) {
|
|
|
|
const { scrollToFirstError } = this;
|
|
|
|
this.$emit('finishFailed', errorInfo);
|
|
|
|
if (scrollToFirstError && errorInfo.errorFields.length) {
|
|
|
|
this.scrollToField(errorInfo.errorFields[0].name);
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
validate(...args: any[]) {
|
|
|
|
return this.validateField(...args);
|
2020-07-28 09:10:06 +00:00
|
|
|
},
|
2020-10-19 08:43:10 +00:00
|
|
|
scrollToField(name: string | number, options = {}) {
|
2020-07-28 09:10:06 +00:00
|
|
|
const fields = this.getFieldsByNameList([name]);
|
|
|
|
if (fields.length) {
|
|
|
|
const fieldId = fields[0].fieldId;
|
|
|
|
const node = fieldId ? document.getElementById(fieldId) : null;
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
scrollIntoView(node, {
|
|
|
|
scrollMode: 'if-needed',
|
|
|
|
block: 'nearest',
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-10-15 09:58:05 +00:00
|
|
|
getFieldsValue(nameList: NamePath[] | true = true) {
|
|
|
|
const values: any = {};
|
2020-07-28 09:10:06 +00:00
|
|
|
this.fields.forEach(({ fieldName, fieldValue }) => {
|
|
|
|
values[fieldName] = fieldValue;
|
|
|
|
});
|
|
|
|
if (nameList === true) {
|
|
|
|
return values;
|
2018-05-05 09:00:51 +00:00
|
|
|
} else {
|
2020-10-15 09:58:05 +00:00
|
|
|
const res: any = {};
|
2020-10-19 08:43:10 +00:00
|
|
|
toArray(nameList as NamePath[]).forEach(
|
|
|
|
namePath => (res[namePath as string] = values[namePath as string]),
|
|
|
|
);
|
2020-07-28 09:10:06 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
validateFields(nameList?: NamePath[], options?: ValidateOptions) {
|
2020-08-14 07:25:17 +00:00
|
|
|
warning(
|
|
|
|
!(nameList instanceof Function),
|
|
|
|
'Form',
|
|
|
|
'validateFields/validateField/validate not support callback, please use promise instead',
|
|
|
|
);
|
2020-07-28 09:10:06 +00:00
|
|
|
if (!this.model) {
|
|
|
|
warning(false, 'Form', 'model is required for validateFields to work.');
|
2020-08-25 06:17:00 +00:00
|
|
|
return Promise.reject('Form `model` is required for validateFields to work.');
|
2018-05-05 09:00:51 +00:00
|
|
|
}
|
2020-07-28 09:10:06 +00:00
|
|
|
const provideNameList = !!nameList;
|
2020-10-19 08:43:10 +00:00
|
|
|
const namePathList: InternalNamePath[] = provideNameList
|
|
|
|
? toArray(nameList).map(getNamePath)
|
|
|
|
: [];
|
2020-07-28 09:10:06 +00:00
|
|
|
|
|
|
|
// Collect result in promise list
|
2020-10-15 09:58:05 +00:00
|
|
|
const promiseList: Promise<{
|
|
|
|
name: InternalNamePath;
|
|
|
|
errors: string[];
|
|
|
|
}>[] = [];
|
2020-07-28 09:10:06 +00:00
|
|
|
|
|
|
|
this.fields.forEach(field => {
|
|
|
|
// Add field if not provide `nameList`
|
|
|
|
if (!provideNameList) {
|
|
|
|
namePathList.push(field.getNamePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if without rule
|
|
|
|
if (!field.getRules().length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fieldNamePath = field.getNamePath();
|
|
|
|
|
|
|
|
// Add field validate rule in to promise list
|
|
|
|
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
|
|
|
|
const promise = field.validateRules({
|
|
|
|
validateMessages: {
|
|
|
|
...defaultValidateMessages,
|
|
|
|
...this.validateMessages,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wrap promise with field
|
|
|
|
promiseList.push(
|
|
|
|
promise
|
|
|
|
.then(() => ({ name: fieldNamePath, errors: [] }))
|
2020-10-15 09:58:05 +00:00
|
|
|
.catch((errors: any) =>
|
2020-07-28 09:10:06 +00:00
|
|
|
Promise.reject({
|
|
|
|
name: fieldNamePath,
|
|
|
|
errors,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const summaryPromise = allPromiseFinish(promiseList);
|
|
|
|
this.lastValidatePromise = summaryPromise;
|
|
|
|
|
|
|
|
const returnPromise = summaryPromise
|
|
|
|
.then(() => {
|
|
|
|
if (this.lastValidatePromise === summaryPromise) {
|
|
|
|
return Promise.resolve(this.getFieldsValue(namePathList));
|
|
|
|
}
|
|
|
|
return Promise.reject([]);
|
|
|
|
})
|
|
|
|
.catch(results => {
|
|
|
|
const errorList = results.filter(result => result && result.errors.length);
|
|
|
|
return Promise.reject({
|
|
|
|
values: this.getFieldsValue(namePathList),
|
|
|
|
errorFields: errorList,
|
|
|
|
outOfDate: this.lastValidatePromise !== summaryPromise,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Do not throw in console
|
|
|
|
returnPromise.catch(e => e);
|
|
|
|
|
|
|
|
return returnPromise;
|
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
validateField(...args: any[]) {
|
|
|
|
return this.validateFields(...args);
|
2018-05-05 09:00:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-07-28 09:10:06 +00:00
|
|
|
const { prefixCls: customizePrefixCls, hideRequiredMark, layout, handleSubmit } = this;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-20 06:19:13 +00:00
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
2020-07-28 09:10:06 +00:00
|
|
|
const { class: className, onSubmit: originSubmit, ...restProps } = this.$attrs;
|
2018-05-05 09:00:51 +00:00
|
|
|
|
2020-07-28 09:10:06 +00:00
|
|
|
const formClassName = classNames(prefixCls, className, {
|
2018-05-05 09:00:51 +00:00
|
|
|
[`${prefixCls}-horizontal`]: layout === 'horizontal',
|
|
|
|
[`${prefixCls}-vertical`]: layout === 'vertical',
|
|
|
|
[`${prefixCls}-inline`]: layout === 'inline',
|
|
|
|
[`${prefixCls}-hide-required-mark`]: hideRequiredMark,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
return (
|
2020-07-28 09:10:06 +00:00
|
|
|
<form onSubmit={handleSubmit} class={formClassName} {...restProps}>
|
|
|
|
{getSlot(this)}
|
2019-01-12 03:33:27 +00:00
|
|
|
</form>
|
|
|
|
);
|
2018-05-05 09:00:51 +00:00
|
|
|
},
|
2020-10-15 09:58:05 +00:00
|
|
|
});
|
2018-12-07 13:27:47 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
export default Form as typeof Form & {
|
2020-10-19 08:43:10 +00:00
|
|
|
readonly Item: typeof FormItem;
|
2020-10-15 09:58:05 +00:00
|
|
|
};
|