Skip to content

Commit

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

Signed-off-by: Edward Rojas <edward.rojas@fr.ibm.com>
Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
  • Loading branch information
EAlexRojas and tay1orjones committed Jul 22, 2024
1 parent 133a9e9 commit 242ffdd
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React, {
MouseEvent,
ReactNode,
useEffect,
useMemo,
} from 'react';
import {
useSelect,
Expand Down Expand Up @@ -497,8 +498,13 @@ const Dropdown = React.forwardRef(
},
};

const menuProps = getMenuProps();
const menuRef = mergeRefs(menuProps.ref, refs.setFloating);
const menuProps = useMemo(
() =>
getMenuProps({
ref: autoAlign ? refs.setFloating : null,
}),
[autoAlign]
);

// Slug is always size `mini`
let normalizedSlug;
Expand Down Expand Up @@ -527,7 +533,7 @@ const Dropdown = React.forwardRef(
warnText={warnText}
light={light}
isOpen={isOpen}
ref={refs.setReference}
ref={autoAlign ? refs.setReference : null}
id={id}>
{invalid && (
<WarningFilled className={`${prefix}--list-box__invalid-icon`} />
Expand Down Expand Up @@ -567,7 +573,7 @@ const Dropdown = React.forwardRef(
/>
</button>
{normalizedSlug}
<ListBox.Menu {...menuProps} ref={menuRef}>
<ListBox.Menu {...menuProps}>
{isOpen &&
items.map((item, index) => {
const isObject = item !== null && typeof item === 'object';
Expand Down

0 comments on commit 242ffdd

Please sign in to comment.