-
-
Notifications
You must be signed in to change notification settings - Fork 652
Truncated text component #1117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arkid15r
merged 37 commits into
OWASP:main
from
yashgoyal0110:feat/TruncatedText-component
Mar 24, 2025
Merged
Truncated text component #1117
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
0ebcede
commit
yashgoyal0110 ca4de6e
Merge branch 'OWASP:main' into main
yashgoyal0110 95c2051
Update pnpm-lock.yaml
yashgoyal0110 98e8cd4
Update vite.config.ts
yashgoyal0110 d126560
Update package.json
yashgoyal0110 7117a69
Merge branch 'OWASP:main' into main
yashgoyal0110 3ee3a64
Merge branch 'OWASP:main' into main
yashgoyal0110 c0dd7d4
Merge branch 'OWASP:main' into main
yashgoyal0110 1fe7197
Merge branch 'OWASP:main' into main
yashgoyal0110 1916577
Merge branch 'OWASP:main' into main
yashgoyal0110 269ff95
truncate text component
yashgoyal0110 2aeffce
commit
yashgoyal0110 563902a
Merge branch 'main' into feat/TruncatedText-component
kasya e4ad50d
Merge branch 'main' into feat/TruncatedText-component
yashgoyal0110 8db1ffa
commit
yashgoyal0110 8041c12
commit
yashgoyal0110 ccb61a2
commit
yashgoyal0110 170f5b4
commit
yashgoyal0110 8cebc03
commit
yashgoyal0110 698c9a8
commit
yashgoyal0110 19ca19c
commit
yashgoyal0110 b1d9fad
commit
yashgoyal0110 4d36fd9
commit
yashgoyal0110 c56666b
Merge branch 'OWASP:main' into feat/TruncatedText-component
yashgoyal0110 7fafc90
fixed e2e and improved truncate component
yashgoyal0110 1a57db6
fixed pre-commit
yashgoyal0110 cbae4b9
Merge branch 'main' into feat/TruncatedText-component
arkid15r e5c1427
Merge branch 'main' into feat/TruncatedText-component
arkid15r 0e409b8
comit
yashgoyal0110 ae8344c
comit
yashgoyal0110 38808c6
Merge branch 'OWASP:main' into feat/TruncatedText-component
yashgoyal0110 79bb085
commit
yashgoyal0110 e431133
Merge branch 'main' into feat/TruncatedText-component
arkid15r e8ebf83
fixed unit tests
yashgoyal0110 464d980
fixed unit test
yashgoyal0110 39bf7bb
commit
yashgoyal0110 da5da20
Update code
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,30 @@ | ||
| import { render, screen } from 'wrappers/testUtil' | ||
| import { TruncatedText } from 'components/TruncatedText' | ||
|
|
||
| describe('TruncatedText Component', () => { | ||
| const longText = 'This is very long text that should be truncated for display.' | ||
|
|
||
| test('renders full text when it fits within the container', () => { | ||
| render(<TruncatedText text="Short text" className="w-auto" />) | ||
| const textElement = screen.getByText('Short text') | ||
| expect(textElement).toBeInTheDocument() | ||
| expect(textElement).toHaveAttribute('title', 'Short text') | ||
| }) | ||
|
|
||
| test('truncates text when it exceeds container width', () => { | ||
| render( | ||
| <div style={{ width: '100px' }}> | ||
| <TruncatedText text={longText} /> | ||
| </div> | ||
| ) | ||
| const textElement = screen.getByText(longText) | ||
| expect(textElement).toHaveClass('truncate') | ||
| expect(textElement).toHaveClass('text-ellipsis') | ||
| }) | ||
|
|
||
| test('title attribute is always present', () => { | ||
| render(<TruncatedText text={longText} />) | ||
| const textElement = screen.getByText(longText) | ||
| expect(textElement).toHaveAttribute('title', longText) | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| import { useRef, useEffect, useCallback } from 'react' | ||
|
|
||
| export const TruncatedText = ({ text, className = '' }: { text: string; className?: string }) => { | ||
| const textRef = useRef<HTMLSpanElement>(null) | ||
|
|
||
| const checkTruncation = useCallback(() => { | ||
| const element = textRef.current | ||
| if (element) { | ||
| element.title = text | ||
| } | ||
| }, [text]) | ||
|
|
||
| useEffect(() => { | ||
| checkTruncation() | ||
|
|
||
| const observer = new ResizeObserver(() => checkTruncation()) | ||
| if (textRef.current) { | ||
| observer.observe(textRef.current) | ||
| } | ||
|
|
||
| window.addEventListener('resize', checkTruncation) | ||
|
|
||
| return () => { | ||
| observer.disconnect() | ||
| window.removeEventListener('resize', checkTruncation) | ||
| } | ||
| }, [text, checkTruncation]) | ||
|
|
||
| return ( | ||
| <span | ||
| ref={textRef} | ||
| className={`block overflow-hidden truncate text-ellipsis whitespace-nowrap ${className}`} | ||
| > | ||
| {text} | ||
| </span> | ||
| ) | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue description contains
Apply the component for all existing cases to make sure the universal lookand it seems the current code contains home page only improvements.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all existing cases also includes project, issue and chapter card titles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We want a consistent look, not just a main page fix.