fix: formItem not update when a-form-item in child component #446

pull/471/head
tangjinzhou 2019-01-27 21:47:27 +08:00
parent 56e68f19a7
commit c9d3f7b79c
3 changed files with 36 additions and 2 deletions

View File

@ -139,9 +139,31 @@ const Form = {
createForm(context, options = {}) { createForm(context, options = {}) {
return new Vue(Form.create({ ...options, templateContext: context })()); return new Vue(Form.create({ ...options, templateContext: context })());
}, },
created() {
this.formItemContexts = new Map();
},
provide() { provide() {
return { return {
FormProps: this.$props, FormProps: this.$props,
// https://github.com/vueComponent/ant-design-vue/issues/446
collectFormItemContext:
this.form && this.form.templateContext
? (c, type = 'add') => {
const formItemContexts = this.formItemContexts;
const number = formItemContexts.get(c) || 0;
if (type === 'delete') {
if (number <= 1) {
formItemContexts.delete(c);
} else {
formItemContexts.set(c, number - 1);
}
} else {
if (c !== this.form.templateContext) {
formItemContexts.set(c, number + 1);
}
}
}
: () => {},
}; };
}, },
watch: { watch: {
@ -149,6 +171,13 @@ const Form = {
this.$forceUpdate(); this.$forceUpdate();
}, },
}, },
beforeUpdate() {
this.formItemContexts.forEach((number, c) => {
if (c.$forceUpdate) {
c.$forceUpdate();
}
});
},
updated() { updated() {
if (this.form && this.form.cleanUpUselessFields) { if (this.form && this.form.cleanUpUselessFields) {
this.form.cleanUpUselessFields(); this.form.cleanUpUselessFields();
@ -232,7 +261,6 @@ const Form = {
/> />
); );
} }
return ( return (
<form onSubmit={onSubmit} class={formClassName}> <form onSubmit={onSubmit} class={formClassName}>
{$slots.default} {$slots.default}

View File

@ -19,6 +19,7 @@ import BaseMixin from '../_util/BaseMixin';
import { cloneElement, cloneVNodes } from '../_util/vnode'; import { cloneElement, cloneVNodes } from '../_util/vnode';
import Icon from '../icon'; import Icon from '../icon';
function noop(){}
export const FormItemProps = { export const FormItemProps = {
id: PropTypes.string, id: PropTypes.string,
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
@ -47,10 +48,15 @@ export default {
inject: { inject: {
FormProps: { default: {} }, FormProps: { default: {} },
decoratorFormProps: { default: {} }, decoratorFormProps: { default: {} },
collectFormItemContext: { default: () => noop },
}, },
data() { data() {
this.collectFormItemContext(this.$vnode.context);
return { helpShow: false }; return { helpShow: false };
}, },
beforeDestroy() {
this.collectFormItemContext(this.$vnode.context, 'delete');
},
mounted() { mounted() {
warning( warning(
this.getControls(this.slotDefault, true).length <= 1, this.getControls(this.slotDefault, true).length <= 1,

View File

@ -56,7 +56,7 @@ function createBaseForm(option = {}, mixins = []) {
data() { data() {
const fields = mapPropsToFields && mapPropsToFields(this.$props); const fields = mapPropsToFields && mapPropsToFields(this.$props);
this.fieldsStore = createFieldsStore(fields || {}); this.fieldsStore = createFieldsStore(fields || {});
this.templateContext = templateContext;
this.instances = {}; this.instances = {};
this.cachedBind = {}; this.cachedBind = {};
this.clearedFieldMetaCache = {}; this.clearedFieldMetaCache = {};