Skip to content

Commit 2e49d4d

Browse files
author
Hector Arce De Las Heras
committed
Enhance Tooltip Functionality for Mobile Devices
This commit enhances the Tooltip component's functionality on mobile devices. Key changes include: Updated Tooltip test (tooltip.test.tsx) to reflect new behavior where the tooltip does not act as a modal on mobile devices. Modified handleClick method in TooltipUnControlledComponent to handle different scenarios based on the tooltip's state and the event target's location. Adjusted useTooltip function (useTooltip.ts) to update the tooltip position when the window is resized, improving responsiveness and ensuring accurate positioning. Altered the logic for showing and hiding the tooltip in TooltipUnControlledComponent (tooltipUnControlled.tsx). The tooltip is now only shown if it is not already open. These changes improve the user interaction with the tooltip on mobile and tablet devices, and ensure that the tooltip position is accurate even when the window is resized.
1 parent 2abd183 commit 2e49d4d

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

src/components/inputSearch/types/inputSearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export interface IInputSearch<V = undefined extends string ? unknown : string>
211211
error?: boolean;
212212
searchFilterConfig?: SearchFilterConfig;
213213
disableErrorInvalidOption?: boolean;
214-
regex?: string;
214+
regex?: string | RegExp;
215215
onClick?: (event: React.MouseEvent<HTMLInputElement, MouseEvent>) => void;
216216
clearTextInputPopoverIconClick?: boolean;
217217
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;

src/components/screenReaderOnly/screenReaderOnly.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ export const ScreenReaderOnly = ({
1010
dataTestId,
1111
}: IScreenReaderOnly): JSX.Element => {
1212
return (
13-
<>
14-
{children && (
15-
<ScreenReaderOnlyStyled aria-live={ariaLive} data-testid={dataTestId} id={id}>
16-
{children}
17-
</ScreenReaderOnlyStyled>
18-
)}
19-
</>
13+
<ScreenReaderOnlyStyled aria-live={ariaLive} data-testid={dataTestId} id={id}>
14+
{children}
15+
</ScreenReaderOnlyStyled>
2016
);
2117
};

src/components/screenReaderOnly/types/screenReaderOnly.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AriaLiveOptionType } from '@/types/ariaLiveOption';
22

33
export interface IScreenReaderOnly {
4-
children?: string | JSX.Element;
4+
children?: React.ReactNode;
55
id?: string;
66
show?: boolean;
77
ariaLive?: AriaLiveOptionType;

src/components/tooltip/__tests__/tooltip.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe('Tooltip', () => {
323323
it('Tooltip - mobile - it close the tooltip after clicking twice', () => {
324324
window.matchMedia = windowMatchMedia('onlyMobile');
325325
jest.spyOn(mediaHooks, 'useMediaDevice').mockImplementation(() => DeviceBreakpointsType.MOBILE);
326-
renderProvider(<Tooltip {...mockProps} />);
326+
renderProvider(<Tooltip {...mockProps} tooltipAsModal={false} />);
327327
const label = screen.getByText(mockProps.children as string);
328328

329329
fireEvent.click(label);

src/components/tooltip/hooks/useTooltip.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ export const useTooltip = <V>({
4848

4949
// Avoid to show tooltip when scrolling
5050
React.useEffect(() => {
51+
const handleResize = () => {
52+
updateTooltipPosition();
53+
};
54+
5155
window.addEventListener('scroll', onScroll, true);
56+
window.addEventListener('resize', handleResize);
5257
return () => {
5358
window.removeEventListener('scroll', onScroll, true);
59+
window.removeEventListener('resize', handleResize);
5460
};
5561
}, []);
5662

src/components/tooltip/tooltipUnControlled.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,21 @@ const TooltipUnControlledComponent = React.forwardRef(
9595
event.preventDefault();
9696
};
9797

98-
const handleClick: React.MouseEventHandler<HTMLElement> = () => {
98+
const handleClick: React.MouseEventHandler<HTMLElement> = event => {
9999
if (mediaDevice === DeviceBreakpointsType.DESKTOP) {
100100
if (tooltipAsModalValue && !open) {
101101
//this condition is needed because tooltip would close if you clicked on tooltip container beeing tooltip a modal
102102
showTooltip();
103103
}
104104
return;
105105
}
106-
if (open) {
107-
hideTooltip();
108-
} else {
106+
// Mobile / tablet
107+
if (!open) {
109108
showTooltip();
109+
return;
110+
}
111+
if (!tooltipAsModalValue && !tooltipRef.current?.contains(event.target as Node)) {
112+
hideTooltip();
110113
}
111114
};
112115

0 commit comments

Comments
 (0)