Skip to content

Commit

Permalink
fix(combobox): memoize floating ui ref to prevent max call depth (#16866
Browse files Browse the repository at this point in the history
)

Co-authored-by: Nikhil Tomar <[email protected]>
  • Loading branch information
tay1orjones and 2nikhiltom committed Jun 26, 2024
1 parent d9b7c3e commit caf4334
Showing 1 changed file with 13 additions and 7 deletions.
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

0 comments on commit caf4334

Please sign in to comment.