import * as React from 'react'; import { Button, ModalVariant, Content } from '@patternfly/react-core'; import { ComponentProps, createModalLauncher } from '../../../components/modal/createModalLauncher'; import { useModalLauncher } from '../../../components/modal/ModalProvider'; import './Truncate.scss'; const DEFAULT_MAX_LENGTH = 80; export type TruncateProps = { content: string; maxLength?: number; modalTitle?: string; /** When true, "more"/"less" toggle reveals the full text inline instead of opening a modal. */ expandInline?: boolean; 'data-test'?: string; }; type TruncateModalProps = ComponentProps & { content: string; }; const TruncateModal: React.FC> = ({ content }) => { return ( {content} ); }; export const Truncate: React.FC = ({ content, modalTitle, maxLength = DEFAULT_MAX_LENGTH, expandInline = false, 'data-test': dataTest, }) => { const showModal = useModalLauncher(); const [isExpanded, setIsExpanded] = React.useState(false); const shouldTruncate = content.length > maxLength; if (!shouldTruncate) { return {content}; } const truncatedText = `${content.slice(0, maxLength)}...`; if (expandInline) { return ( {isExpanded ? content : truncatedText} {' '} ); } return ( {truncatedText} ); };