diff --git a/src/OptionList/index.tsx b/src/OptionList/index.tsx index 45d8a9d7..b55ee783 100644 --- a/src/OptionList/index.tsx +++ b/src/OptionList/index.tsx @@ -164,7 +164,7 @@ const RefOptionList = React.forwardRef((props, ref) => { `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 8a355425..bf71d374 100644 --- a/src/utils/commonUtil.ts +++ b/src/utils/commonUtil.ts @@ -43,7 +43,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; @@ -54,6 +54,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 parentToContainer = parent.parentElement.children; + if(!activeValueCells.length || activeValueCells.length === parentToContainer.length) return + const container = parentToContainer[activeValueCells.length]; + if (container && typeof container.scrollTo === 'function') { + container.scrollTo({top: 0}); + } } }