Skip to content

Commit 4882a61

Browse files
committed
Added TSDoc for Truncated Text
1 parent b4b8eba commit 4882a61

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/components/OrgListCard/TruncatedText.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
import React, { useState, useEffect, useRef } from 'react';
22

3+
/**
4+
* Props for the `TruncatedText` component.
5+
*
6+
* Includes the text to be displayed and an optional maximum width override.
7+
*/
38
interface InterfaceTruncatedTextProps {
9+
/** The full text to display. It may be truncated if it exceeds the maximum width. */
410
text: string;
11+
/** Optional: Override the maximum width for truncation. */
512
maxWidthOverride?: number;
613
}
714

15+
/**
16+
* A React functional component that displays text and truncates it with an ellipsis (`...`)
17+
* if the text exceeds the available width or the `maxWidthOverride` value.
18+
*
19+
* The component adjusts the truncation dynamically based on the available space
20+
* or the `maxWidthOverride` value. It also listens for window resize events to reapply truncation.
21+
*
22+
* @param props - The props for the component.
23+
* @returns A heading element (`<h6>`) containing the truncated or full text.
24+
*
25+
* @example
26+
* ```tsx
27+
* <TruncatedText text="This is a very long text" maxWidthOverride={150} />
28+
* ```
29+
*/
830
const TruncatedText: React.FC<InterfaceTruncatedTextProps> = ({
931
text,
1032
maxWidthOverride,
1133
}) => {
12-
const [truncatedText, setTruncatedText] = useState<string>('');
13-
const textRef = useRef<HTMLHeadingElement>(null);
34+
const [truncatedText, setTruncatedText] = useState<string>(''); // State to store the truncated text
35+
const textRef = useRef<HTMLHeadingElement>(null); // Ref to measure the width of the text container
1436

1537
useEffect(() => {
38+
/**
39+
* Truncate the text based on the available width or the `maxWidthOverride` value.
40+
*/
1641
const truncateText = (): void => {
1742
const element = textRef.current;
1843
if (element) {
@@ -21,7 +46,7 @@ const TruncatedText: React.FC<InterfaceTruncatedTextProps> = ({
2146

2247
const computedStyle = getComputedStyle(element);
2348
const fontSize = parseFloat(computedStyle.fontSize);
24-
const charPerPx = 0.065 + fontSize * 0.002; // Adjust value for truncation according to our needs
49+
const charPerPx = 0.065 + fontSize * 0.002;
2550
const maxChars = Math.floor(maxWidth * charPerPx);
2651

2752
setTruncatedText(
@@ -34,7 +59,6 @@ const TruncatedText: React.FC<InterfaceTruncatedTextProps> = ({
3459

3560
truncateText();
3661
window.addEventListener('resize', truncateText);
37-
3862
return () => window.removeEventListener('resize', truncateText);
3963
}, [text, maxWidthOverride]);
4064

0 commit comments

Comments
 (0)