2021-08-01 23:30:00 +08:00
|
|
|
import type { PropType, ExtractPropTypes, ComputedRef } from 'vue';
|
2021-09-25 16:51:32 +08:00
|
|
|
import { watch, defineComponent, computed, nextTick, ref, watchEffect, onBeforeUnmount } from 'vue';
|
2020-09-02 22:44:16 +08:00
|
|
|
import cloneDeep from 'lodash-es/cloneDeep';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import Row from '../grid/Row';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { ColProps } from '../grid/Col';
|
2021-09-25 16:51:32 +08:00
|
|
|
import { filterEmpty } from '../_util/props-util';
|
2021-06-07 17:35:03 +08:00
|
|
|
import { validateRules as validateRulesUtil } from './utils/validateUtil';
|
2020-07-28 17:10:06 +08:00
|
|
|
import { getNamePath } from './utils/valueUtil';
|
|
|
|
import { toArray } from './utils/typeUtil';
|
|
|
|
import { warning } from '../vc-util/warning';
|
2020-09-02 22:44:16 +08:00
|
|
|
import find from 'lodash-es/find';
|
2021-06-07 17:35:03 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2021-06-28 11:21:23 +08:00
|
|
|
import type { InternalNamePath, RuleError, RuleObject, ValidateOptions } from './interface';
|
2021-06-07 17:35:03 +08:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { useInjectForm } from './context';
|
|
|
|
import FormItemLabel from './FormItemLabel';
|
|
|
|
import FormItemInput from './FormItemInput';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { ValidationRule } from './Form';
|
2021-09-25 16:51:32 +08:00
|
|
|
import { useProvideFormItemContext } from './FormItemContext';
|
2018-12-19 21:26:32 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', '');
|
|
|
|
export type ValidateStatus = typeof ValidateStatuses[number];
|
|
|
|
|
|
|
|
export interface FieldExpose {
|
|
|
|
fieldValue: ComputedRef<any>;
|
|
|
|
fieldId: ComputedRef<any>;
|
|
|
|
fieldName: ComputedRef<any>;
|
|
|
|
resetField: () => void;
|
|
|
|
clearValidate: () => void;
|
|
|
|
namePath: ComputedRef<InternalNamePath>;
|
|
|
|
rules?: ComputedRef<ValidationRule[]>;
|
2021-06-28 11:21:23 +08:00
|
|
|
validateRules: (options: ValidateOptions) => Promise<void> | Promise<RuleError[]>;
|
2021-06-07 17:35:03 +08:00
|
|
|
}
|
2020-03-26 11:56:16 +08:00
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
function getPropByPath(obj: any, namePathList: any, strict?: boolean) {
|
2020-07-28 17:10:06 +08:00
|
|
|
let tempObj = obj;
|
|
|
|
|
|
|
|
const keyArr = namePathList;
|
|
|
|
let i = 0;
|
|
|
|
try {
|
|
|
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
|
|
if (!tempObj && !strict) break;
|
2020-10-19 16:43:10 +08:00
|
|
|
const key = keyArr[i];
|
2020-07-28 17:10:06 +08:00
|
|
|
if (key in tempObj) {
|
|
|
|
tempObj = tempObj[key];
|
|
|
|
} else {
|
|
|
|
if (strict) {
|
|
|
|
throw Error('please transfer a valid name path to form item!');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strict && !tempObj) {
|
|
|
|
throw Error('please transfer a valid name path to form item!');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('please transfer a valid name path to form item!');
|
|
|
|
}
|
2019-09-11 18:11:48 +08:00
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
return {
|
|
|
|
o: tempObj,
|
|
|
|
k: keyArr[i],
|
|
|
|
v: tempObj ? tempObj[keyArr[i]] : undefined,
|
|
|
|
};
|
2019-09-11 18:11:48 +08:00
|
|
|
}
|
2021-02-07 18:32:29 +08:00
|
|
|
export const formItemProps = {
|
2020-03-07 19:45:13 +08:00
|
|
|
htmlFor: PropTypes.string,
|
2018-05-05 17:00:51 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2020-10-15 17:58:05 +08:00
|
|
|
label: PropTypes.VNodeChild,
|
|
|
|
help: PropTypes.VNodeChild,
|
|
|
|
extra: PropTypes.VNodeChild,
|
2020-10-19 16:43:10 +08:00
|
|
|
labelCol: { type: Object as PropType<ColProps> },
|
|
|
|
wrapperCol: { type: Object as PropType<ColProps> },
|
2020-10-15 17:58:05 +08:00
|
|
|
hasFeedback: PropTypes.looseBool.def(false),
|
2020-10-10 18:16:28 +08:00
|
|
|
colon: PropTypes.looseBool,
|
2020-10-15 17:58:05 +08:00
|
|
|
labelAlign: PropTypes.oneOf(tuple('left', 'right')),
|
2020-10-19 16:43:10 +08:00
|
|
|
prop: { type: [String, Number, Array] as PropType<string | number | string[] | number[]> },
|
|
|
|
name: { type: [String, Number, Array] as PropType<string | number | string[] | number[]> },
|
2020-07-28 17:10:06 +08:00
|
|
|
rules: PropTypes.oneOfType([Array, Object]),
|
2020-10-15 17:58:05 +08:00
|
|
|
autoLink: PropTypes.looseBool.def(true),
|
2020-10-10 18:16:28 +08:00
|
|
|
required: PropTypes.looseBool,
|
|
|
|
validateFirst: PropTypes.looseBool,
|
2020-10-15 17:58:05 +08:00
|
|
|
validateStatus: PropTypes.oneOf(tuple('', 'success', 'warning', 'error', 'validating')),
|
2020-10-19 16:43:10 +08:00
|
|
|
validateTrigger: { type: [String, Array] as PropType<string | string[]> },
|
|
|
|
messageVariables: { type: Object as PropType<Record<string, string>> },
|
2021-06-07 17:35:03 +08:00
|
|
|
hidden: Boolean,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2021-02-07 18:32:29 +08:00
|
|
|
export type FormItemProps = Partial<ExtractPropTypes<typeof formItemProps>>;
|
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
let indexGuid = 0;
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
// default form item id prefix.
|
|
|
|
const defaultItemNamePrefixCls = 'form_item';
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
export default defineComponent({
|
2018-05-05 17:00:51 +08:00
|
|
|
name: 'AFormItem',
|
2020-07-06 22:31:07 +08:00
|
|
|
inheritAttrs: false,
|
2020-07-28 17:10:06 +08:00
|
|
|
__ANT_NEW_FORM_ITEM: true,
|
2021-02-07 18:32:29 +08:00
|
|
|
props: formItemProps,
|
2021-06-07 17:35:03 +08:00
|
|
|
slots: ['help', 'label', 'extra'],
|
2021-06-09 11:33:08 +08:00
|
|
|
setup(props, { slots, attrs, expose }) {
|
2021-06-07 17:35:03 +08:00
|
|
|
warning(props.prop === undefined, `\`prop\` is deprecated. Please use \`name\` instead.`);
|
|
|
|
const eventKey = `form-item-${++indexGuid}`;
|
|
|
|
const { prefixCls } = useConfigInject('form', props);
|
|
|
|
const formContext = useInjectForm();
|
2020-10-19 16:43:10 +08:00
|
|
|
const fieldName = computed(() => props.name || props.prop);
|
2021-06-07 17:35:03 +08:00
|
|
|
const errors = ref([]);
|
|
|
|
const validateDisabled = ref(false);
|
|
|
|
const domErrorVisible = ref(false);
|
|
|
|
const inputRef = ref();
|
2020-10-19 16:43:10 +08:00
|
|
|
const namePath = computed(() => {
|
|
|
|
const val = fieldName.value;
|
|
|
|
return getNamePath(val);
|
|
|
|
});
|
|
|
|
const fieldId = computed(() => {
|
2021-09-25 16:51:32 +08:00
|
|
|
if (!namePath.value.length) {
|
2020-07-28 17:10:06 +08:00
|
|
|
return undefined;
|
|
|
|
} else {
|
2021-06-07 17:35:03 +08:00
|
|
|
const formName = formContext.name.value;
|
2020-10-15 17:58:05 +08:00
|
|
|
const mergedId = namePath.value.join('_');
|
2021-09-25 16:51:32 +08:00
|
|
|
return formName ? `${formName}_${mergedId}` : `${defaultItemNamePrefixCls}_${mergedId}`;
|
2020-07-28 17:10:06 +08:00
|
|
|
}
|
2020-10-19 16:43:10 +08:00
|
|
|
});
|
|
|
|
const fieldValue = computed(() => {
|
2021-06-07 17:35:03 +08:00
|
|
|
const model = formContext.model.value;
|
2020-10-15 17:58:05 +08:00
|
|
|
if (!model || !fieldName.value) {
|
2020-07-28 17:10:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-10-15 17:58:05 +08:00
|
|
|
return getPropByPath(model, namePath.value, true).v;
|
2020-10-19 16:43:10 +08:00
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
|
|
|
|
const initialValue = ref(cloneDeep(fieldValue.value));
|
2020-10-15 17:58:05 +08:00
|
|
|
const mergedValidateTrigger = computed(() => {
|
|
|
|
let validateTrigger =
|
2021-06-07 17:35:03 +08:00
|
|
|
props.validateTrigger !== undefined
|
|
|
|
? props.validateTrigger
|
|
|
|
: formContext.validateTrigger.value;
|
2020-10-15 17:58:05 +08:00
|
|
|
validateTrigger = validateTrigger === undefined ? 'change' : validateTrigger;
|
|
|
|
return toArray(validateTrigger);
|
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
const rulesRef = computed<ValidationRule[]>(() => {
|
|
|
|
let formRules = formContext.rules.value;
|
2020-10-15 17:58:05 +08:00
|
|
|
const selfRules = props.rules;
|
|
|
|
const requiredRule =
|
2020-10-19 16:43:10 +08:00
|
|
|
props.required !== undefined
|
2020-10-15 17:58:05 +08:00
|
|
|
? { required: !!props.required, trigger: mergedValidateTrigger.value }
|
|
|
|
: [];
|
|
|
|
const prop = getPropByPath(formRules, namePath.value);
|
|
|
|
formRules = formRules ? prop.o[prop.k] || prop.v : [];
|
|
|
|
const rules = [].concat(selfRules || formRules || []);
|
|
|
|
if (find(rules, rule => rule.required)) {
|
|
|
|
return rules;
|
|
|
|
} else {
|
|
|
|
return rules.concat(requiredRule);
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
});
|
2020-10-15 17:58:05 +08:00
|
|
|
const isRequired = computed(() => {
|
2021-06-07 17:35:03 +08:00
|
|
|
const rules = rulesRef.value;
|
2020-07-28 17:10:06 +08:00
|
|
|
let isRequired = false;
|
|
|
|
if (rules && rules.length) {
|
|
|
|
rules.every(rule => {
|
|
|
|
if (rule.required) {
|
|
|
|
isRequired = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2020-10-15 17:58:05 +08:00
|
|
|
return isRequired || props.required;
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const validateState = ref();
|
|
|
|
watchEffect(() => {
|
|
|
|
validateState.value = props.validateStatus;
|
|
|
|
});
|
|
|
|
|
|
|
|
const validateRules = (options: ValidateOptions) => {
|
|
|
|
const { validateFirst = false, messageVariables } = props;
|
2020-07-28 17:10:06 +08:00
|
|
|
const { triggerName } = options || {};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
let filteredRules = rulesRef.value;
|
2020-07-28 17:10:06 +08:00
|
|
|
if (triggerName) {
|
|
|
|
filteredRules = filteredRules.filter(rule => {
|
|
|
|
const { trigger } = rule;
|
2021-06-07 17:35:03 +08:00
|
|
|
if (!trigger && !mergedValidateTrigger.value.length) {
|
2020-07-28 17:10:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
const triggerList = toArray(trigger || mergedValidateTrigger.value);
|
2020-07-28 17:10:06 +08:00
|
|
|
return triggerList.includes(triggerName);
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
}
|
2020-07-28 17:10:06 +08:00
|
|
|
if (!filteredRules.length) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
const promise = validateRulesUtil(
|
|
|
|
namePath.value,
|
|
|
|
fieldValue.value,
|
|
|
|
filteredRules as RuleObject[],
|
2020-07-28 17:10:06 +08:00
|
|
|
options,
|
|
|
|
validateFirst,
|
|
|
|
messageVariables,
|
|
|
|
);
|
2021-06-07 17:35:03 +08:00
|
|
|
validateState.value = 'validating';
|
|
|
|
errors.value = [];
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
promise
|
|
|
|
.catch(e => e)
|
2021-06-28 11:21:23 +08:00
|
|
|
.then((results: RuleError[] = []) => {
|
2021-06-07 17:35:03 +08:00
|
|
|
if (validateState.value === 'validating') {
|
2021-06-28 11:21:23 +08:00
|
|
|
const res = results.filter(result => result && result.errors.length);
|
|
|
|
validateState.value = res.length ? 'error' : 'success';
|
|
|
|
|
|
|
|
errors.value = res.map(r => r.errors);
|
2020-07-28 17:10:06 +08:00
|
|
|
}
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
return promise;
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const onFieldBlur = () => {
|
|
|
|
validateRules({ triggerName: 'blur' });
|
|
|
|
};
|
|
|
|
const onFieldChange = () => {
|
|
|
|
if (validateDisabled.value) {
|
|
|
|
validateDisabled.value = false;
|
2020-07-28 17:10:06 +08:00
|
|
|
return;
|
2020-03-07 19:45:13 +08:00
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
validateRules({ triggerName: 'change' });
|
|
|
|
};
|
|
|
|
const clearValidate = () => {
|
|
|
|
validateState.value = '';
|
|
|
|
validateDisabled.value = false;
|
|
|
|
errors.value = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetField = () => {
|
|
|
|
validateState.value = '';
|
|
|
|
validateDisabled.value = true;
|
|
|
|
errors.value = [];
|
|
|
|
const model = formContext.model.value || {};
|
|
|
|
const value = fieldValue.value;
|
|
|
|
const prop = getPropByPath(model, namePath.value, true);
|
2020-07-28 17:10:06 +08:00
|
|
|
if (Array.isArray(value)) {
|
2021-06-07 17:35:03 +08:00
|
|
|
prop.o[prop.k] = [].concat(initialValue.value);
|
2020-07-28 17:10:06 +08:00
|
|
|
} else {
|
2021-06-07 17:35:03 +08:00
|
|
|
prop.o[prop.k] = initialValue.value;
|
2020-03-07 19:45:13 +08:00
|
|
|
}
|
2020-07-28 17:10:06 +08:00
|
|
|
// reset validateDisabled after onFieldChange triggered
|
2020-10-28 15:31:45 +08:00
|
|
|
nextTick(() => {
|
2021-06-07 17:35:03 +08:00
|
|
|
validateDisabled.value = false;
|
2020-07-28 17:10:06 +08:00
|
|
|
});
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
const onLabelClick = () => {
|
|
|
|
const id = fieldId.value;
|
|
|
|
if (!id || !inputRef.value) {
|
2020-03-07 19:45:13 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
const control = inputRef.value.$el.querySelector(`[id="${id}"]`);
|
2020-03-07 19:45:13 +08:00
|
|
|
if (control && control.focus) {
|
|
|
|
control.focus();
|
|
|
|
}
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
2021-06-09 11:33:08 +08:00
|
|
|
expose({
|
|
|
|
onFieldBlur,
|
|
|
|
onFieldChange,
|
|
|
|
clearValidate,
|
|
|
|
resetField,
|
|
|
|
});
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
useProvideFormItemContext(
|
|
|
|
{
|
|
|
|
id: fieldId,
|
|
|
|
onFieldBlur: () => {
|
|
|
|
if (props.autoLink) {
|
|
|
|
onFieldBlur();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFieldChange: () => {
|
|
|
|
if (props.autoLink) {
|
|
|
|
onFieldChange();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
clearValidate,
|
|
|
|
},
|
|
|
|
computed(() => {
|
|
|
|
return !!(props.autoLink && formContext.model.value && fieldName.value);
|
|
|
|
}),
|
|
|
|
);
|
2021-07-31 22:41:36 +08:00
|
|
|
let registered = false;
|
|
|
|
watch(
|
2021-06-07 17:35:03 +08:00
|
|
|
fieldName,
|
2021-07-31 22:41:36 +08:00
|
|
|
val => {
|
|
|
|
if (val) {
|
|
|
|
if (!registered) {
|
|
|
|
registered = true;
|
|
|
|
formContext.addField(eventKey, {
|
|
|
|
fieldValue,
|
|
|
|
fieldId,
|
|
|
|
fieldName,
|
|
|
|
resetField,
|
|
|
|
clearValidate,
|
|
|
|
namePath,
|
|
|
|
validateRules,
|
|
|
|
rules: rulesRef,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registered = false;
|
|
|
|
formContext.removeField(eventKey);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ immediate: true },
|
|
|
|
);
|
2021-06-07 17:35:03 +08:00
|
|
|
onBeforeUnmount(() => {
|
|
|
|
formContext.removeField(eventKey);
|
|
|
|
});
|
|
|
|
const itemClassName = computed(() => ({
|
|
|
|
[`${prefixCls.value}-item`]: true,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
// Status
|
|
|
|
[`${prefixCls.value}-item-has-feedback`]: validateState.value && props.hasFeedback,
|
|
|
|
[`${prefixCls.value}-item-has-success`]: validateState.value === 'success',
|
|
|
|
[`${prefixCls.value}-item-has-warning`]: validateState.value === 'warning',
|
|
|
|
[`${prefixCls.value}-item-has-error`]: validateState.value === 'error',
|
|
|
|
[`${prefixCls.value}-item-is-validating`]: validateState.value === 'validating',
|
|
|
|
[`${prefixCls.value}-item-hidden`]: props.hidden,
|
|
|
|
}));
|
|
|
|
return () => {
|
|
|
|
const help = props.help ?? (slots.help ? filterEmpty(slots.help()) : null);
|
2018-05-05 17:00:51 +08:00
|
|
|
return (
|
2021-06-07 17:35:03 +08:00
|
|
|
<Row
|
2021-06-09 10:31:56 +08:00
|
|
|
{...attrs}
|
2021-06-07 17:35:03 +08:00
|
|
|
class={[
|
|
|
|
itemClassName.value,
|
|
|
|
domErrorVisible.value || !!help ? `${prefixCls.value}-item-with-help` : '',
|
2021-06-09 10:31:56 +08:00
|
|
|
attrs.class,
|
2021-06-07 17:35:03 +08:00
|
|
|
]}
|
|
|
|
key="row"
|
2021-09-25 16:51:32 +08:00
|
|
|
v-slots={{
|
|
|
|
default: () => (
|
|
|
|
<>
|
|
|
|
{/* Label */}
|
|
|
|
<FormItemLabel
|
|
|
|
{...props}
|
|
|
|
htmlFor={fieldId.value}
|
|
|
|
required={isRequired.value}
|
|
|
|
requiredMark={formContext.requiredMark.value}
|
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
onClick={onLabelClick}
|
|
|
|
label={props.label ?? slots.label?.()}
|
|
|
|
/>
|
|
|
|
{/* Input Group */}
|
|
|
|
<FormItemInput
|
|
|
|
{...props}
|
|
|
|
errors={help !== undefined && help !== null ? toArray(help) : errors.value}
|
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
status={validateState.value}
|
|
|
|
onDomErrorVisibleChange={(v: boolean) => (domErrorVisible.value = v)}
|
|
|
|
validateStatus={validateState.value}
|
|
|
|
ref={inputRef}
|
|
|
|
help={help}
|
|
|
|
extra={props.extra ?? slots.extra?.()}
|
|
|
|
v-slots={{ default: slots.default }}
|
|
|
|
// v-slots={{ default: () => [firstChildren, children.slice(1)] }}
|
|
|
|
></FormItemInput>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
></Row>
|
2020-03-07 19:45:13 +08:00
|
|
|
);
|
2021-06-07 17:35:03 +08:00
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
});
|