From 14ecd3e0f4d61de0ba9a49fae4c2156e0322d83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A0=97=E5=98=89=E7=94=B7?= <574980606@qq.com> Date: Sun, 25 Aug 2024 09:58:28 +0800 Subject: [PATCH] refactor: add validNumberValue --- src/Table.tsx | 10 +++------- src/utils/valueUtil.tsx | 4 ++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Table.tsx b/src/Table.tsx index 46051385a..31edce798 100644 --- a/src/Table.tsx +++ b/src/Table.tsx @@ -74,7 +74,7 @@ import Panel from './Panel'; import StickyScrollBar from './stickyScrollBar'; import Column from './sugar/Column'; import ColumnGroup from './sugar/ColumnGroup'; -import { getColumnsKey, validateValue } from './utils/valueUtil'; +import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil'; import { getDOM } from 'rc-util/lib/Dom/findDOMNode'; export const DEFAULT_PREFIX = 'rc-table'; @@ -331,12 +331,8 @@ function Table( if (scrollBodyRef.current instanceof HTMLElement) { // Native scroll const { index, top, key } = config; - - // * 考虑top为0的情况 - if (top || top === 0) { - scrollBodyRef.current?.scrollTo({ - top, - }); + if (validNumberValue(top)) { + scrollBodyRef.current?.scrollTo({ top }); } else { const mergedKey = key ?? getRowKey(mergedData[index]); scrollBodyRef.current.querySelector(`[data-row-key="${mergedKey}"]`)?.scrollIntoView(); diff --git a/src/utils/valueUtil.tsx b/src/utils/valueUtil.tsx index 47526c31c..56f6fef4f 100644 --- a/src/utils/valueUtil.tsx +++ b/src/utils/valueUtil.tsx @@ -36,3 +36,7 @@ export function getColumnsKey(columns: readonly GetColumnKeyColumn[] export function validateValue(val: T) { return val !== null && val !== undefined; } + +export function validNumberValue(value: any) { + return typeof value === 'number' && !Number.isNaN(value); +}