Skip to content

Commit

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

Signed-off-by: Edward Rojas <edward.rojas@fr.ibm.com>
Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
  • Loading branch information
2 people authored and 2nikhiltom committed Jul 22, 2024
1 parent cbf7965 commit 39d3b0b
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 39d3b0b

Please sign in to comment.