Skip to content

Commit 3fd1bbf

Browse files
committed
Fix #46
1 parent 1f6911e commit 3fd1bbf

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var TextTruncate = require('react-text-truncate'); // CommonJS or UMD
4141
Please polyfill `requestAnimationFrame` before you called `<TextTruncated />`
4242

4343
## Changelog
44+
* 0.10.0 Allow pass `element` to specify root element type
4445
* 0.9.3 Fix SSR window not found (removed raf polyfill)
4546
* 0.9.2 Fix styles prop is not working when the text is not truncated
4647
* 0.9.1 Fix async this.forceUpdate() issue

lib/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,21 @@
244244
var _this2 = this;
245245

246246
var _props2 = this.props,
247+
element = _props2.element,
247248
text = _props2.text,
248249
containerClassName = _props2.containerClassName;
249250

250251

251-
var renderText = text;
252-
if (this.scope) {
253-
renderText = this.getRenderText();
254-
}
252+
var renderText = this.scope ? this.getRenderText() : text;
253+
var rootProps = {
254+
ref: function ref(el) {
255+
_this2.scope = el;
256+
},
257+
className: containerClassName,
258+
style: { overflow: 'hidden' }
259+
};
255260

256-
return _react2.default.createElement(
257-
'div',
258-
{ ref: function ref(el) {
259-
_this2.scope = el;
260-
}, className: containerClassName, style: { overflow: 'hidden' } },
261-
renderText
262-
);
261+
return (0, _react.createElement)(element, rootProps, renderText);
263262
}
264263
}]);
265264

@@ -268,12 +267,14 @@
268267

269268
TextTruncate.propTypes = {
270269
containerClassName: _propTypes2.default.string,
270+
element: _propTypes2.default.string,
271271
line: _propTypes2.default.number,
272272
text: _propTypes2.default.string,
273273
textTruncateChild: _propTypes2.default.node,
274274
truncateText: _propTypes2.default.string
275275
};
276276
TextTruncate.defaultProps = {
277+
element: 'div',
277278
line: 1,
278279
text: '',
279280
truncateText: '…'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-text-truncate",
3-
"version": "0.9.3",
3+
"version": "0.10.0",
44
"description": "Truncate text for React.js",
55
"main": "lib/index.js",
66
"scripts": {

src/TextTruncate.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import React, {Component} from 'react';
1+
import React, {Component, createElement} from 'react';
22
import PropTypes from 'prop-types';
33

44
export default class TextTruncate extends Component {
55
static propTypes = {
66
containerClassName: PropTypes.string,
7+
element: PropTypes.string,
78
line: PropTypes.number,
89
text: PropTypes.string,
910
textTruncateChild: PropTypes.node,
1011
truncateText: PropTypes.string
1112
};
1213

1314
static defaultProps = {
15+
element: 'div',
1416
line: 1,
1517
text: '',
1618
truncateText: '…'
@@ -154,19 +156,18 @@ export default class TextTruncate extends Component {
154156

155157
render() {
156158
const {
159+
element,
157160
text,
158161
containerClassName,
159162
} = this.props;
160163

161-
let renderText = text;
162-
if (this.scope) {
163-
renderText = this.getRenderText();
164-
}
164+
const renderText = this.scope ? this.getRenderText() : text;
165+
const rootProps = {
166+
ref: (el) => {this.scope = el},
167+
className: containerClassName,
168+
style: {overflow: 'hidden'}
169+
};
165170

166-
return (
167-
<div ref={(el) => {this.scope = el;}} className={containerClassName} style={{overflow: 'hidden'}}>
168-
{renderText}
169-
</div>
170-
);
171+
return createElement(element, rootProps, renderText);
171172
}
172173
};

0 commit comments

Comments
 (0)