2020-07-28 17:10:06 +08:00
|
|
|
|
import { inject, provide, Transition } 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-07-28 17:10:06 +08:00
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps';
|
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, {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
initDefaultProps,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
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';
|
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-07-28 17:10:06 +08:00
|
|
|
|
function getPropByPath(obj, namePathList, strict) {
|
|
|
|
|
let tempObj = obj;
|
|
|
|
|
|
|
|
|
|
const keyArr = namePathList;
|
|
|
|
|
let i = 0;
|
|
|
|
|
try {
|
|
|
|
|
for (let len = keyArr.length; i < len - 1; ++i) {
|
|
|
|
|
if (!tempObj && !strict) break;
|
|
|
|
|
let key = keyArr[i];
|
|
|
|
|
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,
|
|
|
|
|
label: PropTypes.any,
|
|
|
|
|
help: PropTypes.any,
|
|
|
|
|
extra: PropTypes.any,
|
2020-10-02 21:16:05 +08:00
|
|
|
|
labelCol: PropTypes.object,
|
|
|
|
|
wrapperCol: PropTypes.object,
|
2020-10-10 18:16:28 +08:00
|
|
|
|
hasFeedback: PropTypes.looseBool,
|
|
|
|
|
colon: PropTypes.looseBool,
|
2020-03-07 19:45:13 +08:00
|
|
|
|
labelAlign: PropTypes.oneOf(['left', 'right']),
|
2020-07-28 17:10:06 +08:00
|
|
|
|
prop: PropTypes.oneOfType([Array, String, Number]),
|
|
|
|
|
name: PropTypes.oneOfType([Array, String, Number]),
|
|
|
|
|
rules: PropTypes.oneOfType([Array, Object]),
|
2020-10-10 18:16:28 +08:00
|
|
|
|
autoLink: PropTypes.looseBool,
|
|
|
|
|
required: PropTypes.looseBool,
|
|
|
|
|
validateFirst: PropTypes.looseBool,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
validateStatus: PropTypes.oneOf(['', 'success', 'warning', 'error', 'validating']),
|
2020-08-14 18:11:30 +08:00
|
|
|
|
validateTrigger: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
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,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
props: initDefaultProps(FormItemProps, {
|
|
|
|
|
hasFeedback: false,
|
2020-07-28 17:10:06 +08:00
|
|
|
|
autoLink: true,
|
2018-05-05 17:00:51 +08:00
|
|
|
|
}),
|
2020-07-06 22:31:07 +08:00
|
|
|
|
setup() {
|
2020-03-07 19:45:13 +08:00
|
|
|
|
return {
|
2020-07-06 22:31:07 +08:00
|
|
|
|
isFormItemChildren: inject('isFormItemChildren', false),
|
2020-09-30 10:47:18 +08:00
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-07-28 17:10:06 +08:00
|
|
|
|
FormContext: inject('FormContext', {}),
|
2020-03-07 19:45:13 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
data() {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
warning(!hasProp(this, 'prop'), `\`prop\` is deprecated. Please use \`name\` instead.`);
|
|
|
|
|
return {
|
|
|
|
|
validateState: this.validateStatus,
|
|
|
|
|
validateMessage: '',
|
|
|
|
|
validateDisabled: false,
|
|
|
|
|
validator: {},
|
|
|
|
|
helpShow: false,
|
|
|
|
|
errors: [],
|
|
|
|
|
};
|
2018-05-05 17:00:51 +08:00
|
|
|
|
},
|
2019-08-30 14:07:32 +08:00
|
|
|
|
computed: {
|
2020-07-28 17:10:06 +08:00
|
|
|
|
fieldName() {
|
|
|
|
|
return this.name || this.prop;
|
|
|
|
|
},
|
|
|
|
|
namePath() {
|
|
|
|
|
return getNamePath(this.fieldName);
|
|
|
|
|
},
|
|
|
|
|
fieldId() {
|
|
|
|
|
if (this.id) {
|
|
|
|
|
return this.id;
|
|
|
|
|
} else if (!this.namePath.length) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else {
|
|
|
|
|
const formName = this.FormContext.name;
|
|
|
|
|
const mergedId = this.namePath.join('_');
|
|
|
|
|
return formName ? `${formName}_${mergedId}` : mergedId;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fieldValue() {
|
|
|
|
|
const model = this.FormContext.model;
|
|
|
|
|
if (!model || !this.fieldName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return getPropByPath(model, this.namePath, true).v;
|
|
|
|
|
},
|
|
|
|
|
isRequired() {
|
|
|
|
|
let rules = this.getRules();
|
|
|
|
|
let isRequired = false;
|
|
|
|
|
if (rules && rules.length) {
|
|
|
|
|
rules.every(rule => {
|
|
|
|
|
if (rule.required) {
|
|
|
|
|
isRequired = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return isRequired || this.required;
|
|
|
|
|
},
|
2020-08-14 18:11:30 +08:00
|
|
|
|
mergedValidateTrigger() {
|
|
|
|
|
let validateTrigger =
|
|
|
|
|
this.validateTrigger !== undefined
|
|
|
|
|
? this.validateTrigger
|
|
|
|
|
: this.FormContext.validateTrigger;
|
|
|
|
|
validateTrigger = validateTrigger === undefined ? 'change' : validateTrigger;
|
|
|
|
|
return toArray(validateTrigger);
|
|
|
|
|
},
|
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-07-28 17:10:06 +08:00
|
|
|
|
validateRules(options) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
onHelpAnimEnd(_key, helpShow) {
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
renderHelp(prefixCls) {
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
renderExtra(prefixCls) {
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
renderValidateWrapper(prefixCls, c1, c2, c3) {
|
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-07-28 17:10:06 +08:00
|
|
|
|
'has-feedback': this.hasFeedback || validateStatus === 'validating',
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
renderWrapper(prefixCls, children) {
|
2020-03-15 13:12:47 +08:00
|
|
|
|
const { wrapperCol: contextWrapperCol } = this.isFormItemChildren ? {} : this.FormContext;
|
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
|
|
|
|
},
|
|
|
|
|
|
2019-04-20 14:19:13 +08:00
|
|
|
|
renderLabel(prefixCls) {
|
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-07-28 17:10:06 +08:00
|
|
|
|
renderChildren(prefixCls, child) {
|
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-07-28 17:10:06 +08:00
|
|
|
|
renderFormItem(child) {
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
2020-07-06 22:31:07 +08:00
|
|
|
|
const { class: className, ...restProps } = this.$attrs;
|
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),
|
|
|
|
|
onBlur: (...args) => {
|
|
|
|
|
originalBlur && originalBlur(...args);
|
|
|
|
|
this.onFieldBlur();
|
|
|
|
|
},
|
|
|
|
|
onChange: (...args) => {
|
|
|
|
|
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
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|