Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
24 changes: 22 additions & 2 deletions packages/components/typography/ellipsis/Truncate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ export default class Truncate extends React.Component<TruncateProps, TruncateSta

trimRight = (text: string) => text.replace(/\s+$/, '');

breakAtWordBoundary = (text: string) => {
// Try to break at word boundary to avoid splitting words
// Look backwards for a space or punctuation
const wordBoundaryMatch = text.match(/^(.*[\s\-,;.!?])/);
if (wordBoundaryMatch) {
const [, boundaryText] = wordBoundaryMatch;
if (boundaryText.trim().length > 0) {
return boundaryText;
}
}
return text;
};

createMarkup = (str: string) => (
<span className={this.props.lineClassName} dangerouslySetInnerHTML={{ __html: str }} />
);
Expand Down Expand Up @@ -296,6 +309,7 @@ export default class Truncate extends React.Component<TruncateProps, TruncateSta
}

let lastLineText = textRest.slice(0, lower);
lastLineText = this.breakAtWordBoundary(lastLineText);

if (trimWhitespace) {
lastLineText = trimRight(lastLineText);
Expand Down Expand Up @@ -345,9 +359,15 @@ export default class Truncate extends React.Component<TruncateProps, TruncateSta
continue;
}

resultLine = textWords.slice(0, lower).join('');
let resultLineText = textWords.slice(0, lower).join('');

const boundaryText = this.breakAtWordBoundary(resultLineText);
if (boundaryText !== resultLineText) {
resultLineText = boundaryText;
lower = boundaryText.length;
}

resultLine = restoreReplacedLinks(resultLine);
resultLine = restoreReplacedLinks(resultLineText);

textLines[0].splice(0, lower);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/tdesign-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ spline: explain

## 🌈 1.9.3 `2024-10-31`
### 🐞 Bug Fixes
- `Select`: 修复`valueDisplay`下的`onClose`回调问题 @uyarn ([#3154](https://github.com/Tencent/tdesign-react/pull/3154))
- `Typography`: 修复 `Typography` 的`Ellipsis` 功能在中文下的问题 @HaixingOoO ([#3158](https://github.com/Tencent/tdesign-react/pull/3158))
- `Select`: 修复 `valueDisplay` 下的 `onClose` 回调问题 @uyarn ([#3154](https://github.com/Tencent/tdesign-react/pull/3154))
- `Typography`: 修复 `ellipsis` 功能在中文下的问题 @HaixingOoO ([#3158](https://github.com/Tencent/tdesign-react/pull/3158))
- `Form`: 修复 `FormList` 或 `FormItem` 数据中的 `getFieldsValue` 问题 @HaixingOoO ([#3149](https://github.com/Tencent/tdesign-react/pull/3149))
- `Form`: 修复动态渲染表单无法使用 `setFieldsValue` 预设数据的问题 @l123wx ([#3145](https://github.com/Tencent/tdesign-react/pull/3145))
- `lib`: 修复`1.9.2`升级依赖改动导致`lib`错误携带`style`导致在`next`下不可用的异常 @honkinglin ([#3165](https://github.com/Tencent/tdesign-react/pull/3165))
Expand Down
Loading