Skip to content

Commit d31bfef

Browse files
刘欢claude
andcommitted
fix: refine scrollIntoView with offset handling
- Simplify scrollY demo buttons - Update README to note virtual table doesn't support center align - Refactor offset logic: call scrollIntoView first, then apply offset via scrollTo 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3230c6b commit d31bfef

File tree

4 files changed

+25
-96
lines changed

4 files changed

+25
-96
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ tblRef.current?.scrollTo({ key: 'rowKey', align: 'start' });
136136
| top | number | - | Scroll to specific top position (in px) |
137137
| key | string | - | Scroll to row by row key |
138138
| offset | number | - | Additional offset from target position |
139-
| align | `start` \| `center` \| `end` \| `nearest` | `nearest` | Alignment of the target element within the scroll container. `start` aligns to top, `center` to middle, `end` to bottom, `nearest` automatically chooses the closest alignment |
139+
| align | `start` \| `center` \| `end` \| `nearest` | `nearest` | Alignment of the target element within the scroll container. `start` aligns to top, `center` to middle, `end` to bottom, `nearest` automatically chooses the closest alignment. Note: Virtual table does not support `center`. |
140140
141141
## Column Props
142142

docs/examples/scrollY.tsx

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -41,103 +41,35 @@ const Test = () => {
4141
return (
4242
<div>
4343
<h2>scroll body table</h2>
44-
<button
45-
onClick={() => {
46-
tblRef.current?.scrollTo({
47-
top: 9999,
48-
});
49-
}}
50-
>
51-
Scroll To End
44+
<button onClick={() => tblRef.current?.scrollTo({ top: 0 })}>Top</button>
45+
<button onClick={() => tblRef.current?.scrollTo({ top: 9999 })}>End</button>
46+
<button onClick={() => tblRef.current?.scrollTo({ key: 9 })}>Key 9</button>
47+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'start' })}>
48+
Key 9 align: start
5249
</button>
53-
<button
54-
onClick={() => {
55-
tblRef.current?.scrollTo({
56-
key: 9,
57-
});
58-
}}
59-
>
60-
Scroll To key 9
50+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'center' })}>
51+
Key 9 align: center
6152
</button>
62-
<button
63-
onClick={() => {
64-
tblRef.current?.scrollTo({
65-
top: 0,
66-
});
67-
}}
68-
>
69-
Scroll To top
53+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'end' })}>
54+
Key 9 align: end
7055
</button>
71-
<button
72-
onClick={() => {
73-
tblRef.current?.scrollTo({
74-
index: 5,
75-
offset: -10,
76-
});
77-
}}
78-
>
79-
Scroll To Index 5 + Offset -10
56+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'nearest' })}>
57+
Key 9 align: nearest
8058
</button>
81-
<button
82-
onClick={() => {
83-
tblRef.current?.scrollTo({
84-
key: 6,
85-
offset: -10,
86-
});
87-
}}
88-
>
89-
Scroll To Key 6 + Offset -10
59+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'start', offset: 20 })}>
60+
Key 9 start offset20
9061
</button>
91-
<button
92-
onClick={() => {
93-
tblRef.current?.scrollTo({
94-
key: 9,
95-
align: 'start',
96-
});
97-
}}
98-
>
99-
Scroll To key 9 (align: start)
62+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'center', offset: 20 })}>
63+
Key 9 center offset20
10064
</button>
101-
<button
102-
onClick={() => {
103-
tblRef.current?.scrollTo({
104-
key: 9,
105-
align: 'center',
106-
});
107-
}}
108-
>
109-
Scroll To key 9 (align: center)
65+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'end', offset: 20 })}>
66+
Key 9 end offset20
11067
</button>
111-
<button
112-
onClick={() => {
113-
tblRef.current?.scrollTo({
114-
key: 9,
115-
align: 'end',
116-
});
117-
}}
118-
>
119-
Scroll To key 9 (align: end)
68+
<button onClick={() => tblRef.current?.scrollTo({ key: 9, align: 'nearest', offset: 20 })}>
69+
Key 9 nearest offset20
12070
</button>
121-
<button
122-
onClick={() => {
123-
tblRef.current?.scrollTo({
124-
key: 9,
125-
align: 'nearest',
126-
});
127-
}}
128-
>
129-
Scroll To key 9 (align: nearest)
130-
</button>
131-
<button
132-
onClick={() => {
133-
tblRef.current?.scrollTo({
134-
index: 9,
135-
offset: 50,
136-
align: 'nearest',
137-
});
138-
}}
139-
>
140-
Scroll To index 9 + offset 50 (align: nearest)
71+
<button onClick={() => tblRef.current?.scrollTo({ index: 5, offset: 50 })}>
72+
Index 5 + Offset 50
14173
</button>
14274
<Table
14375
ref={tblRef}

src/Table.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ const Table = <RecordType extends DefaultRecordType>(
351351
scrollTo: config => {
352352
if (scrollBodyRef.current instanceof HTMLElement) {
353353
// Native scroll
354-
const { index, top, key, offset, align } = config;
354+
const { index, top, key, offset, align = 'nearest' } = config;
355355

356356
if (validNumberValue(top)) {
357357
// In top mode, offset is ignored
@@ -362,16 +362,14 @@ const Table = <RecordType extends DefaultRecordType>(
362362
`[data-row-key="${mergedKey}"]`,
363363
);
364364
if (targetElement) {
365-
if (!offset) {
366-
targetElement.scrollIntoView({ block: align ?? 'nearest' });
367-
} else {
365+
targetElement.scrollIntoView({ block: align });
366+
if (offset) {
368367
const container = scrollBodyRef.current;
369368
const elementTop = (targetElement as HTMLElement).offsetTop;
370369
const elementHeight = (targetElement as HTMLElement).offsetHeight;
371370
const containerHeight = container.clientHeight;
372371
const elementBottom = elementTop + elementHeight;
373372
let targetTop: number;
374-
375373
switch (align) {
376374
case 'nearest': {
377375
const currentTop = container.scrollTop;

tests/refs.spec.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ describe('Table.Ref', () => {
169169

170170
// align start + offset 20 = 0 + 20 = 20
171171
ref.current.scrollTo({ index: 0, align: 'start', offset: 20 });
172-
expect(scrollIntoViewElement).toBeNull();
173172
expect(scrollParam.top).toEqual(20);
174173

175174
// align center + offset 30 = 0 + 30 = 30

0 commit comments

Comments
 (0)