From fa054209b10f7f9f3712d16b26914da30a9c744d Mon Sep 17 00:00:00 2001 From: clean99 <53188310+clean99@users.noreply.github.com> Date: Tue, 24 Nov 2026 07:44:05 +0800 Subject: [PATCH] fix: start upward navigation at the last option when none is active --- src/OptionList.tsx | 6 +++++- src/utils/keyUtil.ts | 4 ++++ tests/keyUtil.test.ts | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/keyUtil.test.ts diff --git a/src/OptionList.tsx b/src/OptionList.tsx index 87b9094af..8740a4e9a 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -8,6 +8,7 @@ import SelectContext from './SelectContext'; import TransBtn from './TransBtn'; import useBaseProps from './hooks/useBaseProps'; import type { FlattenOptionData } from './interface'; +import { getNextActiveIndex } from './utils/keyUtil'; import { isPlatformMac } from './utils/platformUtil'; import { isValidCount } from './utils/valueUtil'; @@ -211,7 +212,10 @@ const OptionList: React.ForwardRefRenderFunction = (_, r } if (offset !== 0) { - const nextActiveIndex = getEnabledActiveIndex(activeIndex + offset, offset); + const nextActiveIndex = getEnabledActiveIndex( + getNextActiveIndex(activeIndex, offset), + offset, + ); scrollIntoView(nextActiveIndex); setActive(nextActiveIndex, true); } diff --git a/src/utils/keyUtil.ts b/src/utils/keyUtil.ts index e63253744..ffa8e16c4 100644 --- a/src/utils/keyUtil.ts +++ b/src/utils/keyUtil.ts @@ -1,5 +1,9 @@ import { KeyCode } from '@rc-component/util'; +export function getNextActiveIndex(activeIndex: number, offset: number): number { + return activeIndex === -1 && offset < 0 ? -1 : activeIndex + offset; +} + /** keyCode Judgment function */ export function isValidateOpenKey(currentKeyCode: number): boolean { return ( diff --git a/tests/keyUtil.test.ts b/tests/keyUtil.test.ts new file mode 100644 index 000000000..5bdfa77c8 --- /dev/null +++ b/tests/keyUtil.test.ts @@ -0,0 +1,16 @@ +import { getNextActiveIndex } from '../src/utils/keyUtil'; + +describe('getNextActiveIndex', () => { + it.each([ + [-1, -1, -1], + [-1, 1, 0], + [0, -1, -1], + [0, 1, 1], + [1, -1, 0], + [1, 1, 2], + [2, -1, 1], + [2, 1, 3], + ])('starts scanning from %i with offset %i at %i', (activeIndex, offset, expected) => { + expect(getNextActiveIndex(activeIndex, offset)).toBe(expected); + }); +});