Skip to content

Commit

Permalink
Fix primefaces#6123: Dropdown scroll into view on focus
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Apr 23, 2024
1 parent 6ef3a90 commit a46c533
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions components/lib/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,16 @@ export const Dropdown = React.memo(
};

const scrollInView = () => {
const highlightItem = DomHandler.findSingle(overlayRef.current, 'li[data-p-highlight="true"]');
const focusedItem = DomHandler.findSingle(overlayRef.current, 'li[data-p-focused="true"]');

if (highlightItem && highlightItem.scrollIntoView) {
highlightItem.scrollIntoView({ block: 'nearest', inline: 'nearest' });
if (focusedItem && focusedItem.scrollIntoView) {
focusedItem.scrollIntoView({ block: 'nearest', inline: 'nearest' });
} else {
const highlightItem = DomHandler.findSingle(overlayRef.current, 'li[data-p-highlight="true"]');

if (highlightItem && highlightItem.scrollIntoView) {
highlightItem.scrollIntoView({ block: 'nearest', inline: 'nearest' });
}
}
};

Expand Down Expand Up @@ -945,10 +951,10 @@ export const Dropdown = React.memo(
});

useUpdateEffect(() => {
if (overlayVisibleState && props.value) {
if (overlayVisibleState && (props.value || focusedOptionIndex >= 0)) {
scrollInView();
}
}, [overlayVisibleState, props.value]);
}, [overlayVisibleState, props.value, focusedOptionIndex]);

useUpdateEffect(() => {
if (overlayVisibleState && filterState && props.filter) {
Expand Down

0 comments on commit a46c533

Please sign in to comment.