Skip to content

fix(Text): only break words if text can wrap #6432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/main/src/components/Text/Text.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Text', () => {
{longText}
</Text>
);
cy.findByTestId('text').invoke('outerHeight').should('equal', 240);
cy.findByTestId('text').invoke('outerHeight').should('equal', 16);
cy.mount(
<Text data-testid="text" style={{ width: '300px' }} maxLines={1}>
{longText}
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/components/Text/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const meta = {
children: { control: 'text' }
},
args: {
wrapping: true,
children: `If "renderWhitespace" is set to true, there will be thirteen white spaces after this sentence. Lorem ipsum dolor st amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat`
}
} satisfies Meta<typeof Text>;
Expand Down
17 changes: 14 additions & 3 deletions packages/main/src/components/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ export interface TextPropTypes extends CommonProps {
renderWhitespace?: boolean;
/**
* Defines whether the text wraps when there is not enough space.
*
* @detault true
*/
wrapping?: boolean;
/**
* Limits the number of lines for wrapping texts.
*
* __Note:__ This prop only takes effect if the `wrapping` prop is set to `true`.
*/
maxLines?: number;
/**
Expand Down Expand Up @@ -64,11 +68,13 @@ const Text = forwardRef<HTMLSpanElement, TextPropTypes>((props, ref) => {
useStylesheet(styleData, Text.displayName);
const i18nBundle = useI18nBundle('@ui5/webcomponents-react');

const applyMaxLines = typeof maxLines === 'number' && maxLines > 1;

const classNameString = clsx(
classNames.text,
wrapping === false && classNames.noWrap,
(wrapping === false || (typeof maxLines === 'number' && maxLines <= 1)) && classNames.noWrap,
renderWhitespace && classNames.renderWhitespace,
typeof maxLines === 'number' && classNames.maxLines,
applyMaxLines && classNames.maxLines,
hyphenated && classNames.hyphenated,
className
);
Expand All @@ -89,7 +95,12 @@ const Text = forwardRef<HTMLSpanElement, TextPropTypes>((props, ref) => {
return (
<span
ref={ref}
style={{ '--_ui5wcr_maxLines': typeof maxLines === 'number' ? maxLines : undefined, ...style } as CSSProperties}
style={
{
'--_ui5wcr_maxLines': applyMaxLines ? maxLines : undefined,
...style
} as CSSProperties
}
className={classNameString}
{...rest}
>
Expand Down
Loading