From 3816be012069655d755b3b0320e25161fda061d1 Mon Sep 17 00:00:00 2001 From: melloware Date: Sun, 7 Apr 2024 08:03:54 -0400 Subject: [PATCH] Fix #6305: Multiselect proper scrollIntoView handling --- components/lib/multiselect/MultiSelect.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/components/lib/multiselect/MultiSelect.js b/components/lib/multiselect/MultiSelect.js index b0290a6882..5a544535ab 100644 --- a/components/lib/multiselect/MultiSelect.js +++ b/components/lib/multiselect/MultiSelect.js @@ -465,11 +465,20 @@ export const MultiSelect = React.memo( props.onFilter && props.onFilter({ filter: '' }); }; - const scrollInView = () => { - const highlightItem = DomHandler.findSingle(overlayRef.current, 'li[data-p-highlight="true"]'); + const scrollInView = (event) => { + if (!overlayVisibleState) { + return; + } + + let focusedItem; + if (event) { + focusedItem = event.currentTarget; + } else { + focusedItem = DomHandler.findSingle(overlayRef.current, 'li[data-p-highlight="true"]'); + } - if (highlightItem && highlightItem.scrollIntoView) { - highlightItem.scrollIntoView({ block: 'nearest', inline: 'nearest' }); + if (focusedItem && focusedItem.scrollIntoView) { + focusedItem.scrollIntoView({ block: 'nearest', inline: 'nearest' }); } }; @@ -792,7 +801,7 @@ export const MultiSelect = React.memo( const changeFocusedOptionIndex = (event, index) => { if (focusedOptionIndex !== index) { setFocusedOptionIndex(index); - scrollInView(); + scrollInView(event); if (props.selectOnFocus) { onOptionSelect(event, visibleOptions[index], false);