Skip to content

feat(Text): add maxLines prop #5018

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 3 commits into from
Aug 29, 2023
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
30 changes: 30 additions & 0 deletions packages/main/src/components/Text/Text.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,35 @@ describe('Text', () => {
.should('have.css', 'overflow', 'hidden');
});

it('maxLines', () => {
cy.mount(
<Text data-testid="text" style={{ width: '300px' }}>
{longText}
</Text>
);
cy.findByTestId('text').invoke('outerHeight').should('equal', 240);

cy.mount(
<Text data-testid="text" style={{ width: '300px' }} maxLines={0}>
{longText}
</Text>
);
cy.findByTestId('text').invoke('outerHeight').should('equal', 240);
cy.mount(
<Text data-testid="text" style={{ width: '300px' }} maxLines={1}>
{longText}
</Text>
);
cy.findByTestId('text').invoke('outerHeight').should('equal', 16);
cy.mount(
<Text data-testid="text" style={{ width: '300px' }} maxLines={4}>
{longText}
</Text>
);
cy.findByTestId('text').invoke('outerHeight').should('equal', 64);
});

cypressPassThroughTestsFactory(Text);
});

const longText = `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`;
6 changes: 6 additions & 0 deletions packages/main/src/components/Text/Text.jss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ export const TextStyles = {
'&$renderWhitespace': {
whiteSpace: 'pre'
}
},
maxLines: {
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
overflow: 'hidden',
WebkitLineClamp: 'var(--_ui5wcr_maxLines)'
}
};
22 changes: 13 additions & 9 deletions packages/main/src/components/Text/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clsx } from 'clsx';
import type { ReactNode } from 'react';
import type { CSSProperties, ReactNode } from 'react';
import React, { forwardRef } from 'react';
import { createUseStyles } from 'react-jss';
import type { CommonProps } from '../../interfaces/index.js';
Expand All @@ -18,6 +18,10 @@ export interface TextPropTypes extends CommonProps {
* Defines whether the text wraps when there is not enough space.
*/
wrapping?: boolean;
/**
* Limits the number of lines for wrapping texts.
*/
maxLines?: number;
}

const useStyles = createUseStyles(TextStyles, { name: 'Text' });
Expand All @@ -26,28 +30,28 @@ const useStyles = createUseStyles(TextStyles, { name: 'Text' });
* <br />__Note:__ Line breaks will always be visualized except when the wrapping property is set to false. In addition, tabs and whitespace can be preserved by setting the renderWhitespace property to true.
*/
const Text = forwardRef<HTMLSpanElement, TextPropTypes>((props, ref) => {
const { children, renderWhitespace, wrapping, className, style, slot, ...rest } = props;

const { children, renderWhitespace, wrapping = true, className, style, maxLines, ...rest } = props;
const classes = useStyles();
const classNameString = clsx(
classes.text,
wrapping === false && classes.noWrap,
renderWhitespace && classes.renderWhitespace,
typeof maxLines === 'number' && classes.maxLines,
className
);

return (
<span ref={ref} style={style} className={classNameString} slot={slot} {...rest}>
<span
ref={ref}
style={{ '--_ui5wcr_maxLines': typeof maxLines === 'number' ? maxLines : undefined, ...style } as CSSProperties}
className={classNameString}
{...rest}
>
{children}
</span>
);
});

Text.defaultProps = {
renderWhitespace: false,
wrapping: true
};

Text.displayName = 'Text';

export { Text };