Skip to content

Commit 7e45837

Browse files
fix(Form): outer component being incorrectly triggered by nested component (#4243)
* fix(Form): outer component being incorrectly triggered by nested component * chore: stash changelog [ci skip] --------- Co-authored-by: tdesign-bot <tdesign@tencent.com>
1 parent 03b4189 commit 7e45837

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/components/form/Form.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ const Form = forwardRefWithStatics(
6666
form?.getInternalHooks?.(HOOK_MARK)?.flashQueue?.();
6767
}, [form]);
6868

69+
// Dialog / Popup 等通过 Portal 渲染时,DOM 已脱离外层 Form,但 React 合成事件仍沿 Fiber 树冒泡
70+
// 会导致内层 Form 的 reset / submit 误触发外层 Form 的处理逻辑
71+
// 这里过滤掉来自嵌套 Form 的伪冒泡事件
72+
function isEventFromSelf(e: React.FormEvent<HTMLFormElement>) {
73+
return e?.target === formRef.current;
74+
}
75+
6976
function onResetHandler(e: React.FormEvent<HTMLFormElement>) {
77+
if (!isEventFromSelf(e)) return;
7078
[...formMapRef.current.values()].forEach((formItemRef) => {
7179
formItemRef?.current.resetField();
7280
});
@@ -76,6 +84,11 @@ const Form = forwardRefWithStatics(
7684
onReset?.({ e });
7785
}
7886

87+
function onSubmitHandler(e: React.FormEvent<HTMLFormElement>) {
88+
if (!isEventFromSelf(e)) return;
89+
formInstance.submit(e);
90+
}
91+
7992
function onFormItemValueChange(changedValue: Record<string, unknown>) {
8093
const allFields = formInstance.getFieldsValue(true);
8194
onValuesChange(changedValue, allFields);
@@ -110,7 +123,7 @@ const Form = forwardRefWithStatics(
110123
id={id}
111124
style={style}
112125
className={formClass}
113-
onSubmit={formInstance.submit}
126+
onSubmit={onSubmitHandler}
114127
onReset={onResetHandler}
115128
>
116129
{children}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
pr_number: 4243
3+
contributor: RylanBot
4+
---
5+
6+
- fix(Form): 修复 Portal 场景下,内部组件的 `reset``submit` 事件冒泡导致外层组件事件被误触发的问题 @RylanBot ([#4243](https://github.com/Tencent/tdesign-react/pull/4243))

0 commit comments

Comments
 (0)