-
Notifications
You must be signed in to change notification settings - Fork 380
feat(Truncate): added Truncate component #6713
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
jpuzz0
merged 7 commits into
patternfly:main
from
gabipodolnikova:new-component-truncate
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7d9b92b
feat(Truncate): Added new component
gabipodolnikova c2847a4
Added Tooltip
gabipodolnikova d407dff
fixes condition
gabipodolnikova db077e0
Small review fix
gabipodolnikova ce2353e
Fixes and updates from core
gabipodolnikova a7ae3be
Fixes tests
gabipodolnikova 2dbca5b
Review fixes number I don't even know
gabipodolnikova 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import * as React from 'react'; | ||
| import styles from '@patternfly/react-styles/css/components/Truncate/truncate'; | ||
| import { css } from '@patternfly/react-styles'; | ||
| import { Tooltip } from '../Tooltip'; | ||
|
|
||
| export enum TruncatePosition { | ||
| start = 'start', | ||
| end = 'end', | ||
| middle = 'middle' | ||
| } | ||
|
|
||
| const truncateStyles = { | ||
| start: styles.truncateEnd, | ||
| end: styles.truncateStart | ||
| }; | ||
|
|
||
| const minWidthCharacters: number = 12; | ||
|
|
||
| interface TruncateProps extends React.HTMLProps<HTMLSpanElement> { | ||
| /** Class to add to outer span */ | ||
| className?: string; | ||
| /** Text to truncate */ | ||
| content: string; | ||
| /** The number of characters displayed in the second half of the truncation */ | ||
| trailingNumChars?: number; | ||
| /** Where the text will be truncated */ | ||
| position?: 'start' | 'middle' | 'end'; | ||
| /** Tooltip position */ | ||
| tooltipPosition?: 'auto' | 'top' | 'bottom' | 'left' | 'right'; | ||
| } | ||
|
|
||
| const sliceContent = (str: string, slice: number) => [str.slice(0, str.length - slice), str.slice(-slice)]; | ||
|
|
||
| export const Truncate: React.FunctionComponent<TruncateProps> = ({ | ||
| className, | ||
| position = 'end', | ||
| tooltipPosition = 'top', | ||
| trailingNumChars = 7, | ||
| content, | ||
| ...props | ||
| }: TruncateProps) => ( | ||
| <Tooltip position={tooltipPosition} content={content}> | ||
| <span className={css(styles.truncate, className)} {...props}> | ||
| {(position === TruncatePosition.end || position === TruncatePosition.start) && ( | ||
| <span className={truncateStyles[position]}> | ||
| {content} | ||
| {position === TruncatePosition.start && <React.Fragment>‎</React.Fragment>} | ||
| </span> | ||
| )} | ||
| {position === TruncatePosition.middle && | ||
| content.slice(0, content.length - trailingNumChars).length > minWidthCharacters && ( | ||
| <React.Fragment> | ||
| <span className={styles.truncateStart}>{sliceContent(content, trailingNumChars)[0]}</span> | ||
| <span className={styles.truncateEnd}>{sliceContent(content, trailingNumChars)[1]}</span> | ||
| </React.Fragment> | ||
| )} | ||
| {position === TruncatePosition.middle && | ||
| content.slice(0, content.length - trailingNumChars).length <= minWidthCharacters && | ||
| content} | ||
| </span> | ||
| </Tooltip> | ||
| ); | ||
| Truncate.displayName = 'Truncate'; | ||
18 changes: 18 additions & 0 deletions
18
packages/react-core/src/components/Truncate/__tests__/Truncate.test.tsx
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,18 @@ | ||
| import React from 'react'; | ||
| import { shallow } from 'enzyme'; | ||
| import { Truncate } from '../Truncate'; | ||
|
|
||
| test('renders default truncation', () => { | ||
| const view = shallow(<Truncate content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} />); | ||
| expect(view).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders start truncation', () => { | ||
| const view = shallow(<Truncate content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} position={'start'} />); | ||
| expect(view).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test('renders middle truncation', () => { | ||
| const view = shallow(<Truncate content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} position={'middle'} />); | ||
| expect(view).toMatchSnapshot(); | ||
| }); |
58 changes: 58 additions & 0 deletions
58
packages/react-core/src/components/Truncate/__tests__/__snapshots__/Truncate.test.tsx.snap
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,58 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`renders default truncation 1`] = ` | ||
| <Tooltip | ||
| content="Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan." | ||
| position="top" | ||
| > | ||
| <span | ||
| className="pf-c-truncate" | ||
| > | ||
| <span | ||
| className="pf-c-truncate__start" | ||
| > | ||
| Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan. | ||
| </span> | ||
| </span> | ||
| </Tooltip> | ||
| `; | ||
|
|
||
| exports[`renders middle truncation 1`] = ` | ||
| <Tooltip | ||
| content="Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan." | ||
| position="top" | ||
| > | ||
| <span | ||
| className="pf-c-truncate" | ||
| > | ||
| <span | ||
| className="pf-c-truncate__start" | ||
| > | ||
| Vestibulum interdum risus et enim faucibus, sit amet molestie est ac | ||
| </span> | ||
| <span | ||
| className="pf-c-truncate__end" | ||
| > | ||
| cumsan. | ||
| </span> | ||
| </span> | ||
| </Tooltip> | ||
| `; | ||
|
|
||
| exports[`renders start truncation 1`] = ` | ||
| <Tooltip | ||
| content="Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan." | ||
| position="top" | ||
| > | ||
| <span | ||
| className="pf-c-truncate" | ||
| > | ||
| <span | ||
| className="pf-c-truncate__end" | ||
| > | ||
| Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan. | ||
| | ||
| </span> | ||
| </span> | ||
| </Tooltip> | ||
| `; |
59 changes: 59 additions & 0 deletions
59
packages/react-core/src/components/Truncate/examples/Truncate.md
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,59 @@ | ||
| --- | ||
| id: Truncate | ||
| section: components | ||
| propComponents: [Truncate] | ||
| beta: true | ||
| --- | ||
|
|
||
| import './TruncateExamples.css'; | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Default | ||
| ```js | ||
| import React from 'react'; | ||
| import { Truncate } from '@patternfly/react-core'; | ||
| <div className="pf-c-truncate--example"> | ||
| <Truncate | ||
| content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} | ||
| /> | ||
| </div> | ||
| ``` | ||
|
|
||
| ### Middle | ||
|
|
||
| ```js | ||
| import React from 'react'; | ||
| import { Truncate } from '@patternfly/react-core'; | ||
| <div className="pf-c-truncate--example"> | ||
| <Truncate | ||
| content={'redhat_logo_black_and_white_reversed_simple_with_fedora_container.zip'} | ||
| trailingNumChars={10} | ||
| position={'middle'} | ||
| /> | ||
| </div> | ||
| ``` | ||
|
|
||
| ### Start | ||
| ```js | ||
| import React from 'react'; | ||
| import { Truncate } from '@patternfly/react-core'; | ||
| <div className="pf-c-truncate--example"> | ||
| <Truncate | ||
| content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} | ||
| position={'start'} | ||
| /> | ||
| </div> | ||
| ``` | ||
|
|
||
| ### Default with tooltip at the bottom | ||
| ```js | ||
| import React from 'react'; | ||
| import { Truncate } from '@patternfly/react-core'; | ||
| <div className="pf-c-truncate--example"> | ||
| <Truncate | ||
| content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'} | ||
| tooltipPosition={'bottom'} | ||
| /> | ||
| </div> | ||
| ``` |
9 changes: 9 additions & 0 deletions
9
packages/react-core/src/components/Truncate/examples/TruncateExamples.css
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,9 @@ | ||
| .pf-c-truncate--example { | ||
| width: 320px; | ||
| resize: horizontal; | ||
| overflow: auto; | ||
| min-width: 161px; | ||
| max-width: 100%; | ||
| padding: var(--pf-global--spacer--md); | ||
| border: var(--pf-global--BorderWidth--sm) solid var(--pf-global--BorderColor--100); | ||
| } |
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 @@ | ||
| export * from './Truncate'; |
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.
Wondering if we considered just using
childrenfor content instead. I don't have much of a preference, but curious about your thoughts.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.
I think that if we know that the content will always be a string, there is no need to have it as children and just use a prop content. But this was one thing that I knew that someone will be commenting, so totally up for debate :D
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.
I wonder if at some point we'd want
contentto be bold or italicized. In that case, I think we'd want to just use ReactNode. If there's potential for that, I think it's worth a debate, or at least the changing of the type, otherwise if we know it will always be a string, what you have is fine with me.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.
This is a good question for @mattnolting
Will the css that controls the truncation still work if someone wants to put an icon or link somewhere in the string?
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 text can be bold or italicized, you can just add those tags around. But with the icon it won't work. So I think that it is best we keep it as string.