Skip to content

Commit f82b40b

Browse files
authored
Merge pull request #33 from Panya/patch-3
Wrong truncating when a container has long words without spaces
2 parents 17768ff + 5875703 commit f82b40b

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
var displayLine = line;
174174
var width = 0;
175175
var lastIsEng = false;
176+
var lastSpaceIndex = -1;
176177

177178
while (displayLine--) {
178179
var ext = displayLine ? '' : truncateText + ' ' + childText;
@@ -196,8 +197,11 @@
196197
truncatedText = text.substr(startPos, currentPos - 1);
197198
}
198199
if (lastIsEng) {
199-
currentPos = truncatedText.lastIndexOf(' ');
200-
truncatedText = text.substr(startPos, currentPos);
200+
lastSpaceIndex = truncatedText.lastIndexOf(' ');
201+
if (lastSpaceIndex > -1) {
202+
currentPos = lastSpaceIndex;
203+
truncatedText = text.substr(startPos, currentPos);
204+
}
201205
}
202206
width = this.measureWidth(truncatedText + ext);
203207
} while (width >= scopeWidth);

src/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ export class App extends Component {
9292
<h5>5. Block-level textTruncateChild</h5>
9393
<TextTruncate {...props} textTruncateChild={<div>Block level child</div>}/>
9494
</div>
95+
<div id='sample-6'>
96+
<h5>6. Long words inside a small container</h5>
97+
<div style={{
98+
width: 150
99+
}}><TextTruncate line={3} text="Национал-Большевизм. Сталинская массовая культура и формирование русского национального самосознания (1931-1956)" /></div>
100+
</div>
95101
</div>
96102
</div>
97103
)

src/TextTruncate.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export default class TextTruncate extends Component {
8888
let displayLine = line;
8989
let width = 0;
9090
let lastIsEng = false;
91+
let lastSpaceIndex = -1;
9192

9293
while (displayLine--) {
9394
let ext = displayLine ? '' : truncateText + ' ' + childText;
@@ -111,8 +112,11 @@ export default class TextTruncate extends Component {
111112
truncatedText = text.substr(startPos, currentPos - 1);
112113
}
113114
if (lastIsEng) {
114-
currentPos = truncatedText.lastIndexOf(' ');
115-
truncatedText = text.substr(startPos, currentPos);
115+
lastSpaceIndex = truncatedText.lastIndexOf(' ');
116+
if (lastSpaceIndex > -1) {
117+
currentPos = lastSpaceIndex;
118+
truncatedText = text.substr(startPos, currentPos);
119+
}
116120
}
117121
width = this.measureWidth(truncatedText + ext);
118122
} while (width >= scopeWidth);

0 commit comments

Comments
 (0)