Skip to content

Commit c34a446

Browse files
committed
fix: avoid update when formitem from slot
1 parent de68d08 commit c34a446

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

components/form/FormItem.jsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ export const FormItemProps = {
3535
fieldDecoratorId: PropTypes.string,
3636
fieldDecoratorOptions: PropTypes.object,
3737
};
38+
function comeFromSlot(vnodes = [], itemVnode) {
39+
let isSlot = false;
40+
for (let i = 0, len = vnodes.length; i < len; i++) {
41+
const vnode = vnodes[i];
42+
if (vnode && (vnode === itemVnode || vnode.$vnode === itemVnode)) {
43+
isSlot = true;
44+
} else {
45+
const children = vnode.componentOptions ? vnode.componentOptions.children : vnode.children;
46+
isSlot = comeFromSlot(children, itemVnode);
47+
}
48+
if (isSlot) {
49+
break;
50+
}
51+
}
52+
return isSlot;
53+
}
3854

3955
export default {
4056
name: 'AFormItem',
@@ -51,7 +67,20 @@ export default {
5167
collectFormItemContext: { default: () => noop },
5268
},
5369
data() {
54-
this.collectFormItemContext(this.$vnode.context);
70+
const { templateContext = {} } = this.FormProps.form || {};
71+
const vnodes = Object.values(templateContext.$slots || {}).reduce((a, b) => {
72+
return [...a, ...b];
73+
}, []);
74+
const isSlot = comeFromSlot(vnodes, this.$vnode);
75+
warning(!isSlot, 'You can not set FormItem from slot, please use slot-scope instead slot');
76+
let isSlotScope = false;
77+
// 进一步判断是否是通过slot-scope传递
78+
if (!isSlot && this.$vnode.context !== templateContext) {
79+
isSlotScope = comeFromSlot(this.$vnode.context.$children, templateContext.$vnode);
80+
}
81+
if (!isSlotScope && !isSlot) {
82+
this.collectFormItemContext(this.$vnode.context);
83+
}
5584
return { helpShow: false };
5685
},
5786
beforeDestroy() {

0 commit comments

Comments
 (0)