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); + }); + }); });