ant-design-vue/components/form-model/FormItem.jsx

499 lines
15 KiB
Vue
Raw Normal View History

2020-07-13 10:56:31 +00:00
import { inject, provide, Transition } from 'vue';
import cloneDeep from 'lodash/cloneDeep';
import PropTypes from '../_util/vue-types';
2020-07-13 10:56:31 +00:00
import classNames from 'classnames';
import getTransitionProps from '../_util/getTransitionProps';
import Row from '../grid/Row';
import Col, { ColProps } from '../grid/Col';
2020-07-14 10:39:43 +00:00
import hasProp, {
initDefaultProps,
2020-07-13 10:56:31 +00:00
findDOMNode,
2020-07-06 14:31:07 +00:00
getComponent,
getOptionProps,
getEvents,
isValidElement,
2020-07-06 14:31:07 +00:00
getSlot,
} from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
import { cloneElement } from '../_util/vnode';
2020-07-13 10:56:31 +00: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-13 15:55:46 +00:00
import { validateRules } from './utils/validateUtil';
import { getNamePath } from './utils/valueUtil';
import { toArray } from './utils/typeUtil';
2020-07-14 10:39:43 +00:00
import { warning } from '../vc-util/warning';
2020-07-13 10:56:31 +00:00
const iconMap = {
success: CheckCircleFilled,
warning: ExclamationCircleFilled,
error: CloseCircleFilled,
validating: LoadingOutlined,
};
2020-07-14 10:39:43 +00:00
function getPropByPath(obj, namePathList, strict) {
let tempObj = obj;
2020-07-14 10:39:43 +00:00
const keyArr = namePathList;
let i = 0;
2020-07-14 10:39:43 +00:00
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;
}
}
2020-07-14 10:39:43 +00:00
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!');
}
2020-07-14 10:39:43 +00:00
return {
o: tempObj,
k: keyArr[i],
2020-07-14 10:39:43 +00:00
v: tempObj ? tempObj[keyArr[i]] : undefined,
};
}
export const FormItemProps = {
id: PropTypes.string,
htmlFor: PropTypes.string,
prefixCls: PropTypes.string,
label: PropTypes.any,
help: PropTypes.any,
extra: PropTypes.any,
labelCol: PropTypes.shape(ColProps).loose,
wrapperCol: PropTypes.shape(ColProps).loose,
hasFeedback: PropTypes.bool,
colon: PropTypes.bool,
labelAlign: PropTypes.oneOf(['left', 'right']),
2020-07-14 10:39:43 +00:00
prop: PropTypes.oneOfType([Array, String, Number]),
name: PropTypes.oneOfType([Array, String, Number]),
rules: PropTypes.oneOfType([Array, Object]),
autoLink: PropTypes.bool,
required: PropTypes.bool,
2020-07-10 10:26:35 +00:00
validateFirst: PropTypes.bool,
validateStatus: PropTypes.oneOf(['', 'success', 'warning', 'error', 'validating']),
};
export default {
2020-03-16 04:06:55 +00:00
name: 'AFormModelItem',
mixins: [BaseMixin],
2020-07-06 14:31:07 +00:00
inheritAttrs: false,
__ANT_NEW_FORM_ITEM: true,
props: initDefaultProps(FormItemProps, {
hasFeedback: false,
autoLink: true,
}),
2020-07-06 14:31:07 +00:00
setup() {
return {
2020-07-13 10:56:31 +00:00
isFormItemChildren: inject('isFormItemChildren', false),
2020-07-06 14:31:07 +00:00
configProvider: inject('configProvider', ConfigConsumerProps),
FormContext: inject('FormContext', {}),
};
},
data() {
2020-07-14 10:39:43 +00:00
warning(hasProp(this, 'prop'), `\`prop\` is deprecated. Please use \`name\` instead.`);
return {
validateState: this.validateStatus,
validateMessage: '',
validateDisabled: false,
validator: {},
2020-07-13 10:56:31 +00:00
helpShow: false,
2020-07-13 15:55:46 +00:00
errors: [],
};
},
computed: {
2020-07-14 10:39:43 +00:00
fieldName() {
return this.name || this.prop;
},
namePath() {
return getNamePath(this.fieldName);
},
2020-07-13 10:56:31 +00:00
fieldId() {
2020-07-14 10:39:43 +00:00
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;
}
2020-07-13 10:56:31 +00:00
},
fieldValue() {
2020-03-16 05:27:38 +00:00
const model = this.FormContext.model;
2020-07-14 10:39:43 +00:00
if (!model || !this.fieldName) {
return;
}
2020-07-14 10:39:43 +00:00
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;
});
}
2020-07-13 10:56:31 +00:00
return isRequired || this.required;
},
},
watch: {
validateStatus(val) {
this.validateState = val;
},
},
2020-07-13 10:56:31 +00:00
created() {
provide('isFormItemChildren', true);
},
mounted() {
2020-07-14 10:39:43 +00:00
if (this.fieldName) {
2020-03-16 05:27:38 +00:00
const { addField } = this.FormContext;
addField && addField(this);
this.initialValue = cloneDeep(this.fieldValue);
}
},
2020-06-11 08:13:09 +00:00
beforeUnmount() {
2020-03-16 05:27:38 +00:00
const { removeField } = this.FormContext;
removeField && removeField(this);
},
methods: {
2020-07-13 15:55:46 +00:00
getNamePath() {
2020-07-14 10:39:43 +00:00
const { fieldName } = this;
2020-07-13 15:55:46 +00:00
const { prefixName = [] } = this.FormContext;
2020-07-13 10:56:31 +00:00
2020-07-14 10:39:43 +00:00
return fieldName !== undefined ? [...prefixName, ...this.namePath] : [];
2020-07-13 10:56:31 +00:00
},
2020-07-13 15:55:46 +00:00
validateRules(options) {
const { validateFirst = false, messageVariables } = this.$props;
const { triggerName } = options || {};
const namePath = this.getNamePath();
let filteredRules = this.getRules();
if (triggerName) {
filteredRules = filteredRules.filter(rule => {
2020-07-14 10:39:43 +00:00
const { trigger } = rule;
if (!trigger) {
2020-07-13 15:55:46 +00:00
return true;
2020-07-13 10:56:31 +00:00
}
2020-07-14 10:39:43 +00:00
const triggerList = toArray(trigger);
2020-07-13 15:55:46 +00:00
return triggerList.includes(triggerName);
2020-07-13 10:56:31 +00:00
});
}
2020-07-14 10:39:43 +00:00
if (!filteredRules.length) {
return Promise.resolve();
}
2020-07-13 15:55:46 +00:00
const promise = validateRules(
namePath,
this.fieldValue,
filteredRules,
options,
validateFirst,
messageVariables,
);
this.validateState = 'validating';
2020-07-13 15:55:46 +00:00
this.errors = [];
2020-07-13 10:56:31 +00:00
promise
2020-07-13 15:55:46 +00:00
.catch(e => e)
.then((errors = []) => {
if (this.validateState === 'validating') {
this.validateState = errors.length ? 'error' : 'success';
this.validateMessage = errors[0];
this.errors = errors;
}
2020-07-13 10:56:31 +00:00
});
2020-07-13 15:55:46 +00:00
2020-07-13 10:56:31 +00:00
return promise;
},
getRules() {
2020-03-16 05:27:38 +00:00
let formRules = this.FormContext.rules;
const selfRules = this.rules;
const requiredRule =
this.required !== undefined ? { required: !!this.required, trigger: 'change' } : [];
2020-07-14 10:39:43 +00:00
const prop = getPropByPath(formRules, this.namePath);
formRules = formRules ? prop.o[prop.k] || prop.v : [];
return [].concat(selfRules || formRules || []).concat(requiredRule);
},
getFilteredRule(trigger) {
const rules = this.getRules();
return rules
.filter(rule => {
if (!rule.trigger || trigger === '') return true;
if (Array.isArray(rule.trigger)) {
return rule.trigger.indexOf(trigger) > -1;
} else {
return rule.trigger === trigger;
}
})
.map(rule => ({ ...rule }));
},
onFieldBlur() {
2020-07-13 15:55:46 +00:00
this.validateRules({ triggerName: 'blur' });
},
onFieldChange() {
if (this.validateDisabled) {
this.validateDisabled = false;
return;
}
2020-07-13 15:55:46 +00:00
this.validateRules({ triggerName: 'change' });
},
clearValidate() {
this.validateState = '';
this.validateMessage = '';
this.validateDisabled = false;
},
resetField() {
this.validateState = '';
this.validateMessage = '';
2020-07-14 10:39:43 +00:00
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;
}
// reset validateDisabled after onFieldChange triggered
this.$nextTick(() => {
this.validateDisabled = false;
});
},
2020-07-13 10:56:31 +00:00
getHelpMessage() {
const help = getComponent(this, 'help');
return this.validateMessage || help;
},
onLabelClick() {
const id = this.fieldId;
if (!id) {
return;
}
const formItemNode = findDOMNode(this);
const control = formItemNode.querySelector(`[id="${id}"]`);
if (control && control.focus) {
control.focus();
}
},
onHelpAnimEnd(_key, helpShow) {
this.helpShow = helpShow;
if (!helpShow) {
this.$forceUpdate();
}
},
renderHelp(prefixCls) {
const help = this.getHelpMessage();
const children = help ? (
<div class={`${prefixCls}-explain`} key="help">
{help}
</div>
) : null;
if (children) {
this.helpShow = !!children;
}
const transitionProps = getTransitionProps('show-help', {
onAfterEnter: () => this.onHelpAnimEnd('help', true),
onAfterLeave: () => this.onHelpAnimEnd('help', false),
});
return (
<Transition {...transitionProps} key="help">
{children}
</Transition>
);
},
renderExtra(prefixCls) {
const extra = getComponent(this, 'extra');
return extra ? <div class={`${prefixCls}-extra`}>{extra}</div> : null;
},
renderValidateWrapper(prefixCls, c1, c2, c3) {
const validateStatus = this.validateState;
let classes = `${prefixCls}-item-control`;
if (validateStatus) {
classes = classNames(`${prefixCls}-item-control`, {
'has-feedback': this.hasFeedback || validateStatus === 'validating',
'has-success': validateStatus === 'success',
'has-warning': validateStatus === 'warning',
'has-error': validateStatus === 'error',
'is-validating': validateStatus === 'validating',
});
}
const IconNode = validateStatus && iconMap[validateStatus];
const icon =
this.hasFeedback && IconNode ? (
<span class={`${prefixCls}-item-children-icon`}>
<IconNode />
</span>
) : null;
return (
<div class={classes}>
<span class={`${prefixCls}-item-children`}>
{c1}
{icon}
</span>
{c2}
{c3}
</div>
);
},
renderWrapper(prefixCls, children) {
const { wrapperCol: contextWrapperCol } = this.isFormItemChildren ? {} : this.FormContext;
const { wrapperCol } = this;
const mergedWrapperCol = wrapperCol || contextWrapperCol || {};
const { style, id, ...restProps } = mergedWrapperCol;
const className = classNames(`${prefixCls}-item-control-wrapper`, mergedWrapperCol.class);
const colProps = {
...restProps,
class: className,
key: 'wrapper',
style,
id,
};
return <Col {...colProps}>{children}</Col>;
},
renderLabel(prefixCls) {
const {
vertical,
labelAlign: contextLabelAlign,
labelCol: contextLabelCol,
colon: contextColon,
} = this.FormContext;
const { labelAlign, labelCol, colon, fieldId, htmlFor } = this;
const label = getComponent(this, 'label');
const required = this.isRequired;
const mergedLabelCol = labelCol || contextLabelCol || {};
const mergedLabelAlign = labelAlign || contextLabelAlign;
const labelClsBasic = `${prefixCls}-item-label`;
const labelColClassName = classNames(
labelClsBasic,
mergedLabelAlign === 'left' && `${labelClsBasic}-left`,
mergedLabelCol.class,
);
const {
class: labelColClass,
style: labelColStyle,
id: labelColId,
...restProps
} = mergedLabelCol;
let labelChildren = label;
// Keep label is original where there should have no colon
const computedColon = colon === true || (contextColon !== false && colon !== false);
const haveColon = computedColon && !vertical;
// Remove duplicated user input colon
if (haveColon && typeof label === 'string' && label.trim() !== '') {
labelChildren = label.replace(/[:]\s*$/, '');
}
const labelClassName = classNames({
[`${prefixCls}-item-required`]: required,
[`${prefixCls}-item-no-colon`]: !computedColon,
});
const colProps = {
...restProps,
class: labelColClassName,
key: 'label',
style: labelColStyle,
id: labelColId,
};
return label ? (
<Col {...colProps}>
<label
for={htmlFor || fieldId}
class={labelClassName}
title={typeof label === 'string' ? label : ''}
onClick={this.onLabelClick}
>
{labelChildren}
</label>
</Col>
) : null;
},
renderChildren(prefixCls, child) {
return [
this.renderLabel(prefixCls),
this.renderWrapper(
prefixCls,
this.renderValidateWrapper(
prefixCls,
child,
this.renderHelp(prefixCls),
this.renderExtra(prefixCls),
),
),
];
},
renderFormItem(child) {
const { prefixCls: customizePrefixCls } = this.$props;
const { class: className, ...restProps } = this.$attrs;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('form', customizePrefixCls);
const children = this.renderChildren(prefixCls, child);
const itemClassName = {
[className]: className,
[`${prefixCls}-item`]: true,
[`${prefixCls}-item-with-help`]: this.helpShow,
};
return (
<Row class={classNames(itemClassName)} key="row" {...restProps}>
{children}
</Row>
);
},
},
render() {
2020-07-13 10:56:31 +00:00
const { autoLink } = getOptionProps(this);
2020-07-06 14:31:07 +00:00
const children = getSlot(this);
let firstChildren = children[0];
2020-07-14 10:39:43 +00:00
if (this.fieldName && autoLink && isValidElement(firstChildren)) {
const originalEvents = getEvents(firstChildren);
2020-07-06 14:31:07 +00:00
const originalBlur = originalEvents.onBlur;
const originalChange = originalEvents.onChange;
firstChildren = cloneElement(firstChildren, {
2020-07-13 10:56:31 +00:00
...(this.fieldId ? { id: this.fieldId } : undefined),
2020-07-06 14:31:07 +00:00
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);
}
2020-07-06 14:31:07 +00:00
} else if (originalChange) {
originalChange(...args);
}
this.onFieldChange();
},
});
}
2020-07-13 10:56:31 +00:00
return this.renderFormItem([firstChildren, children.slice(1)]);
},
};