Skip to content

Commit

Permalink
Merge pull request primefaces#6958 from primefaces/6957-autocomplete-…
Browse files Browse the repository at this point in the history
…autohighlight-prop-is-not-working-properly

Autocomplete autoHighlight prop is inconsistent
  • Loading branch information
nitrogenous authored Jul 31, 2024
2 parents c5f6f9f + 175ee0e commit 92f8164
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions components/lib/autocomplete/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,16 @@ export const AutoComplete = React.memo(

const onOverlayEntering = () => {
if (props.autoHighlight && props.suggestions && props.suggestions.length) {
const element = getScrollableElement().firstChild.firstChild;
autoHighlightFirstOption();
}
};

if (element) {
!isUnstyled() && DomHandler.addClass(element, 'p-highlight');
element.setAttribute('data-p-highlight', true);
}
const autoHighlightFirstOption = () => {
const element = getScrollableElement()?.firstChild?.firstChild;

if (element) {
!isUnstyled() && DomHandler.addClass(element, 'p-highlight');
element.setAttribute('data-p-highlight', true);
}
};

Expand Down Expand Up @@ -453,7 +457,7 @@ export const AutoComplete = React.memo(
};

const getScrollableElement = () => {
return overlayRef.current.firstChild;
return overlayRef?.current?.firstChild;
};

const getOptionGroupLabel = (optionGroup) => {
Expand All @@ -472,6 +476,12 @@ export const AutoComplete = React.memo(
ObjectUtils.combinedRefs(inputRef, props.inputRef);
}, [inputRef, props.inputRef]);

React.useEffect(() => {
if (searchingState && props.autoHighlight && props.suggestions && props.suggestions.length) {
autoHighlightFirstOption();
}
}, [searchingState]);

useMountEffect(() => {
if (!idState) {
setIdState(UniqueComponentId());
Expand Down

0 comments on commit 92f8164

Please sign in to comment.