You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(Truncate): added logic to truncate based on max characters (#11742)
* feat(Truncate): added logic to truncate based on max characters
* Converted existing examples to TS
* Wrote tests for max chars logic
* Wrapped visible content in classless spans
* Updated classes based on core updates
The default behavior of the `Truncate` component is to truncate based on whether the content can fit within the width of its parent container, and to prevent text from wrapping. The following examples that use this default behavior render the `<Truncate>` component inside a resizable container, allowing you to see how the parent container width affects the truncation.
13
+
12
14
### Default
13
-
```js
14
-
import { Truncate } from'@patternfly/react-core';
15
-
16
-
<div className="truncate-example-resize">
17
-
<Truncate
18
-
content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'}
19
-
/>
20
-
</div>
15
+
16
+
By default content will be truncated at its end when it cannot fit entirely inside its parent container.
When passing a `position` property with a value of "middle", the position of the truncation will change based on the parent container's width and the amount of `trailingNumChars` passed in. The `trailingNumChars` will always be displayed, while the rest of the content will be truncated based on the parent container width.
25
+
26
+
```ts file="./TruncateMiddle.tsx"
27
+
34
28
```
35
29
36
30
### Start
37
-
```js
38
-
import { Truncate } from'@patternfly/react-core';
39
-
40
-
<div className="truncate-example-resize">
41
-
<Truncate
42
-
content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'}
43
-
position={'start'}
44
-
/>
45
-
</div>
31
+
32
+
You can truncate content at its start by passing the `position` property with a value of "start". This can be useful if you have several strings to truncate that have similar text at the start, but unique text at the end that you want to have visible.
33
+
34
+
```ts file="./TruncateStart.tsx"
35
+
46
36
```
47
37
48
-
### Default with tooltip at the bottom
49
-
```js
50
-
import { Truncate } from'@patternfly/react-core';
38
+
### With custom tooltip position
39
+
40
+
You can customize the position of the `<Tooltip>` that is rendered by passing in the `tooltipPosition` property. The following example overrides the default "top" position with a "bottom" position.
41
+
42
+
```ts file="./TruncateCustomTooltipPosition.tsx"
43
+
44
+
```
45
+
46
+
### Based on max characters
47
+
48
+
Rather than observing container width, you can have truncation be based on a maximum amount of characters that should always be displayed via the `maxCharsDisplayed` property. While the content's parent container width will not have an affect on whether truncation occurs, it will affect whether the content wraps. This property must be set to a value larger than `0`, otherwise the component will fall back to observing container width.
49
+
50
+
Truncating based on a maximum amount of characters will truncate the content at the end by default. When the `position` property is set to "middle", the truncation will split the content as evenly as possible, providing a more "true middle" truncation.
51
+
52
+
```ts file="./TruncateMaxChars.tsx"
51
53
52
-
<div className="truncate-example-resize">
53
-
<Truncate
54
-
content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'}
0 commit comments