Skip to content

Commit 3230c6b

Browse files
刘欢claude
andcommitted
refactor: unify align handling with switch statement
Move nearest logic into switch for consistent code style. Also extract static ALIGN_MAP constant in BodyGrid.tsx. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e2c1aea commit 3230c6b

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/Table.tsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -369,29 +369,34 @@ const Table = <RecordType extends DefaultRecordType>(
369369
const elementTop = (targetElement as HTMLElement).offsetTop;
370370
const elementHeight = (targetElement as HTMLElement).offsetHeight;
371371
const containerHeight = container.clientHeight;
372-
const currentTop = container.scrollTop;
373372
const elementBottom = elementTop + elementHeight;
374-
const viewportBottom = currentTop + containerHeight;
375373
let targetTop: number;
376374

377-
if (align === 'nearest') {
378-
const targetWithOffset = elementTop + offset;
379-
const targetBottomWithOffset = elementBottom + offset;
380-
381-
if (targetWithOffset < currentTop) {
382-
targetTop = targetWithOffset;
383-
} else if (targetBottomWithOffset > viewportBottom) {
384-
targetTop = targetBottomWithOffset - containerHeight;
385-
} else {
386-
targetTop = currentTop;
375+
switch (align) {
376+
case 'nearest': {
377+
const currentTop = container.scrollTop;
378+
const viewportBottom = currentTop + containerHeight;
379+
const targetWithOffset = elementTop + offset;
380+
const targetBottomWithOffset = elementBottom + offset;
381+
382+
if (targetWithOffset < currentTop) {
383+
targetTop = targetWithOffset;
384+
} else if (targetBottomWithOffset > viewportBottom) {
385+
targetTop = targetBottomWithOffset - containerHeight;
386+
} else {
387+
targetTop = currentTop;
388+
}
389+
break;
387390
}
388-
} else {
389-
const alignMap: Record<string, number> = {
390-
start: elementTop,
391-
end: elementBottom - containerHeight,
392-
center: elementTop - (containerHeight - elementHeight) / 2,
393-
};
394-
targetTop = alignMap[align ?? 'start'] + offset;
391+
case 'end':
392+
targetTop = elementBottom - containerHeight + offset;
393+
break;
394+
case 'center':
395+
targetTop = elementTop - (containerHeight - elementHeight) / 2 + offset;
396+
break;
397+
case 'start':
398+
default:
399+
targetTop = elementTop + offset;
395400
}
396401

397402
container.scrollTo({ top: targetTop });

src/VirtualTable/BodyGrid.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import type {
1212
import BodyLine from './BodyLine';
1313
import { GridContext, StaticContext } from './context';
1414

15+
const ALIGN_MAP: Record<string, 'top' | 'bottom' | 'auto'> = {
16+
start: 'top',
17+
end: 'bottom',
18+
nearest: 'auto',
19+
};
20+
1521
export interface GridProps<RecordType = any> {
1622
data: RecordType[];
1723
onScroll: OnCustomizeScroll;
@@ -87,13 +93,7 @@ const Grid = React.forwardRef<GridRef, GridProps>((props, ref) => {
8793
scrollTo: (config: VirtualScrollConfig) => {
8894
const { align, offset, ...restConfig } = config;
8995

90-
const alignMap: Record<string, 'top' | 'bottom' | 'auto'> = {
91-
start: 'top',
92-
end: 'bottom',
93-
nearest: 'auto',
94-
};
95-
96-
const virtualAlign = alignMap[align] ?? (offset ? 'top' : 'auto');
96+
const virtualAlign = ALIGN_MAP[align] ?? (offset ? 'top' : 'auto');
9797

9898
listRef.current?.scrollTo({
9999
...restConfig,

0 commit comments

Comments
 (0)