Skip to content

Commit c9d3f7b

Browse files
committed
fix: formItem not update when a-form-item in child component #446
1 parent 56e68f1 commit c9d3f7b

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

components/form/Form.jsx

+29-1
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,45 @@ const Form = {
139139
createForm(context, options = {}) {
140140
return new Vue(Form.create({ ...options, templateContext: context })());
141141
},
142+
created() {
143+
this.formItemContexts = new Map();
144+
},
142145
provide() {
143146
return {
144147
FormProps: this.$props,
148+
// https://github.com/vueComponent/ant-design-vue/issues/446
149+
collectFormItemContext:
150+
this.form && this.form.templateContext
151+
? (c, type = 'add') => {
152+
const formItemContexts = this.formItemContexts;
153+
const number = formItemContexts.get(c) || 0;
154+
if (type === 'delete') {
155+
if (number <= 1) {
156+
formItemContexts.delete(c);
157+
} else {
158+
formItemContexts.set(c, number - 1);
159+
}
160+
} else {
161+
if (c !== this.form.templateContext) {
162+
formItemContexts.set(c, number + 1);
163+
}
164+
}
165+
}
166+
: () => {},
145167
};
146168
},
147169
watch: {
148170
form() {
149171
this.$forceUpdate();
150172
},
151173
},
174+
beforeUpdate() {
175+
this.formItemContexts.forEach((number, c) => {
176+
if (c.$forceUpdate) {
177+
c.$forceUpdate();
178+
}
179+
});
180+
},
152181
updated() {
153182
if (this.form && this.form.cleanUpUselessFields) {
154183
this.form.cleanUpUselessFields();
@@ -232,7 +261,6 @@ const Form = {
232261
/>
233262
);
234263
}
235-
236264
return (
237265
<form onSubmit={onSubmit} class={formClassName}>
238266
{$slots.default}

components/form/FormItem.jsx

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import BaseMixin from '../_util/BaseMixin';
1919
import { cloneElement, cloneVNodes } from '../_util/vnode';
2020
import Icon from '../icon';
2121

22+
function noop(){}
2223
export const FormItemProps = {
2324
id: PropTypes.string,
2425
prefixCls: PropTypes.string,
@@ -47,10 +48,15 @@ export default {
4748
inject: {
4849
FormProps: { default: {} },
4950
decoratorFormProps: { default: {} },
51+
collectFormItemContext: { default: () => noop },
5052
},
5153
data() {
54+
this.collectFormItemContext(this.$vnode.context);
5255
return { helpShow: false };
5356
},
57+
beforeDestroy() {
58+
this.collectFormItemContext(this.$vnode.context, 'delete');
59+
},
5460
mounted() {
5561
warning(
5662
this.getControls(this.slotDefault, true).length <= 1,

components/vc-form/src/createBaseForm.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function createBaseForm(option = {}, mixins = []) {
5656
data() {
5757
const fields = mapPropsToFields && mapPropsToFields(this.$props);
5858
this.fieldsStore = createFieldsStore(fields || {});
59-
59+
this.templateContext = templateContext;
6060
this.instances = {};
6161
this.cachedBind = {};
6262
this.clearedFieldMetaCache = {};

0 commit comments

Comments
 (0)