ConceptPortal-public/rsconcept/frontend/src/components/ui/TextContent.tsx

38 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-08-11 14:25:10 +03:00
import clsx from 'clsx';
import { globals } from '@/utils/constants';
import { truncateToLastWord } from '@/utils/utils';
2024-08-11 14:25:10 +03:00
import { CProps } from '../props';
export interface TextContentProps extends CProps.Styling {
/** Text to display. */
2024-08-11 14:25:10 +03:00
text: string;
/** Maximum number of symbols to display. */
2024-08-11 14:25:10 +03:00
maxLength?: number;
/** Disable full text in a tooltip. */
noTooltip?: boolean;
2024-08-11 14:25:10 +03:00
}
/**
* Displays text limited to a certain number of symbols.
*/
function TextContent({ className, text, maxLength, noTooltip, ...restProps }: TextContentProps) {
const truncated = maxLength ? truncateToLastWord(text, maxLength) : text;
2024-08-11 14:25:10 +03:00
const isTruncated = maxLength && text.length > maxLength;
return (
<div
className={clsx('text-xs text-pretty', className)}
data-tooltip-id={isTruncated && !noTooltip ? globals.value_tooltip : undefined}
data-tooltip-html={isTruncated && !noTooltip ? text : undefined}
2024-08-11 14:25:10 +03:00
{...restProps}
>
{truncated}
</div>
);
}
export default TextContent;