-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Truncate): Added the truncate component.
- Loading branch information
Showing
13 changed files
with
3,361 additions
and
49 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
17 changes: 17 additions & 0 deletions
17
...tternfly-docs/content/extensions/component-groups/examples/Truncate/Truncate.md
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,17 @@ | ||
--- | ||
section: extensions | ||
subsection: Component groups | ||
id: Truncate | ||
source: react | ||
propComponents: ['Truncate'] | ||
--- | ||
|
||
import Truncate from "@patternfly/react-component-groups/dist/dynamic/Truncate"; | ||
|
||
## Component usage | ||
|
||
### Truncate component | ||
|
||
```js file="./TruncateExample.tsx" | ||
|
||
``` |
6 changes: 6 additions & 0 deletions
6
...patternfly-docs/content/extensions/component-groups/examples/Truncate/TruncateExample.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,6 @@ | ||
import React from 'react'; | ||
import Truncate from "@patternfly/react-component-groups/dist/dynamic/Truncate"; | ||
|
||
|
||
export const BasicExample: React.FunctionComponent = () => <Truncate text="This is a long string that will be truncated" length={10} />; | ||
|
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,79 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import Truncate from './Truncate'; | ||
|
||
const text = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore | ||
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea | ||
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla | ||
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est | ||
laborum.`; | ||
|
||
describe('Truncate component', () => { | ||
describe('should render correctly', () => { | ||
[true, false].forEach((isInline) => { | ||
Check failure on line 13 in packages/module/src/Truncate/Truncate.test.js GitHub Actions / call-build-lint-test-workflow / lint
|
||
describe(isInline ? 'inline' : 'block', () => { | ||
it('without length specified', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
Check failure on line 17 in packages/module/src/Truncate/Truncate.test.js GitHub Actions / call-build-lint-test-workflow / lint
|
||
}); | ||
|
||
it('with short length', () => { | ||
const wrapper = render(<Truncate text={text} length={2} />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('clicking on expand toggles to collapse', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
wrapper.find('.ins-c-expand-button').first().simulate('click'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('custom expande button', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('custom button titles', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
wrapper.find('.ins-c-expand-button').first().simulate('click'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('clicking on expand toggles to collapse', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
wrapper.find('.ins-c-expand-button').first().simulate('click'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('custom expande button', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('custom button titles', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} expandText="Custom expand" collapseText="Custom collapse" />); | ||
wrapper.find('.ins-c-expand-button').first().simulate('click'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('when text length is less than user specified length', () => { | ||
const wrapper = render(<Truncate text={text} inline={isInline} length={1000} />); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('hovering over toggles to collapse', () => { | ||
const wrapper = render(<Truncate length={50} inline={isInline} text={text} expandOnMouseOver hideExpandText />); | ||
const eventElement = isInline ? wrapper.find('.ins-c-truncate').first() : wrapper.find('.ins-c-truncate').first().children().first(); | ||
|
||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
|
||
eventElement.simulate('mouseenter'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
|
||
eventElement.simulate('mouseleave'); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,88 @@ | ||
import React, { useState } from 'react'; | ||
import classNames from 'classnames'; | ||
import { Button, Stack, StackItem } from '@patternfly/react-core'; | ||
import sanitizeHtml from 'sanitize-html'; | ||
|
||
import './truncate.scss'; | ||
|
||
const dangerousHtml = (html: string) => ({ __html: sanitizeHtml(html) }); | ||
|
||
export interface TruncateProps { | ||
/** Additional classes added to the Truncate */ | ||
className?: string; | ||
/** Text to display inside the Truncate component */ | ||
text?: string; | ||
/** A set length for the truncate component. */ | ||
length?: number; | ||
/** Indicates if the component is expanded to show the entire text */ | ||
expandText?: React.ReactNode; | ||
collapseText?: React.ReactNode; | ||
/** Indicates if the text should be kept inline */ | ||
inline?: boolean; | ||
spaceBetween?: boolean; | ||
hideExpandText?: boolean; | ||
/** If set to true the text will be expanded when moused over */ | ||
expandOnMouseOver?: boolean; | ||
} | ||
|
||
const Truncate: React.FunctionComponent<TruncateProps> = ({ | ||
text = '', | ||
length = 150, | ||
expandText = 'Read more', | ||
hideExpandText = false, | ||
expandOnMouseOver = false, | ||
collapseText = 'Collapse', | ||
className, | ||
inline, | ||
spaceBetween, | ||
}: TruncateProps) => { | ||
const truncateClasses = classNames('ins-c-truncate', className, { [`is-inline`]: inline }, { [`is-block`]: !inline }); | ||
const trimmedText = text.substring(0, length); | ||
const textOverflow = text.length > length; | ||
const [showText, setShowText] = useState(false); | ||
const toggleText = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
event && event.preventDefault(); | ||
setShowText(!showText); | ||
}; | ||
|
||
const expandButton = ( | ||
<Button className="ins-c-expand-button" variant="link" onClick={toggleText}> | ||
{expandText} | ||
</Button> | ||
); | ||
const collapseButton = ( | ||
<Button className="ins-c-collapse-button" variant="link" onClick={toggleText}> | ||
{collapseText} | ||
</Button> | ||
); | ||
const textWithOverflow = showText === false ? `${trimmedText}${textOverflow ? '...' : ''}` : text; | ||
const html = dangerousHtml(textWithOverflow); | ||
const mouseOverHandler = expandOnMouseOver && { | ||
onMouseEnter: () => setShowText(true), | ||
onMouseLeave: () => setShowText(false), | ||
}; | ||
|
||
return inline ? ( | ||
<React.Fragment> | ||
<span className={truncateClasses} widget-type="InsightsTruncateInline" dangerouslySetInnerHTML={html} {...mouseOverHandler} /> | ||
{!hideExpandText && textOverflow && (showText === false ? expandButton : collapseButton)} | ||
</React.Fragment> | ||
) : ( | ||
<Stack className={truncateClasses}> | ||
<StackItem {...mouseOverHandler}> | ||
<span widget-type="InsightsTruncateBlock" dangerouslySetInnerHTML={html} /> | ||
</StackItem> | ||
{!hideExpandText && textOverflow && ( | ||
<StackItem | ||
className={classNames({ | ||
'pf-u-mt-sm': spaceBetween, | ||
})} | ||
> | ||
{showText === false ? expandButton : collapseButton} | ||
</StackItem> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default Truncate; |
Oops, something went wrong.