-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
149 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
ui/src/components/table/table-element/tooltip-on-truncate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use client'; | ||
import React, { useEffect, useState, useRef } from 'react'; | ||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; | ||
|
||
const TooltipOnTruncate = ({ children, value }: { children: React.ReactNode; value: string }) => { | ||
const [isTruncated, setIsTruncated] = useState(false); | ||
const ref = useRef<HTMLButtonElement>(null); | ||
|
||
useEffect(() => { | ||
const checkIsTruncated = () => { | ||
if (ref.current) { | ||
setIsTruncated(ref.current.scrollWidth > ref.current.clientWidth); | ||
} | ||
}; | ||
checkIsTruncated(); | ||
window.addEventListener('resize', checkIsTruncated); | ||
return () => window.removeEventListener('resize', checkIsTruncated); | ||
}, []); | ||
|
||
return ( | ||
<TooltipProvider> | ||
<Tooltip> | ||
<TooltipTrigger asChild ref={ref}> | ||
{children} | ||
</TooltipTrigger> | ||
{isTruncated && ( | ||
<TooltipContent className="max-w-[190px] max-h-[180px] text-center whitespace-normal break-words bg-black text-white"> | ||
<p data-testid="tooltip-on-truncate">{value}</p> | ||
</TooltipContent> | ||
)} | ||
</Tooltip> | ||
</TooltipProvider> | ||
); | ||
}; | ||
|
||
export default TooltipOnTruncate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
ui/tests/components/table/table-element/tooltip-on-truncate.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import TooltipOnTruncate from '@/components/table/table-element/tooltip-on-truncate'; | ||
|
||
describe('TooltipOnTruncate component', () => { | ||
it('should show tooltip if content is truncated', async () => { | ||
Object.defineProperties(HTMLElement.prototype, { | ||
scrollWidth: { get: () => 500, configurable: true }, | ||
clientWidth: { get: () => 30, configurable: true } | ||
}); | ||
render( | ||
<TooltipOnTruncate value="Truncated Text"> | ||
<button>Truncated Text</button> | ||
</TooltipOnTruncate> | ||
); | ||
const button = screen.getByRole('button'); | ||
await waitFor(async () => { | ||
await userEvent.hover(button); | ||
}); | ||
|
||
// Wait for the tooltip to appear | ||
await waitFor(() => { | ||
expect(screen.getAllByTestId('tooltip-on-truncate')[0]).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should not show tooltip if content is not truncated', async () => { | ||
Object.defineProperties(HTMLElement.prototype, { | ||
scrollWidth: { get: () => 30, configurable: true }, | ||
clientWidth: { get: () => 500, configurable: true } | ||
}); | ||
render( | ||
<TooltipOnTruncate value="Truncated Text"> | ||
<button>Truncated Text</button> | ||
</TooltipOnTruncate> | ||
); | ||
const button = screen.getByRole('button'); | ||
|
||
await waitFor(async () => { | ||
await userEvent.hover(button); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByTestId('tooltip-on-truncate')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |