Keyed reorder moves the row that moved earlier, so a focused input in it loses focus (React keeps focus)

オープン
#150 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
75/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, react, typescript

調査の方向性

Start by reading placeDomsInOrder in dist/dom/reconcile.js and the keyed reorder test setup using Vitest, jsdom, and Testing Library described in the issue. Reproduce [a, b] to [b, a] with focus on b, then inspect which DOM row the reconciliation moves. Done when the reorder preserves the focused input and the regression test passes.

索引モデルが issue の本文から書いたものです。

説明

Version: @tanstack/redact 0.3.0

When a keyed list is reordered, Redact moves a different DOM node than React does. If the node it moves contains the focused element, the browser blurs it (moving a focused node with insertBefore drops focus in Chromium, Firefox and WebKit), so focus ends up on <body>. React keeps focus in the same case.

Repro (Vitest + jsdom + Testing Library)
import { render, screen } from '@testing-library/react';
import { expect, test } from 'vitest';

test('a keyed reorder keeps focus in the row that moves earlier', () => {
  const list = (order) => (
    <div>{order.map((id) => <div key={id}><input aria-label={id} /></div>)}</div>
  );
  const { rerender } = render(list(['a', 'b']));
  const b = screen.getByLabelText('b');
  b.focus();
  rerender(list(['b', 'a']));
  expect(screen.getByLabelText('b')).toBe(b);   // passes on both: the node is kept
  expect(document.activeElement).toBe(b);       // React: b. Redact: body
});

React 19.2.8 passes; Redact 0.3.0 fails the focus assertion.

Where it seems to come from

For [a, b] -> [b, a], React leaves the rows whose relative order is unchanged in place and moves the ones that fall behind (it moves a). placeDomsInOrder in dist/dom/reconcile.js walks backwards and calls insertBefore on rows that move earlier (it moves b), which takes focus away from the row the user is working in. The same happens when an item is moved to the top of a list.

Matching React's placement (walk the new order forwards, track the highest current DOM index among rows left in place, and move only rows whose current index is below it) would keep focus. moveBefore could help where it exists, but jsdom and some browsers don't have it.

Real-world impact

Keyboard users lose focus when the row they're in moves up: "move up" buttons, lists that re-sort, or lists that put the newest item first while a control in a row has focus. Scroll position and media inside the moved row can also reset.

主要言語
TypeScript
スター
581
フォーク
28
平均マージ
13時間 48分
マージ済み PR(30日)
45

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。