Skip to content

Commit b62f671

Browse files
authored
Merge pull request #41 from dlabaj/longtexttooltip
feat(LongTextTooltip): Added LongTextTooltip component.
2 parents b40be28 + 6ad53a6 commit b62f671

File tree

7 files changed

+665
-0
lines changed

7 files changed

+665
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
section: extensions
3+
subsection: Component groups
4+
id: Long text tooltip
5+
source: react
6+
propComponents: ['LongTextTooltip']
7+
---
8+
9+
import LongTextTooltip from "@patternfly/react-component-groups/dist/dynamic/LongTextTooltip";
10+
11+
The **long text tooltip** component is a tooltip that can be used to display long text to users. It uses the `Tooltip` component to display the truncated text passed in as `content`. It uses `maxLength` prop to control the size of the content, and the `Tooltip` component to display the rest of the content.
12+
13+
## Examples
14+
15+
### LongTextTooltip component
16+
17+
To provide users with long text, a basic long text tooltip should contain an appropriate and informative `content` and `maxLength`. Additionally you can pass in a `tooltipPosition` to control the position of the tooltip, and `tooltipMaxWidth` to control the tool tip width.
18+
19+
```js file="./LongTextTooltipExample.tsx"
20+
21+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import { TooltipPosition } from '@patternfly/react-core';
3+
import LongTextTooltip from "@patternfly/react-component-groups/dist/dynamic/LongTextTooltip";
4+
5+
export const LongTextTooltipExample: React.FunctionComponent = () => (
6+
<LongTextTooltip
7+
content="This is a very long tooltip that will be truncated to fit the screen. It will also have a max width of 400px."
8+
maxLength={40}
9+
tooltipPosition={TooltipPosition.bottom}/>
10+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import LongTextTooltip from './LongTextTooltip';
3+
import { render } from '@testing-library/react';
4+
import { TooltipPosition } from '@patternfly/react-core';
5+
6+
describe('LongTextTooltip component', () => {
7+
it('should render', () => {
8+
expect(render(<LongTextTooltip />)).toMatchSnapshot();
9+
});
10+
11+
it('should render content', () => {
12+
expect(render(<LongTextTooltip content="Lorem Impsum" />)).toMatchSnapshot();
13+
});
14+
15+
it('should render content with maxLength', () => {
16+
expect(
17+
render(<LongTextTooltip content="Lorem Impsum" maxLength={50} />)).toMatchSnapshot();
18+
});
19+
20+
it('should render content with maxLength shorter than content', () => {
21+
expect(render(<LongTextTooltip content="Lorem Impsum" maxLength={1} />)).toMatchSnapshot();
22+
});
23+
24+
it('should render tooltip in a different spot', () => {
25+
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.bottom}/>)).toMatchSnapshot();
26+
});
27+
28+
it('should render tooltip in a different spot', () => {
29+
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.left} />)).toMatchSnapshot();
30+
});
31+
32+
it('should render tooltip in a different spot', () => {
33+
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.right} />)).toMatchSnapshot();
34+
});
35+
36+
it('should render content tooltip in a different spot with different width', () => {
37+
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.left} tooltipMaxWidth="10vw" />)).toMatchSnapshot();
38+
});
39+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Tooltip, TooltipPosition, TooltipProps } from '@patternfly/react-core';
2+
import React from 'react';
3+
4+
export interface LongTextTooltipProps extends Omit<TooltipProps, 'content'> {
5+
/** Content to display */
6+
content?: string;
7+
/** Maximum length of the content being displayed in pixels */
8+
maxLength?: number;
9+
/** Position of the tooltip */
10+
tooltipPosition?: TooltipPosition;
11+
/** Maximum width of the tooltip */
12+
tooltipMaxWidth?: string;
13+
}
14+
15+
const LongTextTooltip: React.FC<LongTextTooltipProps> = ({
16+
content = '',
17+
maxLength = Infinity,
18+
tooltipMaxWidth = '50vw',
19+
tooltipPosition = TooltipPosition.top,
20+
...rest
21+
}: LongTextTooltipProps) => {
22+
const truncate = (str: string, max: number) => (str.length > max ? str.substr(0, max - 1) + '…' : str);
23+
24+
return content.length > maxLength ? (
25+
<Tooltip maxWidth={tooltipMaxWidth} position={tooltipPosition} content={<div>{content}</div>} {...rest}>
26+
<div>{truncate(content, maxLength)}</div>
27+
</Tooltip>
28+
) : (
29+
<span>{content}</span>
30+
);
31+
};
32+
33+
export default LongTextTooltip;

0 commit comments

Comments
 (0)