-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add previously removed LongTextTooltip
- Loading branch information
Showing
11 changed files
with
737 additions
and
91 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
38 changes: 38 additions & 0 deletions
38
packages/components/src/LongTextTooltip/LongTextTooltip.test.js
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,38 @@ | ||
import React from 'react'; | ||
import LongTextTooltip from './LongTextTooltip'; | ||
import { render } from '@testing-library/react'; | ||
import { TooltipPosition } from '@patternfly/react-core/dist/dynamic/components/Tooltip'; | ||
|
||
describe('LongTextTooltip component', () => { | ||
it('should render', () => { | ||
expect(render(<LongTextTooltip />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render content', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render content with maxLength', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" maxLength={50} />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render content with maxLength shorter than content', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" maxLength={1} />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render tooltip in a different spot', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.bottom} />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render tooltip in a different spot', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.left} />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render tooltip in a different spot', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.right} />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render content tooltip in a different spot with different width', () => { | ||
expect(render(<LongTextTooltip content="Lorem Impsum" tooltipPosition={TooltipPosition.left} tooltipMaxWidth="10vw" />)).toMatchSnapshot(); | ||
}); | ||
}); |
31 changes: 28 additions & 3 deletions
31
packages/components/src/LongTextTooltip/LongTextTooltip.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 |
---|---|---|
@@ -1,10 +1,35 @@ | ||
import React from 'react'; | ||
import LongTextTooltipPF, { LongTextTooltipProps } from '@patternfly/react-component-groups/dist/dynamic/LongTextTooltip'; | ||
import { Tooltip } from '@patternfly/react-core/dist/dynamic/components/Tooltip'; | ||
import { TooltipPosition } from '@patternfly/react-core/dist/dynamic/components/Tooltip'; | ||
import { TooltipProps } from '@patternfly/react-core/dist/dynamic/components/Tooltip'; | ||
|
||
export interface LongTextTooltipProps extends Omit<TooltipProps, 'content'> { | ||
content?: string; | ||
maxLength?: number; | ||
tooltipPosition?: TooltipPosition; | ||
tooltipMaxWidth?: string; | ||
} | ||
|
||
/** | ||
* @deprecated Do not use deprecated LongTextTooltip import, the component has been moved to @patternfly/react-component-groups | ||
* @deprecated Use official PatternFly Truncate component instead. This component will be removed soon! | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const LongTextTooltip: React.FunctionComponent<LongTextTooltipProps> = (props) => <LongTextTooltipPF {...props} />; | ||
const LongTextTooltip: React.FunctionComponent<LongTextTooltipProps> = ({ | ||
content = '', | ||
maxLength = Infinity, | ||
tooltipMaxWidth = '50vw', | ||
tooltipPosition = TooltipPosition.top, | ||
...rest | ||
}) => { | ||
const truncate = (str: string, max: number) => (str.length > max ? str.substr(0, max - 1) + '…' : str); | ||
|
||
return content.length > maxLength ? ( | ||
<Tooltip maxWidth={tooltipMaxWidth} position={tooltipPosition} content={<div>{content}</div>} {...rest}> | ||
<div>{truncate(content, maxLength)}</div> | ||
</Tooltip> | ||
) : ( | ||
<span>{content}</span> | ||
); | ||
}; | ||
|
||
export default LongTextTooltip; |
Oops, something went wrong.