From 5e7795fff5af1b683eeca46da5bad247f312253d Mon Sep 17 00:00:00 2001 From: Frida Date: Tue, 21 Oct 2025 11:54:19 +0200 Subject: [PATCH 1/7] =?UTF-8?q?fix(eds-core-react):=20=F0=9F=90=9B=20Updat?= =?UTF-8?q?e=20Tooltip=20component=20to=20use=20mergeRefs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the warning related to accessing element.ref directly, which is no longer supported in React 19. --- .../src/components/Tooltip/Tooltip.tsx | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/eds-core-react/src/components/Tooltip/Tooltip.tsx b/packages/eds-core-react/src/components/Tooltip/Tooltip.tsx index 8436d8b724..1df2544cbb 100644 --- a/packages/eds-core-react/src/components/Tooltip/Tooltip.tsx +++ b/packages/eds-core-react/src/components/Tooltip/Tooltip.tsx @@ -8,6 +8,7 @@ import { useMemo, useEffect, ReactNode, + ReactElement, } from 'react' import { createPortal } from 'react-dom' import styled from 'styled-components' @@ -136,9 +137,9 @@ export const Tooltip = forwardRef( ], whileElementsMounted: autoUpdate, }) - const anchorRef = useMemo( - () => mergeRefs(refs.setReference, children?.ref), - [refs.setReference, children?.ref], + const mergedAnchorRef = useMemo( + () => mergeRefs(refs.setReference), + [refs.setReference], ) const tooltipRef = useMemo( () => mergeRefs(refs.setFloating, ref), @@ -188,11 +189,12 @@ export const Tooltip = forwardRef( } }) - const updatedChildren = cloneElement(children, { - ...getReferenceProps({ - ...children.props, - ref: anchorRef, - }), + const updatedChildren = cloneElement(children as ReactElement, { + ...getReferenceProps(children.props), + ref: mergeRefs( + (children as ReactElement).props.ref, + mergedAnchorRef, + ), }) useEffect(() => { @@ -228,6 +230,7 @@ export const Tooltip = forwardRef( return ( <> + {updatedChildren} {shouldOpen && open && !disabled && @@ -235,7 +238,6 @@ export const Tooltip = forwardRef( TooltipEl, portalContainer ?? rootElement ?? document.body, )} - {updatedChildren} ) }, From ead6ee8ddb2473fa7b0fc49c1fb4dce7170a33d3 Mon Sep 17 00:00:00 2001 From: Frida Date: Tue, 21 Oct 2025 15:27:35 +0200 Subject: [PATCH 2/7] Add test to cover ref issue --- .../src/components/Tooltip/Tooltip.test.tsx | 33 +++++++++++++++++-- .../__snapshots__/Tooltip.test.tsx.snap | 2 +- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/eds-core-react/src/components/Tooltip/Tooltip.test.tsx b/packages/eds-core-react/src/components/Tooltip/Tooltip.test.tsx index f669c6a0c0..a4108dd403 100644 --- a/packages/eds-core-react/src/components/Tooltip/Tooltip.test.tsx +++ b/packages/eds-core-react/src/components/Tooltip/Tooltip.test.tsx @@ -1,8 +1,10 @@ /* eslint-disable no-undef */ -import { fireEvent, render, screen, act } from '@testing-library/react' +import { fireEvent, render, screen, act, cleanup } from '@testing-library/react' import { axe } from 'jest-axe' +import '@testing-library/jest-dom' import styled from 'styled-components' import { Tooltip, Button } from '../../' +import { createRef } from 'react' const StyledTooltip = styled(Tooltip)` background: red; @@ -20,6 +22,9 @@ beforeAll(() => { }) describe('Tooltip', () => { + afterEach(() => { + cleanup() + }) it('Matches snapshot', async () => { render( @@ -76,7 +81,7 @@ describe('Tooltip', () => { await act(() => new Promise((r) => setTimeout(r, openDelay))) const tooltip = await screen.findByRole('tooltip') - expect(tooltip).toHaveStyleRule('background', 'red') + expect(tooltip).toHaveStyle('background: red') }) it('is visible when content is being hovered', async () => { render( @@ -174,4 +179,28 @@ describe('Tooltip', () => { expect(handler).toBeCalled() }) + it('should render correctly when the wrapped component has a ref', async () => { + const buttonRef = createRef() + render( + + + , + ) + + const button = screen.getByText('Test') + + fireEvent.focus(button) + + expect(await screen.findByRole('tooltip')).toBeInTheDocument() + }) + it('should forward refs to the wrapped component', async () => { + const ref = createRef() + render( + + + , + ) + + expect(ref.current).toBeInstanceOf(HTMLButtonElement) + }) }) diff --git a/packages/eds-core-react/src/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap b/packages/eds-core-react/src/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap index bc10bc9bcc..52ce3e6dcb 100644 --- a/packages/eds-core-react/src/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap +++ b/packages/eds-core-react/src/components/Tooltip/__snapshots__/Tooltip.test.tsx.snap @@ -53,7 +53,7 @@ exports[`Tooltip Matches snapshot 1`] = `