Skip to content

Commit a66ce81

Browse files
fix(Truncate): allowed punctuation at start of content (#11798)
* fix(Truncate): allowed punctuation at start of content * Added comment for test about lrm entity in snapshot
1 parent f8b21b5 commit a66ce81

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

packages/react-core/src/components/Truncate/Truncate.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,18 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
142142
setShouldRenderByMaxChars(maxCharsDisplayed > 0);
143143
}, [maxCharsDisplayed]);
144144

145+
const lrmEntity = <Fragment>&lrm;</Fragment>;
146+
const isStartPosition = position === TruncatePosition.start;
147+
const isEndPosition = position === TruncatePosition.end;
148+
145149
const renderResizeObserverContent = () => {
146-
if (position === TruncatePosition.end || position === TruncatePosition.start) {
150+
if (isEndPosition || isStartPosition) {
147151
return (
148152
<>
149153
<span ref={textRef} className={truncateStyles[position]}>
154+
{isStartPosition && lrmEntity}
150155
{content}
151-
{position === TruncatePosition.start && <Fragment>&lrm;</Fragment>}
156+
{isStartPosition && lrmEntity}
152157
</span>
153158
</>
154159
);
@@ -195,7 +200,7 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
195200
</>
196201
);
197202
}
198-
if (position === TruncatePosition.end) {
203+
if (isEndPosition) {
199204
return (
200205
<>
201206
{renderVisibleContent(content.slice(0, maxCharsDisplayed))}

packages/react-core/src/components/Truncate/__tests__/Truncate.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render, screen, within } from '@testing-library/react';
2-
import { Truncate } from '../Truncate';
2+
import { Truncate, TruncatePosition } from '../Truncate';
33
import styles from '@patternfly/react-styles/css/components/Truncate/truncate';
44
import '@testing-library/jest-dom';
55

@@ -67,7 +67,9 @@ test('renders default truncation', () => {
6767
expect(asFragment()).toMatchSnapshot();
6868
});
6969

70-
test('renders start truncation with &lrm; at end', () => {
70+
// If this snapshot fails and the output text doesn't seem like it's changed, it most likely
71+
// is due to the &lrm; HTML entity isn't rendering correctly.
72+
test('renders start truncation with &lrm; at start and end', () => {
7173
const { asFragment } = render(
7274
<Truncate
7375
content={'Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.'}

packages/react-core/src/components/Truncate/__tests__/__snapshots__/Truncate.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ exports[`renders default truncation 1`] = `
6565
</DocumentFragment>
6666
`;
6767

68-
exports[`renders start truncation with &lrm; at end 1`] = `
68+
exports[`renders start truncation with &lrm; at start and end 1`] = `
6969
<DocumentFragment>
7070
<div
7171
data-testid="Tooltip-mock"
@@ -86,7 +86,7 @@ exports[`renders start truncation with &lrm; at end 1`] = `
8686
<span
8787
class="pf-v6-c-truncate__end"
8888
>
89-
Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.‎
89+
Vestibulum interdum risus et enim faucibus, sit amet molestie est accumsan.‎
9090
</span>
9191
</span>
9292
</DocumentFragment>

0 commit comments

Comments
 (0)