Skip to content

fix: find the scrollTo row by key without building a CSS selector - #1528

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/scroll-to-key-special-chars
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/scroll-to-key-special-chars

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Nov 30, 2026 •

Copy link
Copy Markdown

ref.scrollTo({ key }) (and scrollTo({ index }), which turns the index into a row key) finds the row with querySelector('[data-row-key="${mergedKey}"]'), with the key written into the selector as it is. A row key that contains a double quote or a newline makes an invalid selector and scrollTo throws, and a key with a backslash silently matches no row:

ref.scrollTo({ key: 'say "hi"' })
SyntaxError: Invalid selector [data-row-key="say "hi""]

ref.scrollTo({ key: 'C:\\Users' })   // no row is scrolled to

Row keys come from data (file names, paths, ids), so this is reachable with the documented scrollTo API. It is the same class of bug as react-component/cascader#674, tabs#1031 and menu#907.

scrollTo now takes the [data-row-key] nodes of the body and picks the one whose attribute equals String(mergedKey), so no selector is built from keys. BodyRow writes data-row-key={rowKey}, which React stringifies, so the comparison matches what was rendered.

Test: support scrollTo key with special characters in tests/refs.spec.tsx fails on master (the SyntaxError above, and expected null for the backslash key) and passes now. npx vitest run (21 files, 239 tests), tsc --noEmit, eslint (0 errors; 6 warnings that are also there without this change) and prettier on the changed files pass.

Summary by CodeRabbit

  • Bug Fixes
    • 修复行键包含引号、反斜杠或换行符时,表格无法正确滚动定位到目标行的问题。现在通过行键或行索引滚动,均可准确定位。

scrollTo({ key }) and scrollTo({ index }) looked the row up with querySelector('[data-row-key="${key}"]'). A row key with a double quote or a newline threw SyntaxError: Invalid selector, and a key with a backslash silently matched nothing. Compare the data-row-key attribute instead.
@vercel

vercel Bot commented Nov 30, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the afc163's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

scrollTo 现在遍历带有 data-row-key 的元素,并与目标键的字符串值精确比较。新增测试覆盖包含双引号、反斜杠和换行符的行键,分别验证按 key 和按索引滚动。

Changes

行键滚动定位

层 / 文件 摘要
精确匹配行键并验证滚动
src/Table.tsx, tests/refs.spec.tsx
scrollTo 改为遍历 [data-row-key] 元素,并比较属性值与 String(mergedKey)。新增测试验证特殊字符行键按 key 和按索引定位时,目标元素保留原始 data-row-key。

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: zombiej

Merge Risk: Minimal · up to 35741

This fixes scrollTo for row keys containing special characters, which previously threw or matched no row. The change is small and localized, and a test covers it, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: find the scrollTo row by key without building a CSS selector”与变更内容一致。变更将 scrollTo 的行查找从 CSS 属性选择器改为遍历 [data-row-key] 节点并比较属性值,并新增特殊字符 key 的滚动测试。标题简洁地说明了主要变更。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子看见行键轻轻跳,
双引号和反斜杠也能找到家。
换行藏在键里,不再迷路,
按键或索引,都能滚到目标。
测试点点头,胡萝卜香。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 80619e60-2b8a-41bd-b6b0-20e2624d67ac
Commits

Reviewing files that changed from the base of the PR and between b1ef288 and 3574198.

Files selected for processing: 2

  • src/Table.tsx
  • tests/refs.spec.tsx

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant