From fc2d1e1a0ebda4776bbd0f9f66e75c7ebf5ed07c Mon Sep 17 00:00:00 2001 From: lhy Date: Sat, 28 Oct 2023 11:52:21 +0800 Subject: [PATCH] fix default no go top --- src/OptionList/List.tsx | 2 +- src/utils/commonUtil.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/OptionList/List.tsx b/src/OptionList/List.tsx index 9b0ce3a4..c118373b 100644 --- a/src/OptionList/List.tsx +++ b/src/OptionList/List.tsx @@ -173,7 +173,7 @@ const RawOptionList = React.forwardRef(( `li[data-path-key="${cellKeyPath.replace(/\\{0,2}"/g, '\\"')}"]`, // matches unescaped double quotes ); if (ele) { - scrollIntoParentView(ele); + scrollIntoParentView(ele, activeValueCells); } } }, [activeValueCells]); diff --git a/src/utils/commonUtil.ts b/src/utils/commonUtil.ts index c54eb4c0..c96365e1 100644 --- a/src/utils/commonUtil.ts +++ b/src/utils/commonUtil.ts @@ -44,7 +44,7 @@ export function isLeaf(option: DefaultOptionType, fieldNames: FieldNames) { return option.isLeaf ?? !option[fieldNames.children]?.length; } -export function scrollIntoParentView(element: HTMLElement) { +export function scrollIntoParentView(element: HTMLElement, activeValueCells: React.Key[]) { const parent = element.parentElement; if (!parent) { return; @@ -55,6 +55,13 @@ export function scrollIntoParentView(element: HTMLElement) { parent.scrollTo({ top: elementToParent }); } else if (elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight) { parent.scrollTo({ top: elementToParent + element.offsetHeight - parent.offsetHeight }); + } else { + const parentContainerSiblings = parent.parentElement.children; + if (!activeValueCells.length || activeValueCells.length === parentContainerSiblings.length) return + const container = parentContainerSiblings[activeValueCells.length]; + if (container && typeof container.scrollTo === 'function') { + container.scrollTo({ top: 0 }); + } } }