Skip to content

Commit c609395

Browse files
committed
Updated classes based on core updates
1 parent 5dd2ac9 commit c609395

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,18 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
173173
};
174174

175175
const renderMaxDisplayContent = () => {
176+
const renderVisibleContent = (contentToRender: string) => (
177+
<span className={`${styles.truncate}__text`}>{contentToRender}</span>
178+
);
176179
if (!isTruncated) {
177-
return content;
180+
return renderVisibleContent(content);
178181
}
179-
const omissionElement = <span aria-hidden="true">{omissionContent}</span>;
182+
183+
const omissionElement = (
184+
<span className={`${styles.truncate}__omission`} aria-hidden="true">
185+
{omissionContent}
186+
</span>
187+
);
180188
const renderVisuallyHiddenContent = (contentToHide: string) => (
181189
<span className="pf-v6-screen-reader">{contentToHide}</span>
182190
);
@@ -186,14 +194,14 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
186194
<>
187195
{renderVisuallyHiddenContent(content.slice(0, maxCharsDisplayed * -1))}
188196
{omissionElement}
189-
<span>{content.slice(maxCharsDisplayed * -1)}</span>
197+
{renderVisibleContent(content.slice(maxCharsDisplayed * -1))}
190198
</>
191199
);
192200
}
193201
if (position === TruncatePosition.end) {
194202
return (
195203
<>
196-
<span>{content.slice(0, maxCharsDisplayed)}</span>
204+
{renderVisibleContent(content.slice(0, maxCharsDisplayed))}
197205
{omissionElement}
198206
{renderVisuallyHiddenContent(content.slice(maxCharsDisplayed))}
199207
</>
@@ -204,18 +212,18 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
204212
const trueMiddleEnd = Math.ceil(maxCharsDisplayed / 2) * -1;
205213
return (
206214
<>
207-
<span>{content.slice(0, trueMiddleStart)}</span>
215+
{renderVisibleContent(content.slice(0, trueMiddleStart))}
208216
{omissionElement}
209217
{renderVisuallyHiddenContent(content.slice(trueMiddleStart, trueMiddleEnd))}
210-
<span>{content.slice(trueMiddleEnd)}</span>
218+
{renderVisibleContent(content.slice(trueMiddleEnd))}
211219
</>
212220
);
213221
};
214222

215223
const truncateBody = (
216224
<span
217225
ref={subParentRef}
218-
className={css(styles.truncate, shouldRenderByMaxChars && 'class-tbd', className)}
226+
className={css(styles.truncate, shouldRenderByMaxChars && styles.modifiers.fixed, className)}
219227
{...props}
220228
>
221229
{!shouldRenderByMaxChars ? renderResizeObserverContent() : renderMaxDisplayContent()}

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

+19-11
Original file line numberDiff line numberDiff line change
@@ -150,58 +150,66 @@ test('renders with inherited element props spread to the component', () => {
150150
});
151151

152152
describe('Truncation with maxCharsDisplayed', () => {
153-
test(`Does not render with class class-tbd when maxCharsDisplayed is 0`, () => {
153+
test(`Does not render with class ${styles.modifiers.fixed} when maxCharsDisplayed is 0`, () => {
154154
render(<Truncate maxCharsDisplayed={0} data-testid="truncate-component" content="Test content" />);
155155

156-
expect(screen.getByTestId('truncate-component')).not.toHaveClass('class-tbd');
156+
expect(screen.getByTestId('truncate-component')).not.toHaveClass(styles.modifiers.fixed);
157157
});
158158

159-
test(`Renders with class class-tbd when maxCharsDisplayed is greater than 0`, () => {
159+
test(`Renders with class ${styles.modifiers.fixed} when maxCharsDisplayed is greater than 0`, () => {
160160
render(<Truncate maxCharsDisplayed={1} data-testid="truncate-component" content="Test content" />);
161161

162-
expect(screen.getByTestId('truncate-component')).toHaveClass('class-tbd');
162+
expect(screen.getByTestId('truncate-component')).toHaveClass(styles.modifiers.fixed);
163163
});
164164

165165
test('Renders with hidden truncated content at end by default when maxCharsDisplayed is passed', () => {
166166
render(<Truncate content="Default end position content truncated" maxCharsDisplayed={6} />);
167167

168-
expect(screen.getByText('Defaul')).not.toHaveClass('pf-v6-screen-reader');
168+
expect(screen.getByText('Defaul')).toHaveClass(`${styles.truncate}__text`, { exact: true });
169169
expect(screen.getByText('t end position content truncated')).toHaveClass('pf-v6-screen-reader');
170170
});
171171

172172
test('Renders with hidden truncated content at middle position when maxCharsDisplayed is passed and position="middle"', () => {
173173
render(<Truncate position="middle" content="Middle position contents being truncated" maxCharsDisplayed={10} />);
174174

175-
expect(screen.getByText('Middl')).not.toHaveClass('pf-v6-screen-reader');
175+
expect(screen.getByText('Middl')).toHaveClass(`${styles.truncate}__text`, { exact: true });
176176
expect(screen.getByText('e position contents being trun')).toHaveClass('pf-v6-screen-reader');
177-
expect(screen.getByText('cated')).not.toHaveClass('pf-v6-screen-reader');
177+
expect(screen.getByText('cated')).toHaveClass(`${styles.truncate}__text`, { exact: true });
178178
});
179179

180180
test('Renders with hidden truncated content at start when maxCharsDisplayed is passed and position="start"', () => {
181-
render(<Truncate content="Start position content truncated" maxCharsDisplayed={4} />);
181+
render(<Truncate position="start" content="Start position content truncated" maxCharsDisplayed={6} />);
182182

183-
expect(screen.getByText('Star')).not.toHaveClass('pf-v6-screen-reader');
184-
expect(screen.getByText('t position content truncated')).toHaveClass('pf-v6-screen-reader');
183+
expect(screen.getByText('Start position content tru')).toHaveClass('pf-v6-screen-reader');
184+
expect(screen.getByText('ncated')).toHaveClass(`${styles.truncate}__text`, { exact: true });
185185
});
186186

187187
test('Renders full content when maxCharsDisplayed exceeds the length of the content', () => {
188188
render(<Truncate content="This full content is rendered" maxCharsDisplayed={90} />);
189189

190-
expect(screen.getByText('This full content is rendered')).not.toHaveClass('pf-v6-screen-reader');
190+
expect(screen.getByText('This full content is rendered')).toHaveClass(`${styles.truncate}__text`, { exact: true });
191191
});
192192

193193
test('Renders ellipsis as omission content by default', () => {
194194
render(<Truncate content="Test truncation content" maxCharsDisplayed={5} />);
195195

196+
expect(screen.getByText('\u2026')).toHaveClass(`${styles.truncate}__omission`, { exact: true });
196197
expect(screen.getByText('\u2026')).toHaveAttribute('aria-hidden', 'true');
197198
});
198199

199200
test('Renders custom omission content when omissionContent is passed', () => {
200201
render(<Truncate omissionContent="---" content="Test truncation content" maxCharsDisplayed={5} />);
201202

203+
expect(screen.getByText('---')).toHaveClass(`${styles.truncate}__omission`, { exact: true });
202204
expect(screen.getByText('---')).toHaveAttribute('aria-hidden', 'true');
203205
});
204206

207+
test('Does not render omission content when maxCharsDisplayed exceeds the length of the content ', () => {
208+
render(<Truncate content="Test truncation content" maxCharsDisplayed={99} />);
209+
210+
expect(screen.queryByText('\u2026')).not.toBeInTheDocument();
211+
});
212+
205213
test('Matches snapshot with default position', () => {
206214
const { asFragment } = render(<Truncate content="Test truncation content" maxCharsDisplayed={3} />);
207215

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ exports[`Truncation with maxCharsDisplayed Matches snapshot with default positio
1414
position: top
1515
</p>
1616
<span
17-
class="pf-v6-c-truncate class-tbd"
17+
class="pf-v6-c-truncate pf-m-fixed"
1818
>
19-
<span>
19+
<span
20+
class="pf-v6-c-truncate__text"
21+
>
2022
Tes
2123
</span>
2224
<span
2325
aria-hidden="true"
26+
class="pf-v6-c-truncate__omission"
2427
>
2528
2629
</span>

0 commit comments

Comments
 (0)