Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(combobox): memoize floating ui ref to prevent max call depth #16866

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions packages/react/src/components/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import React, {
useEffect,
useState,
useRef,
useMemo,
forwardRef,
type ReactNode,
type ComponentType,
Expand Down Expand Up @@ -655,6 +656,15 @@ const ComboBox = forwardRef(
(helperText && !isFluid && helperTextId) ||
undefined;

const menuProps = useMemo(
() =>
getMenuProps({
'aria-label': deprecatedAriaLabel || ariaLabel,
ref: autoAlign ? refs.setFloating : null,
}),
[autoAlign, deprecatedAriaLabel, ariaLabel]
);

return (
<div className={wrapperClasses}>
{titleText && (
Expand All @@ -674,7 +684,7 @@ const ComboBox = forwardRef(
light={light}
size={size}
warn={warn}
ref={refs.setReference}
ref={autoAlign ? refs.setReference : null}
warnText={warnText}
warnTextId={warnTextId}>
<div className={`${prefix}--list-box__field`}>
Expand All @@ -686,7 +696,7 @@ const ComboBox = forwardRef(
aria-haspopup="listbox"
title={textInput?.current?.value}
{...getInputProps({
'aria-controls': isOpen ? undefined : getMenuProps().id,
'aria-controls': isOpen ? undefined : menuProps.id,
placeholder,
ref: { ...mergeRefs(textInput, ref) },
onKeyDown: (
Expand Down Expand Up @@ -785,11 +795,7 @@ const ComboBox = forwardRef(
/>
</div>
{normalizedSlug}
<ListBox.Menu
{...getMenuProps({
'aria-label': deprecatedAriaLabel || ariaLabel,
})}
ref={mergeRefs(getMenuProps().ref, refs.setFloating)}>
<ListBox.Menu {...menuProps}>
{isOpen
? filterItems(items, itemToString, inputValue).map(
(item, index) => {
Expand Down
Loading