Skip to content

Commit 4e75061

Browse files
authored
fix: adds backward compatibility for innerRefs (#19086)
1 parent 549ee46 commit 4e75061

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/react/src/components/OverflowMenu/OverflowMenu-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,29 @@ describe('OverflowMenu', () => {
413413
expect(button).not.toHaveClass('cds--overflow-menu--open');
414414
expect(button).toHaveFocus();
415415
});
416+
describe('Ref handling', () => {
417+
it('should support both standard ref and innerRef', () => {
418+
const standardRef = React.createRef();
419+
const innerRef = React.createRef();
420+
421+
render(
422+
<OverflowMenu
423+
ref={standardRef}
424+
innerRef={innerRef}
425+
aria-label="Overflow menu"
426+
data-testid="overflow-menu">
427+
<OverflowMenuItem itemText="Option 1" />
428+
<OverflowMenuItem itemText="Option 2" />
429+
</OverflowMenu>
430+
);
431+
const buttonElement = screen.getByRole('button');
432+
expect(standardRef.current).not.toBeNull();
433+
expect(innerRef.current).not.toBeNull();
434+
expect(standardRef.current).toBe(buttonElement);
435+
expect(innerRef.current).toBe(buttonElement);
436+
437+
// Verify both refs point to the same element & not null
438+
expect(standardRef.current).toBe(innerRef.current);
439+
});
440+
});
416441
});

packages/react/src/components/OverflowMenu/OverflowMenu.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ export interface OverflowMenuProps {
229229
size?: 'sm' | 'md' | 'lg';
230230

231231
/**
232-
* The ref to the HTML element that should receive focus when the OverflowMenu opens
232+
* The ref to the overflow menu's trigger button element.
233+
* @deprecated Use the standard React `ref` prop instead.
233234
*/
234235
innerRef?: Ref<any>;
235236
}
@@ -258,6 +259,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
258259
renderIcon: IconElement = OverflowMenuVertical,
259260
selectorPrimaryFocus = '[data-floating-menu-primary-focus]',
260261
size = 'md',
262+
innerRef,
261263
...other
262264
},
263265
ref
@@ -543,6 +545,9 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
543545
})}
544546
</FloatingMenu>
545547
);
548+
const combinedRef = innerRef
549+
? mergeRefs(triggerRef, innerRef, ref)
550+
: mergeRefs(triggerRef, ref);
546551

547552
return (
548553
<>
@@ -559,7 +564,7 @@ export const OverflowMenu = forwardRef<HTMLButtonElement, OverflowMenuProps>(
559564
className={overflowMenuClasses}
560565
onClick={handleClick}
561566
id={id}
562-
ref={mergeRefs(triggerRef, ref)}
567+
ref={combinedRef}
563568
size={size}
564569
label={iconDescription}
565570
kind="ghost">

0 commit comments

Comments
 (0)