A removed input's onBlur runs during the commit that removes it (Chrome)

未关闭
#186 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
30/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend

调研方向

Start with commitBeforeMutationEffects and dispatchEvent in the event-system code, which show why Redact's listener runs while React's does not, then look at commitMutation where the proposed depth counter would go. The branch linked in the issue has a fix and tests in tests/deleted-element-events.test.tsx, so reviewing it or running the browser suite via scripts/actions-browser.config.mjs is the practical entry point. Done means the removed-input blur case no longer fires onBlur in Chrome, and the layout-effect blur still dispatches.

由索引模型根据 Issue 内容生成。

描述

Chrome fires blur on a focused element just before removing it from the DOM. When a Redact commit removes a focused input, Redact runs that input's onBlur mid-commit, with the props of a view the user just closed. If the handler sets state, it can undo the update that removed it.

React never calls it. commitBeforeMutationEffects turns its event system off (_enabled = false) until the mutations finish, and dispatchEvent returns early while it is off (react-dom-client.development.js in react-dom 19.3.1).

function Form() {
  const [open, setOpen] = useState(true)
  if (!open) return <p>closed</p>
  return (
    <div>
      <input onBlur={() => console.log('blur')} />
      <button onMouseDown={e => e.preventDefault()} onClick={() => setOpen(false)}>Done</button>
    </div>
  )
}

Focus the input and click Done in Chrome. React 19.3.1 logs nothing. Redact on main logs blur.

We hit it in a sheet whose field marks itself touched on blur: confirming the sheet removed the field, its onBlur ran, and the sheet reopened the form.

Fix

Branch with the fix and tests, ready to merge (I can't open a PR here): https://github.com/TanStack/redact/compare/main...WolfieLeader:redact:fix/skip-events-on-deleted-elements

What changes
  • commitMutation runs the mutation queue and placements inside runMutations, which raises a depth counter.
  • Redact's event listener returns early while the counter is above zero.
  • Effects, refs and autoFocus run after the mutations, so their focus events still dispatch, as in React.
Testing
  • tests/deleted-element-events.test.tsx has two cases: a click that removes a focused input, and a layout-effect cleanup that blurs an input being removed. The first needs Chrome's removal blur, so it only proves something in the browser run; I added the file to scripts/actions-browser.config.mjs.
  • Both fail on main and pass with the fix, in jsdom and in Chrome. React 19.3.1 passes both in Chrome.
  • onFocus from autoFocus and from focus() in useLayoutEffect still fire, as in React.
  • pnpm test:ci passes on Node 24.22.0. redact/dom-client grows 27 B gzip, within budget.
  • Not run: the Firefox and WebKit browser jobs. scripts/test-react-compatibility-size.mjs fails locally on unchanged main (server/index +67 B), so I left that check to CI.
主要语言
TypeScript
星标
640
派生
32
平均合并
1 小时 15 分钟
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/redact 的其他 Issue

查看 TanStack/redact 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。