From d47e571074af3aec769f0b11c79bb107019862e4 Mon Sep 17 00:00:00 2001 From: Jordan Rowan Date: Wed, 12 Jun 2024 11:19:44 +0100 Subject: [PATCH] fix(tooltip-popover-combo): fix aria labelledby and id issue --- .../Toggletip/Toggletip.unit.test.tsx.snap | 8 ++- .../TooltipPopoverCombo.stories.tsx | 1 + .../TooltipPopoverCombo.tsx | 24 ++++--- .../TooltipPopoverCombo.types.ts | 4 +- .../TooltipPopoverCombo.unit.test.tsx | 6 +- .../TooltipPopoverCombo.unit.test.tsx.snap | 69 +++++++++++++------ 6 files changed, 75 insertions(+), 37 deletions(-) diff --git a/src/components/Toggletip/Toggletip.unit.test.tsx.snap b/src/components/Toggletip/Toggletip.unit.test.tsx.snap index 9f59a91a59..cc5b444a17 100644 --- a/src/components/Toggletip/Toggletip.unit.test.tsx.snap +++ b/src/components/Toggletip/Toggletip.unit.test.tsx.snap @@ -719,7 +719,9 @@ exports[` snapshot should match snapshot with color 2`] = ` exports[` snapshot should match snapshot with id 1`] = `
-
snapshot should match snapshot with id 1`] = ` exports[` snapshot should match snapshot with id 2`] = `
-
= () => { margin: '10rem auto', display: 'flex' }} + id="storybook-id-example" > ); diff --git a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.tsx b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.tsx index 323e280746..20c109422d 100644 --- a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.tsx +++ b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.tsx @@ -1,10 +1,11 @@ -import React, { FC, useCallback, useEffect, useState } from 'react'; +import React, { FC, useCallback, useEffect, useRef, useState } from 'react'; import { Props } from './TooltipPopoverCombo.types'; import Popover from '../Popover'; import { TooltipPopoverComboProps } from '.'; import Tooltip from '../Tooltip/Tooltip'; import { PopoverInstance } from '../Popover/Popover.types'; +import { v4 as uuidV4 } from 'uuid'; /** * The TooltipPopoverCombo component. @@ -21,6 +22,10 @@ const TooltipPopoverCombo: FC = (props: TooltipPopoverComboProps) => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [isTooltipOpen, setIsTooltipOpen] = useState(false); + const triggerComponentId = useRef(triggerComponent.props?.id || uuidV4()); + + const clonedTriggerComponent = React.cloneElement(triggerComponent, {id: triggerComponentId.current}); + // Modified tooltipSetInstance to call both setInstance and updateInstance const setMergedTooltipInstances = useCallback( (popoverInstance: PopoverInstance | undefined) => { @@ -64,14 +69,15 @@ const TooltipPopoverCombo: FC = (props: TooltipPopoverComboProps) => { interactive triggerComponent={ - {tooltipContent} - + triggerComponent={clonedTriggerComponent} + {...otherTooltipProps} + onHide={handleOnHideTooltip} + onShow={handleOnShowTooltip} + setInstance={setMergedTooltipInstances} + id={triggerComponentId.current} + > + {tooltipContent} + } {...otherPopoverProps} onHide={handleOnHidePopover} diff --git a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.types.ts b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.types.ts index 8f5f8a9608..fa127b0d99 100644 --- a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.types.ts +++ b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.types.ts @@ -21,10 +21,10 @@ export interface Props { /** * An object of Popover props to be passed through to the Popover component. */ - otherPopoverProps?: Partial; + otherPopoverProps?: Partial>; /** * An object of Tooltip props to be passed through to the Tooltip component. */ - otherTooltipProps?: Partial; + otherTooltipProps?: Partial>; } diff --git a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx index 02b991f81a..b092424995 100644 --- a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx +++ b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx @@ -19,7 +19,7 @@ jest.mock('uuid', () => { }); describe('', () => { - const triggerComponent = ; + const triggerComponent = ; const tooltipContent = Example tooltip content; const popoverContent = ; @@ -76,7 +76,7 @@ describe('', () => { setInstance: expect.any(Function), triggerComponent: triggerComponent, 'aria-haspopup': 'dialog', - 'id': '1', + 'id': 'test-id', }); }); @@ -114,7 +114,7 @@ describe('', () => { setInstance: expect.any(Function), triggerComponent: triggerComponent, 'aria-haspopup': 'dialog', - 'id': '1', + 'id': 'test-id', 'placement': 'top', }); }); diff --git a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx.snap b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx.snap index dfa7f3ef0a..775a72aa0c 100644 --- a/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx.snap +++ b/src/components/TooltipPopoverCombo/TooltipPopoverCombo.unit.test.tsx.snap @@ -13,7 +13,9 @@ exports[` snapshot should match snapshot 1`] = ` } triggerComponent={ - } @@ -25,11 +27,14 @@ exports[` snapshot should match snapshot 1`] = ` trigger="click" triggerComponent={ + } @@ -217,12 +222,14 @@ exports[` snapshot should match snapshot 1`] = ` > + } @@ -232,7 +239,7 @@ exports[` snapshot should match snapshot 1`] = ` aria-haspopup="dialog" boundary="scrollParent" color="primary" - id="1" + id="test-id" interactive={false} offsetDistance={5} offsetSkidding={0} @@ -245,7 +252,9 @@ exports[` snapshot should match snapshot 1`] = ` strategy="absolute" trigger="mouseenter focus" triggerComponent={ - } @@ -415,7 +424,7 @@ exports[` snapshot should match snapshot 1`] = ` trigger="mouseenter focus" > @@ -471,7 +480,9 @@ exports[` snapshot should match snapshot with otherPopove } triggerComponent={ - } @@ -483,12 +494,15 @@ exports[` snapshot should match snapshot with otherPopove trigger="click" triggerComponent={ + } @@ -676,13 +690,15 @@ exports[` snapshot should match snapshot with otherPopove > + } @@ -692,7 +708,7 @@ exports[` snapshot should match snapshot with otherPopove aria-haspopup="dialog" boundary="scrollParent" color="primary" - id="1" + id="test-id" interactive={false} offsetDistance={5} offsetSkidding={0} @@ -705,7 +721,9 @@ exports[` snapshot should match snapshot with otherPopove strategy="absolute" trigger="mouseenter focus" triggerComponent={ - } @@ -875,7 +893,7 @@ exports[` snapshot should match snapshot with otherPopove trigger="mouseenter focus" > @@ -931,7 +949,9 @@ exports[` snapshot should match snapshot with otherToolti } triggerComponent={ - } @@ -943,12 +963,15 @@ exports[` snapshot should match snapshot with otherToolti trigger="click" triggerComponent={ + } @@ -1136,13 +1159,15 @@ exports[` snapshot should match snapshot with otherToolti > + } @@ -1152,7 +1177,7 @@ exports[` snapshot should match snapshot with otherToolti aria-haspopup="dialog" boundary="scrollParent" color="primary" - id="1" + id="test-id" interactive={false} offsetDistance={5} offsetSkidding={0} @@ -1165,7 +1190,9 @@ exports[` snapshot should match snapshot with otherToolti strategy="absolute" trigger="mouseenter focus" triggerComponent={ - } @@ -1335,7 +1362,7 @@ exports[` snapshot should match snapshot with otherToolti trigger="mouseenter focus" >