From 35741983c865c2771972a6f45d31d536790b37f2 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Tue, 1 Dec 2026 01:45:39 +0530 Subject: [PATCH] fix: find the scrollTo row by key without building a CSS selector 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. --- src/Table.tsx | 6 +++--- tests/refs.spec.tsx | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Table.tsx b/src/Table.tsx index 6fdbe1e35..0a1f02016 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -369,9 +369,9 @@ const Table = ( scrollBodyRef.current?.scrollTo({ top }); } else { const mergedKey = key ?? getRowKey(mergedData[index]); - const targetElement = scrollBodyRef.current.querySelector( - `[data-row-key="${mergedKey}"]`, - ); + const targetElement = Array.from( + scrollBodyRef.current.querySelectorAll('[data-row-key]'), + ).find(element => element.getAttribute('data-row-key') === String(mergedKey)); if (targetElement) { targetElement.scrollIntoView({ block: align }); if (offset) { diff --git a/tests/refs.spec.tsx b/tests/refs.spec.tsx index 2afcb72f7..1df89d973 100644 --- a/tests/refs.spec.tsx +++ b/tests/refs.spec.tsx @@ -207,4 +207,28 @@ describe('Table.Ref', () => { ref.current.scrollTo({ index: 0, align: 'nearest', offset: -10 }); expect(scrollParam.top).toEqual(-10); }); + + it('support scrollTo key with special characters', () => { + const ref = React.createRef(); + const keys = ['say "hi"', 'C:\\Users', 'line1\nline2', 'a\\b']; + + render( + ({ key }))]} + columns={[{ dataIndex: 'key' }]} + ref={ref} + scroll={{ y: 10 }} + />, + ); + + keys.forEach(key => { + scrollIntoViewElement = null; + ref.current.scrollTo({ key }); + expect(scrollIntoViewElement.getAttribute('data-row-key')).toEqual(key); + + scrollIntoViewElement = null; + ref.current.scrollTo({ index: keys.indexOf(key) + 1 }); + expect(scrollIntoViewElement.getAttribute('data-row-key')).toEqual(key); + }); + }); });