2020-10-22 22:45:21 +08:00
|
|
|
|
import { inject, provide, PropType, defineComponent, computed } 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';
|
2020-08-31 16:53:19 +08:00
|
|
|
|
import classNames from '../_util/classNames';
|
2020-10-22 22:45:21 +08:00
|
|
|
|
import { getTransitionProps, Transition } from '../_util/transition';
|
2019-01-12 11:33:27 +08:00
|
|
|
|
import Row from '../grid/Row';
|
2020-10-02 21:16:05 +08:00
|
|
|
|
import Col from '../grid/Col';
|
2020-07-28 17:10:06 +08:00
|
|
|
|
import hasProp, {
|
|
|
|
|
findDOMNode,
|
2020-07-06 22:31:07 +08:00
|
|
|
|
getComponent,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
getOptionProps,
|
|
|
|
|
getEvents,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
isValidElement,
|
2020-07-06 22:31:07 +08:00
|
|
|
|
getSlot,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
} from '../_util/props-util';
|
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
2020-09-30 10:47:18 +08:00
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-07-28 17:10:06 +08:00
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-03-26 11:56:16 +08:00
|
|
|
|
import CheckCircleFilled from '@ant-design/icons-vue/CheckCircleFilled';
|
|
|
|
|
import ExclamationCircleFilled from '@ant-design/icons-vue/ExclamationCircleFilled';
|
|
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
|
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
2020-07-28 17:10:06 +08:00
|
|
|
|
import { validateRules } from './utils/validateUtil';
|
|
|
|
|
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';
|
2020-10-15 17:58:05 +08:00
|
|
|
|
import { ColProps } from '../grid/col';
|
|
|
|
|
import { tuple, VueNode } from '../_util/type';
|
|
|
|
|
import { ValidateOptions } from './interface';
|
2018-12-19 21:26:32 +08:00
|
|
|
|
|
2020-03-26 11:56:16 +08:00
|
|
|
|
const iconMap = {
|
|
|
|
|
success: CheckCircleFilled,
|
|
|
|
|
warning: ExclamationCircleFilled,
|
|
|
|
|
error: CloseCircleFilled,
|
|
|
|
|
validating: LoadingOutlined,
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-05-05 17:00:51 +08:00
|
|
|
|
export const FormItemProps = {
|
|
|
|
|
id: PropTypes.string,
|
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>> },
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
export default defineComponent({
|
2018-05-05 17:00:51 +08:00
|
|
|
|
name: 'AFormItem',
|
|
|
|
|
mixins: [BaseMixin],
|
2020-07-06 22:31:07 +08:00
|
|
|
|
inheritAttrs: false,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
__ANT_NEW_FORM_ITEM: true,
|
2020-10-15 17:58:05 +08:00
|
|
|
|
props: FormItemProps,
|
|
|
|
|
setup(props) {
|
2020-10-19 16:43:10 +08:00
|
|
|
|
const FormContext = inject('FormContext', {}) as any;
|
|
|
|
|
const fieldName = computed(() => props.name || props.prop);
|
|
|
|
|
const namePath = computed(() => {
|
|
|
|
|
const val = fieldName.value;
|
|
|
|
|
return getNamePath(val);
|
|
|
|
|
});
|
|
|
|
|
const fieldId = computed(() => {
|
|
|
|
|
const { id } = props;
|
2020-10-15 17:58:05 +08:00
|
|
|
|
if (id) {
|
|
|
|
|
return id;
|
|
|
|
|
} else if (!namePath.value.length) {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return undefined;
|
|
|
|
|
} else {
|
2020-10-15 17:58:05 +08:00
|
|
|
|
const formName = FormContext.name;
|
|
|
|
|
const mergedId = namePath.value.join('_');
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return formName ? `${formName}_${mergedId}` : mergedId;
|
|
|
|
|
}
|
2020-10-19 16:43:10 +08:00
|
|
|
|
});
|
|
|
|
|
const fieldValue = computed(() => {
|
2020-10-15 17:58:05 +08:00
|
|
|
|
const model = FormContext.model;
|
|
|
|
|
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
|
|
|
|
});
|
2020-10-15 17:58:05 +08:00
|
|
|
|
const mergedValidateTrigger = computed(() => {
|
|
|
|
|
let validateTrigger =
|
2020-10-19 16:43:10 +08:00
|
|
|
|
props.validateTrigger !== undefined ? props.validateTrigger : FormContext.validateTrigger;
|
2020-10-15 17:58:05 +08:00
|
|
|
|
validateTrigger = validateTrigger === undefined ? 'change' : validateTrigger;
|
|
|
|
|
return toArray(validateTrigger);
|
|
|
|
|
});
|
|
|
|
|
const getRules = () => {
|
|
|
|
|
let formRules = FormContext.rules;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const isRequired = computed(() => {
|
2020-10-19 16:43:10 +08:00
|
|
|
|
const rules = getRules();
|
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;
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
isFormItemChildren: inject('isFormItemChildren', false),
|
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
|
|
|
|
FormContext,
|
|
|
|
|
fieldId,
|
|
|
|
|
fieldName,
|
|
|
|
|
namePath,
|
|
|
|
|
isRequired,
|
|
|
|
|
getRules,
|
|
|
|
|
fieldValue,
|
2020-10-19 16:43:10 +08:00
|
|
|
|
mergedValidateTrigger,
|
2020-10-15 17:58:05 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
data() {
|
|
|
|
|
warning(!hasProp(this, 'prop'), `\`prop\` is deprecated. Please use \`name\` instead.`);
|
|
|
|
|
return {
|
|
|
|
|
validateState: this.validateStatus,
|
|
|
|
|
validateMessage: '',
|
|
|
|
|
validateDisabled: false,
|
|
|
|
|
validator: {},
|
|
|
|
|
helpShow: false,
|
|
|
|
|
errors: [],
|
2020-10-19 16:43:10 +08:00
|
|
|
|
initialValue: undefined,
|
2020-10-15 17:58:05 +08:00
|
|
|
|
};
|
2020-07-28 17:10:06 +08:00
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
validateStatus(val) {
|
|
|
|
|
this.validateState = val;
|
2019-08-30 14:07:32 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
2019-02-01 17:23:00 +08:00
|
|
|
|
created() {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
provide('isFormItemChildren', true);
|
2019-02-01 17:23:00 +08:00
|
|
|
|
},
|
2020-07-28 17:10:06 +08:00
|
|
|
|
mounted() {
|
|
|
|
|
if (this.fieldName) {
|
|
|
|
|
const { addField } = this.FormContext;
|
|
|
|
|
addField && addField(this);
|
|
|
|
|
this.initialValue = cloneDeep(this.fieldValue);
|
2019-01-30 21:31:36 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-06-11 16:13:09 +08:00
|
|
|
|
beforeUnmount() {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const { removeField } = this.FormContext;
|
|
|
|
|
removeField && removeField(this);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
getNamePath() {
|
|
|
|
|
const { fieldName } = this;
|
|
|
|
|
const { prefixName = [] } = this.FormContext;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return fieldName !== undefined ? [...prefixName, ...this.namePath] : [];
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
|
validateRules(options: ValidateOptions) {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const { validateFirst = false, messageVariables } = this.$props;
|
|
|
|
|
const { triggerName } = options || {};
|
|
|
|
|
const namePath = this.getNamePath();
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
|
let filteredRules = this.getRules();
|
|
|
|
|
if (triggerName) {
|
|
|
|
|
filteredRules = filteredRules.filter(rule => {
|
|
|
|
|
const { trigger } = rule;
|
2020-08-14 18:11:30 +08:00
|
|
|
|
if (!trigger && !this.mergedValidateTrigger.length) {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-08-14 18:11:30 +08:00
|
|
|
|
const triggerList = toArray(trigger || this.mergedValidateTrigger);
|
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();
|
|
|
|
|
}
|
|
|
|
|
const promise = validateRules(
|
|
|
|
|
namePath,
|
|
|
|
|
this.fieldValue,
|
|
|
|
|
filteredRules,
|
|
|
|
|
options,
|
|
|
|
|
validateFirst,
|
|
|
|
|
messageVariables,
|
|
|
|
|
);
|
|
|
|
|
this.validateState = 'validating';
|
|
|
|
|
this.errors = [];
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
|
promise
|
|
|
|
|
.catch(e => e)
|
|
|
|
|
.then((errors = []) => {
|
|
|
|
|
if (this.validateState === 'validating') {
|
|
|
|
|
this.validateState = errors.length ? 'error' : 'success';
|
|
|
|
|
this.validateMessage = errors[0];
|
|
|
|
|
this.errors = errors;
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return promise;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-07-28 17:10:06 +08:00
|
|
|
|
getRules() {
|
|
|
|
|
let formRules = this.FormContext.rules;
|
|
|
|
|
const selfRules = this.rules;
|
|
|
|
|
const requiredRule =
|
2020-08-14 18:11:30 +08:00
|
|
|
|
this.required !== undefined
|
|
|
|
|
? { required: !!this.required, trigger: this.mergedValidateTrigger }
|
|
|
|
|
: [];
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const prop = getPropByPath(formRules, this.namePath);
|
|
|
|
|
formRules = formRules ? prop.o[prop.k] || prop.v : [];
|
2020-08-05 15:44:52 +08:00
|
|
|
|
const rules = [].concat(selfRules || formRules || []);
|
|
|
|
|
if (find(rules, rule => rule.required)) {
|
|
|
|
|
return rules;
|
|
|
|
|
} else {
|
|
|
|
|
return rules.concat(requiredRule);
|
|
|
|
|
}
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-07-28 17:10:06 +08:00
|
|
|
|
onFieldBlur() {
|
|
|
|
|
this.validateRules({ triggerName: 'blur' });
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-07-28 17:10:06 +08:00
|
|
|
|
onFieldChange() {
|
|
|
|
|
if (this.validateDisabled) {
|
|
|
|
|
this.validateDisabled = false;
|
|
|
|
|
return;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
}
|
2020-07-28 17:10:06 +08:00
|
|
|
|
this.validateRules({ triggerName: 'change' });
|
|
|
|
|
},
|
|
|
|
|
clearValidate() {
|
|
|
|
|
this.validateState = '';
|
|
|
|
|
this.validateMessage = '';
|
|
|
|
|
this.validateDisabled = false;
|
|
|
|
|
},
|
|
|
|
|
resetField() {
|
|
|
|
|
this.validateState = '';
|
|
|
|
|
this.validateMessage = '';
|
|
|
|
|
const model = this.FormContext.model || {};
|
|
|
|
|
const value = this.fieldValue;
|
|
|
|
|
const prop = getPropByPath(model, this.namePath, true);
|
|
|
|
|
this.validateDisabled = true;
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
prop.o[prop.k] = [].concat(this.initialValue);
|
|
|
|
|
} else {
|
|
|
|
|
prop.o[prop.k] = this.initialValue;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
}
|
2020-07-28 17:10:06 +08:00
|
|
|
|
// reset validateDisabled after onFieldChange triggered
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
this.validateDisabled = false;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
getHelpMessage() {
|
|
|
|
|
const help = getComponent(this, 'help');
|
|
|
|
|
|
|
|
|
|
return this.validateMessage || help;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-03-18 14:52:02 +08:00
|
|
|
|
onLabelClick() {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const id = this.fieldId;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
if (!id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-06 22:31:07 +08:00
|
|
|
|
const formItemNode = findDOMNode(this);
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const control = formItemNode.querySelector(`[id="${id}"]`);
|
|
|
|
|
if (control && control.focus) {
|
|
|
|
|
control.focus();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
onHelpAnimEnd(_key: string, helpShow: boolean) {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
this.helpShow = helpShow;
|
2018-09-05 21:28:54 +08:00
|
|
|
|
if (!helpShow) {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
this.$forceUpdate();
|
2018-09-05 21:28:54 +08:00
|
|
|
|
}
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderHelp(prefixCls: string) {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
const help = this.getHelpMessage();
|
2018-05-05 17:00:51 +08:00
|
|
|
|
const children = help ? (
|
2019-01-12 11:33:27 +08:00
|
|
|
|
<div class={`${prefixCls}-explain`} key="help">
|
2018-05-05 17:00:51 +08:00
|
|
|
|
{help}
|
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
) : null;
|
2018-05-05 21:38:03 +08:00
|
|
|
|
if (children) {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
this.helpShow = !!children;
|
2018-05-05 21:38:03 +08:00
|
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
const transitionProps = getTransitionProps('show-help', {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
onAfterEnter: () => this.onHelpAnimEnd('help', true),
|
|
|
|
|
onAfterLeave: () => this.onHelpAnimEnd('help', false),
|
2019-01-12 11:33:27 +08:00
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
return (
|
2020-07-06 22:31:07 +08:00
|
|
|
|
<Transition {...transitionProps} key="help">
|
2018-05-05 17:00:51 +08:00
|
|
|
|
{children}
|
2020-07-06 22:31:07 +08:00
|
|
|
|
</Transition>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderExtra(prefixCls: string) {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
const extra = getComponent(this, 'extra');
|
2019-01-12 11:33:27 +08:00
|
|
|
|
return extra ? <div class={`${prefixCls}-extra`}>{extra}</div> : null;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderValidateWrapper(prefixCls: string, c1: VueNode, c2: VueNode, c3: VueNode) {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const validateStatus = this.validateState;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
let classes = `${prefixCls}-item-control`;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
if (validateStatus) {
|
2019-04-20 14:19:13 +08:00
|
|
|
|
classes = classNames(`${prefixCls}-item-control`, {
|
2020-10-27 23:02:45 +08:00
|
|
|
|
'has-feedback': validateStatus && this.hasFeedback,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
'has-success': validateStatus === 'success',
|
|
|
|
|
'has-warning': validateStatus === 'warning',
|
|
|
|
|
'has-error': validateStatus === 'error',
|
|
|
|
|
'is-validating': validateStatus === 'validating',
|
2019-01-12 11:33:27 +08:00
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
}
|
2020-03-26 11:56:16 +08:00
|
|
|
|
const IconNode = validateStatus && iconMap[validateStatus];
|
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
const icon =
|
2020-07-28 17:10:06 +08:00
|
|
|
|
this.hasFeedback && IconNode ? (
|
2019-04-20 14:19:13 +08:00
|
|
|
|
<span class={`${prefixCls}-item-children-icon`}>
|
2020-03-26 11:56:16 +08:00
|
|
|
|
<IconNode />
|
2019-01-12 11:33:27 +08:00
|
|
|
|
</span>
|
|
|
|
|
) : null;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
return (
|
|
|
|
|
<div class={classes}>
|
2019-04-20 14:19:13 +08:00
|
|
|
|
<span class={`${prefixCls}-item-children`}>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
{c1}
|
|
|
|
|
{icon}
|
2018-12-19 21:26:32 +08:00
|
|
|
|
</span>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
{c2}
|
|
|
|
|
{c3}
|
2018-05-05 17:00:51 +08:00
|
|
|
|
</div>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderWrapper(prefixCls: string, children: VueNode) {
|
2020-10-19 16:43:10 +08:00
|
|
|
|
const { wrapperCol: contextWrapperCol } = (this.isFormItemChildren
|
|
|
|
|
? {}
|
|
|
|
|
: this.FormContext) as any;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const { wrapperCol } = this;
|
|
|
|
|
const mergedWrapperCol = wrapperCol || contextWrapperCol || {};
|
2020-07-06 22:31:07 +08:00
|
|
|
|
const { style, id, ...restProps } = mergedWrapperCol;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const className = classNames(`${prefixCls}-item-control-wrapper`, mergedWrapperCol.class);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
const colProps = {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
...restProps,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
class: className,
|
|
|
|
|
key: 'wrapper',
|
|
|
|
|
style,
|
|
|
|
|
id,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
|
|
|
|
return <Col {...colProps}>{children}</Col>;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderLabel(prefixCls: string) {
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const {
|
|
|
|
|
vertical,
|
|
|
|
|
labelAlign: contextLabelAlign,
|
|
|
|
|
labelCol: contextLabelCol,
|
|
|
|
|
colon: contextColon,
|
2020-03-15 13:12:47 +08:00
|
|
|
|
} = this.FormContext;
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const { labelAlign, labelCol, colon, fieldId, htmlFor } = this;
|
2020-07-06 22:31:07 +08:00
|
|
|
|
const label = getComponent(this, 'label');
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const required = this.isRequired;
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const mergedLabelCol = labelCol || contextLabelCol || {};
|
|
|
|
|
|
|
|
|
|
const mergedLabelAlign = labelAlign || contextLabelAlign;
|
|
|
|
|
const labelClsBasic = `${prefixCls}-item-label`;
|
|
|
|
|
const labelColClassName = classNames(
|
|
|
|
|
labelClsBasic,
|
|
|
|
|
mergedLabelAlign === 'left' && `${labelClsBasic}-left`,
|
|
|
|
|
mergedLabelCol.class,
|
|
|
|
|
);
|
2019-01-12 11:33:27 +08:00
|
|
|
|
const {
|
|
|
|
|
class: labelColClass,
|
|
|
|
|
style: labelColStyle,
|
|
|
|
|
id: labelColId,
|
|
|
|
|
...restProps
|
2020-03-07 19:45:13 +08:00
|
|
|
|
} = mergedLabelCol;
|
2019-01-12 11:33:27 +08:00
|
|
|
|
let labelChildren = label;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
// Keep label is original where there should have no colon
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const computedColon = colon === true || (contextColon !== false && colon !== false);
|
|
|
|
|
const haveColon = computedColon && !vertical;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
// Remove duplicated user input colon
|
|
|
|
|
if (haveColon && typeof label === 'string' && label.trim() !== '') {
|
2020-03-07 19:45:13 +08:00
|
|
|
|
labelChildren = label.replace(/[::]\s*$/, '');
|
2018-05-05 17:00:51 +08:00
|
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
|
|
const labelClassName = classNames({
|
|
|
|
|
[`${prefixCls}-item-required`]: required,
|
|
|
|
|
[`${prefixCls}-item-no-colon`]: !computedColon,
|
|
|
|
|
});
|
2018-05-05 17:00:51 +08:00
|
|
|
|
const colProps = {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
...restProps,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
class: labelColClassName,
|
|
|
|
|
key: 'label',
|
|
|
|
|
style: labelColStyle,
|
|
|
|
|
id: labelColId,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
|
|
|
|
return label ? (
|
2019-01-12 11:33:27 +08:00
|
|
|
|
<Col {...colProps}>
|
2018-05-05 17:00:51 +08:00
|
|
|
|
<label
|
2020-07-28 17:10:06 +08:00
|
|
|
|
for={htmlFor || fieldId}
|
2018-05-05 17:00:51 +08:00
|
|
|
|
class={labelClassName}
|
|
|
|
|
title={typeof label === 'string' ? label : ''}
|
|
|
|
|
onClick={this.onLabelClick}
|
|
|
|
|
>
|
|
|
|
|
{labelChildren}
|
|
|
|
|
</label>
|
|
|
|
|
</Col>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
) : null;
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderChildren(prefixCls: string, child: VueNode) {
|
2018-05-05 17:00:51 +08:00
|
|
|
|
return [
|
2019-04-20 14:19:13 +08:00
|
|
|
|
this.renderLabel(prefixCls),
|
2018-05-05 17:00:51 +08:00
|
|
|
|
this.renderWrapper(
|
2019-04-20 14:19:13 +08:00
|
|
|
|
prefixCls,
|
|
|
|
|
this.renderValidateWrapper(
|
|
|
|
|
prefixCls,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
child,
|
2019-04-20 14:19:13 +08:00
|
|
|
|
this.renderHelp(prefixCls),
|
2019-05-28 11:37:38 +08:00
|
|
|
|
this.renderExtra(prefixCls),
|
2019-04-20 14:19:13 +08:00
|
|
|
|
),
|
2018-05-05 17:00:51 +08:00
|
|
|
|
),
|
2019-01-12 11:33:27 +08:00
|
|
|
|
];
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
|
renderFormItem(child: any[]) {
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
2020-10-15 17:58:05 +08:00
|
|
|
|
const { class: className, ...restProps } = this.$attrs as any;
|
2019-09-11 22:35:25 +08:00
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-04-20 14:19:13 +08:00
|
|
|
|
const prefixCls = getPrefixCls('form', customizePrefixCls);
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const children = this.renderChildren(prefixCls, child);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
const itemClassName = {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
[className]: className,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
[`${prefixCls}-item`]: true,
|
2018-09-05 21:28:54 +08:00
|
|
|
|
[`${prefixCls}-item-with-help`]: this.helpShow,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
|
return (
|
2020-07-06 22:31:07 +08:00
|
|
|
|
<Row class={classNames(itemClassName)} key="row" {...restProps}>
|
2020-03-07 19:45:13 +08:00
|
|
|
|
{children}
|
|
|
|
|
</Row>
|
|
|
|
|
);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
render() {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
const { autoLink } = getOptionProps(this);
|
|
|
|
|
const children = getSlot(this);
|
|
|
|
|
let firstChildren = children[0];
|
|
|
|
|
if (this.fieldName && autoLink && isValidElement(firstChildren)) {
|
|
|
|
|
const originalEvents = getEvents(firstChildren);
|
|
|
|
|
const originalBlur = originalEvents.onBlur;
|
|
|
|
|
const originalChange = originalEvents.onChange;
|
|
|
|
|
firstChildren = cloneElement(firstChildren, {
|
|
|
|
|
...(this.fieldId ? { id: this.fieldId } : undefined),
|
2020-10-15 17:58:05 +08:00
|
|
|
|
onBlur: (...args: any[]) => {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
originalBlur && originalBlur(...args);
|
|
|
|
|
this.onFieldBlur();
|
|
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
|
onChange: (...args: any[]) => {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
if (Array.isArray(originalChange)) {
|
|
|
|
|
for (let i = 0, l = originalChange.length; i < l; i++) {
|
|
|
|
|
originalChange[i](...args);
|
|
|
|
|
}
|
|
|
|
|
} else if (originalChange) {
|
|
|
|
|
originalChange(...args);
|
|
|
|
|
}
|
|
|
|
|
this.onFieldChange();
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-06-22 21:35:14 +08:00
|
|
|
|
}
|
2020-07-28 17:10:06 +08:00
|
|
|
|
return this.renderFormItem([firstChildren, children.slice(1)]);
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2020-10-15 17:58:05 +08:00
|
|
|
|
});
|