react-show-more-text 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # React Show More Text 1.6.1
1
+ # React Show More Text 1.7.0
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
4
  [![Downloads][downloads-image]][npm-url]
5
5
  [![Build status][travis-image]][travis-url]
6
6
 
7
- The text surrounded by the component will be truncated. Anything surrounded by the component could be evaluated as text. The component react-show-more-text/ShowMoreText is fork of react-show-more/ShowMore, applied improvements, works with React 16.x.x, React 18.x.x added onClick event.
7
+ The text surrounded by the component will be truncated. Anything surrounded by the component could be evaluated as text. The component react-show-more-text/ShowMoreText is fork of react-show-more/ShowMore, applied improvements, added onClick event, works with React 16.x.x, React 18.x.x, Next.Js 13.3.x and upper.
8
8
 
9
9
  ## Demo
10
10
 
@@ -36,7 +36,7 @@ class Foo extends Component {
36
36
  more="Show more"
37
37
  less="Show less"
38
38
  className="content-css"
39
- anchorClass="my-anchor-css-class"
39
+ anchorClass="show-more-less-clickable"
40
40
  onClick={this.executeOnClick}
41
41
  expanded={false}
42
42
  width={280}
@@ -83,7 +83,7 @@ class Foo extends Component {
83
83
  | more | string, React node | 'Show more' | The text to display in the anchor element to show more. | `'Show more'`, `<span>Show more</span>` |
84
84
  | less | string, React node | 'Show less' | The text to display in the anchor element to show less. | `'Show less'`, `<span>Show less</span>` |
85
85
  | className | string | '' | Class name(s) to add on component content wrapper div. | `'wrapper-class'`, `'wrapper-class-1 wrapper-class-2'` |
86
- | anchorClass | string | '' | Class name(s) to add to the anchor elements. | `'my-anchor-class'`, `'class-1 class-2'` |
86
+ | anchorClass | string | 'show-more-less-clickable' | Class name(s) to add to the anchor elements. Should be the name of a css class defined globally by you. | `'my-anchor-class'`, `'class-1 class-2'` |
87
87
  | onClick | Function | | Function executed on click on 'Show more' or 'Show less' | `onClick={this.executeOnClick}` |
88
88
  | expanded | boolean | 'false' | Control the text to be shown as expanded | `expanded={true}` |
89
89
  | expandByClick | boolean | 'true' | Cancel the default anchor click expand behavior | `expandByClick={false}` |
@@ -1,193 +1,125 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.default = void 0;
9
-
10
7
  var _react = _interopRequireWildcard(require("react"));
11
-
12
8
  var _propTypes = require("prop-types");
13
-
14
9
  var _Truncate = _interopRequireDefault(require("./Truncate"));
15
-
16
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
-
20
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
-
22
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
-
24
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
25
-
26
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
27
-
28
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
-
30
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
-
32
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
33
-
34
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
35
-
36
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
37
-
38
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
39
-
40
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
41
-
11
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
42
13
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
43
-
44
- var ShowMoreText = /*#__PURE__*/function (_Component) {
45
- _inherits(ShowMoreText, _Component);
46
-
47
- var _super = _createSuper(ShowMoreText);
48
-
49
- function ShowMoreText(props) {
50
- var _this;
51
-
52
- _classCallCheck(this, ShowMoreText);
53
-
54
- _this = _super.call(this, props);
55
-
56
- _defineProperty(_assertThisInitialized(_this), "_isMounted", false);
57
-
58
- _defineProperty(_assertThisInitialized(_this), "handleTruncate", function (truncated) {
59
- if (_this._isMounted && truncated !== _this.state.truncated) {
60
- _this.setState({
61
- truncated: truncated
14
+ class ShowMoreText extends _react.Component {
15
+ constructor(props) {
16
+ super(props);
17
+ _defineProperty(this, "_isMounted", false);
18
+ _defineProperty(this, "handleTruncate", truncated => {
19
+ if (this._isMounted && truncated !== this.state.truncated) {
20
+ this.setState({
21
+ truncated
62
22
  });
63
-
64
23
  if (truncated) {
65
- _this.truncateRef.onResize();
24
+ this.truncateRef.onResize();
66
25
  }
67
-
68
- _this.props.onTruncate && _this.props.onTruncate();
26
+ this.props.onTruncate && this.props.onTruncate();
69
27
  }
70
28
  });
71
-
72
- _defineProperty(_assertThisInitialized(_this), "toggleLines", function (event) {
29
+ _defineProperty(this, "toggleLines", event => {
73
30
  event.preventDefault();
74
-
75
- var _self = _assertThisInitialized(_this);
76
-
31
+ var _self = this;
77
32
  if (!_self.props.expandByClick) {
78
33
  if (_self.props.onClick) {
79
34
  _self.props.onClick(_self.state.expanded, event);
80
35
  }
81
-
82
36
  return;
83
37
  }
84
-
85
- if (_this._isMounted) {
86
- _this.setState({
87
- expanded: !_this.state.expanded
88
- }, function () {
38
+ if (this._isMounted) {
39
+ this.setState({
40
+ expanded: !this.state.expanded
41
+ }, () => {
89
42
  if (_self.props.onClick) {
90
43
  _self.props.onClick(_self.state.expanded, event);
91
44
  }
92
45
  });
93
46
  }
94
47
  });
95
-
96
- _this.state = {
48
+ this.state = {
97
49
  expanded: false,
98
50
  truncated: false
99
51
  };
100
- return _this;
101
52
  }
102
-
103
- _createClass(ShowMoreText, [{
104
- key: "componentDidMount",
105
- value: function componentDidMount() {
106
- this._isMounted = true;
107
-
108
- var _self = this;
109
-
110
- if (this._isMounted) {
111
- this.setState({
112
- expanded: _self.props.expanded
113
- });
114
- }
53
+ componentDidMount() {
54
+ this._isMounted = true;
55
+ var _self = this;
56
+ if (this._isMounted) {
57
+ this.setState({
58
+ expanded: _self.props.expanded
59
+ });
115
60
  }
116
- }, {
117
- key: "componentWillUnmount",
118
- value: function componentWillUnmount() {
119
- this._isMounted = false;
120
- }
121
- }, {
122
- key: "render",
123
- value: function render() {
124
- var _this2 = this;
125
-
126
- var _this$props = this.props,
127
- children = _this$props.children,
128
- more = _this$props.more,
129
- less = _this$props.less,
130
- lines = _this$props.lines,
131
- anchorClass = _this$props.anchorClass,
132
- className = _this$props.className,
133
- width = _this$props.width,
134
- keepNewLines = _this$props.keepNewLines,
135
- truncatedEndingComponent = _this$props.truncatedEndingComponent,
136
- onTruncate = _this$props.onTruncate;
137
- var _this$state = this.state,
138
- expanded = _this$state.expanded,
139
- truncated = _this$state.truncated;
140
- return /*#__PURE__*/_react.default.createElement("div", {
141
- className: className
142
- }, /*#__PURE__*/_react.default.createElement(_Truncate.default, {
143
- width: width,
144
- lines: !expanded && lines,
145
- ellipsis: /*#__PURE__*/_react.default.createElement("span", null, truncatedEndingComponent, /*#__PURE__*/_react.default.createElement("a", {
146
- href: "",
147
- className: anchorClass,
148
- onClick: this.toggleLines
149
- }, more)),
150
- onTruncate: this.handleTruncate,
151
- ref: function ref(_ref) {
152
- return _this2.truncateRef = _ref;
153
- }
154
- }, keepNewLines ? children.split('\n').map(function (line, i, arr) {
155
- line = /*#__PURE__*/_react.default.createElement("span", {
156
- key: i
157
- }, line);
158
-
159
- if (i === arr.length - 1) {
160
- return line;
161
- } else {
162
- return [line, /*#__PURE__*/_react.default.createElement("br", {
163
- key: i + 'br'
164
- })];
165
- }
166
- }) : children), !truncated && expanded && /*#__PURE__*/_react.default.createElement("span", null, ' ', /*#__PURE__*/_react.default.createElement("a", {
167
- href: "",
61
+ }
62
+ componentWillUnmount() {
63
+ this._isMounted = false;
64
+ }
65
+ render() {
66
+ const {
67
+ children,
68
+ more,
69
+ less,
70
+ lines,
71
+ anchorClass,
72
+ className,
73
+ width,
74
+ keepNewLines,
75
+ truncatedEndingComponent,
76
+ onTruncate
77
+ } = this.props;
78
+ const {
79
+ expanded,
80
+ truncated
81
+ } = this.state;
82
+ return /*#__PURE__*/_react.default.createElement("div", {
83
+ className: className
84
+ }, /*#__PURE__*/_react.default.createElement(_Truncate.default, {
85
+ width: width,
86
+ lines: !expanded && lines,
87
+ ellipsis: /*#__PURE__*/_react.default.createElement("span", null, truncatedEndingComponent, /*#__PURE__*/_react.default.createElement("span", {
168
88
  className: anchorClass,
169
89
  onClick: this.toggleLines
170
- }, less)));
171
- }
172
- }]);
173
-
174
- return ShowMoreText;
175
- }(_react.Component);
176
-
90
+ }, more)),
91
+ onTruncate: this.handleTruncate,
92
+ ref: ref => this.truncateRef = ref
93
+ }, keepNewLines ? children.split("\n").map((line, i, arr) => {
94
+ line = /*#__PURE__*/_react.default.createElement("span", {
95
+ key: i
96
+ }, line);
97
+ if (i === arr.length - 1) {
98
+ return line;
99
+ } else {
100
+ return [line, /*#__PURE__*/_react.default.createElement("br", {
101
+ key: i + "br"
102
+ })];
103
+ }
104
+ }) : children), !truncated && expanded && /*#__PURE__*/_react.default.createElement("span", null, " ", /*#__PURE__*/_react.default.createElement("span", {
105
+ className: anchorClass,
106
+ onClick: this.toggleLines
107
+ }, less)));
108
+ }
109
+ }
177
110
  _defineProperty(ShowMoreText, "defaultProps", {
178
111
  lines: 3,
179
- more: 'Show more',
180
- less: 'Show less',
181
- anchorClass: '',
112
+ more: "Show more",
113
+ less: "Show less",
114
+ anchorClass: "show-more-less-clickable",
182
115
  onClick: undefined,
183
116
  expanded: false,
184
117
  width: 0,
185
118
  keepNewLines: false,
186
- truncatedEndingComponent: '... ',
119
+ truncatedEndingComponent: "... ",
187
120
  expandByClick: true,
188
121
  onTruncate: undefined
189
122
  });
190
-
191
123
  _defineProperty(ShowMoreText, "propTypes", {
192
124
  children: _propTypes.PropTypes.node,
193
125
  lines: _propTypes.PropTypes.number,
@@ -203,6 +135,5 @@ _defineProperty(ShowMoreText, "propTypes", {
203
135
  expandByClick: _propTypes.PropTypes.bool,
204
136
  onTruncate: _propTypes.PropTypes.func
205
137
  });
206
-
207
138
  var _default = ShowMoreText;
208
139
  exports.default = _default;
package/lib/Truncate.js CHANGED
@@ -1,208 +1,148 @@
1
1
  "use strict";
2
2
 
3
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.default = void 0;
9
-
10
7
  var _react = _interopRequireDefault(require("react"));
11
-
12
8
  var _propTypes = _interopRequireDefault(require("prop-types"));
13
-
14
- var _excluded = ["children", "ellipsis", "lines"];
15
-
16
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
10
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
19
-
20
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
21
-
22
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
23
-
24
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
-
26
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
27
-
28
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
29
-
30
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
31
-
32
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
33
-
34
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
35
-
36
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
37
-
38
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
39
-
40
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
41
-
42
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
43
-
44
11
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
45
-
46
- var Truncate = /*#__PURE__*/function (_React$Component) {
47
- _inherits(Truncate, _React$Component);
48
-
49
- var _super = _createSuper(Truncate);
50
-
51
- function Truncate() {
52
- var _this;
53
-
12
+ class Truncate extends _react.default.Component {
13
+ constructor() {
54
14
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
55
15
  args[_key] = arguments[_key];
56
16
  }
57
-
58
- _classCallCheck(this, Truncate);
59
-
60
- _this = _super.call.apply(_super, [this].concat(args));
61
-
62
- _defineProperty(_assertThisInitialized(_this), "state", {});
63
-
64
- _defineProperty(_assertThisInitialized(_this), "extractReplaceLinksKeys", function (content) {
65
- var _self = _assertThisInitialized(_this),
66
- i = 0;
67
-
68
- _this.replacedLinks = [];
17
+ super(...args);
18
+ _defineProperty(this, "state", {});
19
+ _defineProperty(this, "extractReplaceLinksKeys", content => {
20
+ var _self = this,
21
+ i = 0;
22
+ this.replacedLinks = [];
69
23
  content.replace(/(<a[\s]+([^>]+)>((?:.(?!\<\/a\>))*.)<\/a>)/g, function () {
70
- var item = Array.prototype.slice.call(arguments, 1, 4);
71
- item.key = '[' + '@'.repeat(item[2].length - 1) + '=' + i++ + ']';
72
-
24
+ const item = Array.prototype.slice.call(arguments, 1, 4);
25
+ item.key = "[" + "@".repeat(item[2].length - 1) + "=" + i++ + "]";
73
26
  _self.replacedLinks.push(item);
74
-
75
27
  content = content.replace(item[0], item.key);
76
28
  });
77
29
  return content;
78
30
  });
79
-
80
- _defineProperty(_assertThisInitialized(_this), "restoreReplacedLinks", function (content) {
81
- _this.replacedLinks.forEach(function (item) {
31
+ _defineProperty(this, "restoreReplacedLinks", content => {
32
+ this.replacedLinks.forEach(item => {
82
33
  content = content.replace(item.key, item[0]);
83
34
  });
84
-
85
- return _this.createMarkup(content);
35
+ return this.createMarkup(content);
86
36
  });
87
-
88
- _defineProperty(_assertThisInitialized(_this), "innerText", function (node) {
89
- var div = document.createElement('div');
90
- var contentKey = 'innerText' in window.HTMLElement.prototype ? 'innerText' : 'textContent';
91
- var content = node.innerHTML.replace(/\r\n|\r|\n/g, ' ');
92
- div.innerHTML = _this.extractReplaceLinksKeys(content);
93
- var text = div[contentKey];
94
- var test = document.createElement('div');
95
- test.innerHTML = 'foo<br/>bar';
96
-
97
- if (test[contentKey].replace(/\r\n|\r/g, '\n') !== 'foo\nbar') {
98
- div.innerHTML = div.innerHTML.replace(/<br.*?[\/]?>/gi, '\n');
37
+ _defineProperty(this, "innerText", node => {
38
+ const div = document.createElement("div");
39
+ const contentKey = "innerText" in window.HTMLElement.prototype ? "innerText" : "textContent";
40
+ const content = node.innerHTML.replace(/\r\n|\r|\n/g, " ");
41
+ div.innerHTML = this.extractReplaceLinksKeys(content);
42
+ let text = div[contentKey];
43
+ const test = document.createElement("div");
44
+ test.innerHTML = "foo<br/>bar";
45
+ if (test[contentKey].replace(/\r\n|\r/g, "\n") !== "foo\nbar") {
46
+ div.innerHTML = div.innerHTML.replace(/<br.*?[\/]?>/gi, "\n");
99
47
  text = div[contentKey];
100
48
  }
101
-
102
49
  return text;
103
50
  });
104
-
105
- _defineProperty(_assertThisInitialized(_this), "onResize", function () {
106
- _this.calcTargetWidth();
51
+ _defineProperty(this, "onResize", () => {
52
+ this.calcTargetWidth();
107
53
  });
108
-
109
- _defineProperty(_assertThisInitialized(_this), "onTruncate", function (didTruncate) {
110
- var onTruncate = _this.props.onTruncate;
111
-
112
- if (typeof onTruncate === 'function') {
113
- _this.timeout = window.requestAnimationFrame(function () {
54
+ _defineProperty(this, "onTruncate", didTruncate => {
55
+ const {
56
+ onTruncate
57
+ } = this.props;
58
+ if (typeof onTruncate === "function") {
59
+ this.timeout = window.requestAnimationFrame(() => {
114
60
  onTruncate(didTruncate);
115
61
  });
116
62
  }
117
63
  });
64
+ _defineProperty(this, "calcTargetWidth", callback => {
65
+ const {
66
+ elements: {
67
+ target
68
+ },
69
+ calcTargetWidth,
70
+ canvasContext,
71
+ props: {
72
+ width
73
+ }
74
+ } = this;
118
75
 
119
- _defineProperty(_assertThisInitialized(_this), "calcTargetWidth", function (callback) {
120
- var _assertThisInitialize = _assertThisInitialized(_this),
121
- target = _assertThisInitialize.elements.target,
122
- calcTargetWidth = _assertThisInitialize.calcTargetWidth,
123
- canvasContext = _assertThisInitialize.canvasContext,
124
- width = _assertThisInitialize.props.width; // Calculation is no longer relevant, since node has been removed
125
-
126
-
76
+ // Calculation is no longer relevant, since node has been removed
127
77
  if (!target) {
128
78
  return;
129
79
  }
80
+ const targetWidth = width ||
81
+ // Floor the result to deal with browser subpixel precision
82
+ Math.floor(target.parentNode.getBoundingClientRect().width);
130
83
 
131
- var targetWidth = width || // Floor the result to deal with browser subpixel precision
132
- Math.floor(target.parentNode.getBoundingClientRect().width); // Delay calculation until parent node is inserted to the document
84
+ // Delay calculation until parent node is inserted to the document
133
85
  // Mounting order in React is ChildComponent, ParentComponent
134
-
135
86
  if (!targetWidth) {
136
- return window.requestAnimationFrame(function () {
137
- return calcTargetWidth(callback);
138
- });
87
+ return window.requestAnimationFrame(() => calcTargetWidth(callback));
139
88
  }
140
-
141
- var style = window.getComputedStyle(target);
142
- var font = [style['font-weight'], style['font-style'], style['font-size'], style['font-family']].join(' ');
89
+ const style = window.getComputedStyle(target);
90
+ const font = [style["font-weight"], style["font-style"], style["font-size"], style["font-family"]].join(" ");
143
91
  canvasContext.font = font;
144
-
145
- _this.setState({
146
- targetWidth: targetWidth
92
+ this.setState({
93
+ targetWidth
147
94
  }, callback);
148
95
  });
149
-
150
- _defineProperty(_assertThisInitialized(_this), "measureWidth", function (text) {
151
- return _this.canvasContext.measureText(text).width;
96
+ _defineProperty(this, "measureWidth", text => {
97
+ return this.canvasContext.measureText(text).width;
152
98
  });
153
-
154
- _defineProperty(_assertThisInitialized(_this), "ellipsisWidth", function (node) {
99
+ _defineProperty(this, "ellipsisWidth", node => {
155
100
  return node.offsetWidth;
156
101
  });
157
-
158
- _defineProperty(_assertThisInitialized(_this), "trimRight", function (text) {
159
- return text.replace(/\s+$/, '');
102
+ _defineProperty(this, "trimRight", text => {
103
+ return text.replace(/\s+$/, "");
160
104
  });
161
-
162
- _defineProperty(_assertThisInitialized(_this), "createMarkup", function (str) {
105
+ _defineProperty(this, "createMarkup", str => {
163
106
  return /*#__PURE__*/_react.default.createElement("span", {
164
107
  dangerouslySetInnerHTML: {
165
108
  __html: str
166
109
  }
167
110
  });
168
111
  });
169
-
170
- _defineProperty(_assertThisInitialized(_this), "getLines", function () {
171
- var _assertThisInitialize2 = _assertThisInitialized(_this),
172
- elements = _assertThisInitialize2.elements,
173
- _assertThisInitialize3 = _assertThisInitialize2.props,
174
- numLines = _assertThisInitialize3.lines,
175
- ellipsis = _assertThisInitialize3.ellipsis,
176
- trimWhitespace = _assertThisInitialize3.trimWhitespace,
177
- targetWidth = _assertThisInitialize2.state.targetWidth,
178
- innerText = _assertThisInitialize2.innerText,
179
- measureWidth = _assertThisInitialize2.measureWidth,
180
- onTruncate = _assertThisInitialize2.onTruncate,
181
- trimRight = _assertThisInitialize2.trimRight,
182
- renderLine = _assertThisInitialize2.renderLine,
183
- restoreReplacedLinks = _assertThisInitialize2.restoreReplacedLinks;
184
-
185
- var lines = [];
186
- var text = innerText(elements.text);
187
- var textLines = text.split('\n').map(function (line) {
188
- return line.split(' ');
189
- });
190
- var didTruncate = true;
191
-
192
- var ellipsisWidth = _this.ellipsisWidth(_this.elements.ellipsis);
193
-
194
- for (var line = 1; line <= numLines; line++) {
195
- var textWords = textLines[0]; // Handle newline
196
-
112
+ _defineProperty(this, "getLines", () => {
113
+ const {
114
+ elements,
115
+ props: {
116
+ lines: numLines,
117
+ ellipsis,
118
+ trimWhitespace
119
+ },
120
+ state: {
121
+ targetWidth
122
+ },
123
+ innerText,
124
+ measureWidth,
125
+ onTruncate,
126
+ trimRight,
127
+ renderLine,
128
+ restoreReplacedLinks
129
+ } = this;
130
+ const lines = [];
131
+ const text = innerText(elements.text);
132
+ const textLines = text.split("\n").map(line => line.split(" "));
133
+ let didTruncate = true;
134
+ const ellipsisWidth = this.ellipsisWidth(this.elements.ellipsis);
135
+ for (let line = 1; line <= numLines; line++) {
136
+ const textWords = textLines[0];
137
+
138
+ // Handle newline
197
139
  if (textWords.length === 0) {
198
140
  lines.push();
199
141
  textLines.shift();
200
142
  line--;
201
143
  continue;
202
144
  }
203
-
204
- var resultLine = textWords.join(' ');
205
-
145
+ let resultLine = textWords.join(" ");
206
146
  if (measureWidth(resultLine) <= targetWidth) {
207
147
  if (textLines.length === 1) {
208
148
  // Line is end of text and fits without truncating
@@ -212,90 +152,74 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
212
152
  break;
213
153
  }
214
154
  }
215
-
216
155
  if (line === numLines) {
217
156
  // Binary search determining the longest possible line inluding truncate string
218
- var textRest = textWords.join(' ');
219
- var lower = 0;
220
- var upper = textRest.length - 1;
221
-
157
+ const textRest = textWords.join(" ");
158
+ let lower = 0;
159
+ let upper = textRest.length - 1;
222
160
  while (lower <= upper) {
223
- var middle = Math.floor((lower + upper) / 2);
224
- var testLine = textRest.slice(0, middle + 1);
225
-
161
+ const middle = Math.floor((lower + upper) / 2);
162
+ const testLine = textRest.slice(0, middle + 1);
226
163
  if (measureWidth(testLine) + ellipsisWidth <= targetWidth) {
227
164
  lower = middle + 1;
228
165
  } else {
229
166
  upper = middle - 1;
230
167
  }
231
168
  }
232
-
233
- var lastLineText = textRest.slice(0, lower);
234
-
169
+ let lastLineText = textRest.slice(0, lower);
235
170
  if (trimWhitespace) {
236
- lastLineText = trimRight(lastLineText); // Remove blank lines from the end of text
171
+ lastLineText = trimRight(lastLineText);
237
172
 
173
+ // Remove blank lines from the end of text
238
174
  while (!lastLineText.length && lines.length) {
239
- var prevLine = lines.pop();
175
+ const prevLine = lines.pop();
240
176
  lastLineText = trimRight(prevLine);
241
177
  }
242
178
  }
243
-
244
- if (lastLineText.substr(lastLineText.length - 2) === '][') {
179
+ if (lastLineText.substr(lastLineText.length - 2) === "][") {
245
180
  lastLineText = lastLineText.substring(0, lastLineText.length - 1);
246
181
  }
247
-
248
- ;
249
- lastLineText = lastLineText.replace(/\[@+$/, '');
182
+ lastLineText = lastLineText.replace(/\[@+$/, "");
250
183
  lastLineText = restoreReplacedLinks(lastLineText);
251
184
  resultLine = /*#__PURE__*/_react.default.createElement("span", null, lastLineText, ellipsis);
252
185
  } else {
253
186
  // Binary search determining when the line breaks
254
- var _lower = 0;
255
-
256
- var _upper = textWords.length - 1;
257
-
258
- while (_lower <= _upper) {
259
- var _middle = Math.floor((_lower + _upper) / 2);
260
-
261
- var _testLine = textWords.slice(0, _middle + 1).join(' ');
262
-
263
- if (measureWidth(_testLine) <= targetWidth) {
264
- _lower = _middle + 1;
187
+ let lower = 0;
188
+ let upper = textWords.length - 1;
189
+ while (lower <= upper) {
190
+ const middle = Math.floor((lower + upper) / 2);
191
+ const testLine = textWords.slice(0, middle + 1).join(" ");
192
+ if (measureWidth(testLine) <= targetWidth) {
193
+ lower = middle + 1;
265
194
  } else {
266
- _upper = _middle - 1;
195
+ upper = middle - 1;
267
196
  }
268
- } // The first word of this line is too long to fit it
269
-
197
+ }
270
198
 
271
- if (_lower === 0) {
199
+ // The first word of this line is too long to fit it
200
+ if (lower === 0) {
272
201
  // Jump to processing of last line
273
202
  line = numLines - 1;
274
203
  continue;
275
204
  }
276
-
277
- resultLine = textWords.slice(0, _lower).join(' ');
205
+ resultLine = textWords.slice(0, lower).join(" ");
278
206
  resultLine = restoreReplacedLinks(resultLine);
279
- textLines[0].splice(0, _lower);
207
+ textLines[0].splice(0, lower);
280
208
  }
281
-
282
209
  lines.push(resultLine);
283
210
  }
284
-
285
211
  onTruncate(didTruncate);
286
212
  return lines.map(renderLine);
287
213
  });
288
-
289
- _defineProperty(_assertThisInitialized(_this), "renderLine", function (line, i, arr) {
214
+ _defineProperty(this, "renderLine", (line, i, arr) => {
290
215
  if (i === arr.length - 1) {
291
216
  return /*#__PURE__*/_react.default.createElement("span", {
292
217
  key: i
293
218
  }, line);
294
219
  } else {
295
- var br = /*#__PURE__*/_react.default.createElement("br", {
296
- key: i + 'br'
220
+ const br = /*#__PURE__*/_react.default.createElement("br", {
221
+ key: i + "br"
297
222
  });
298
-
299
223
  if (line) {
300
224
  return [/*#__PURE__*/_react.default.createElement("span", {
301
225
  key: i
@@ -305,115 +229,111 @@ var Truncate = /*#__PURE__*/function (_React$Component) {
305
229
  }
306
230
  }
307
231
  });
308
-
309
- _defineProperty(_assertThisInitialized(_this), "styles", {
232
+ _defineProperty(this, "styles", {
310
233
  ellipsis: {
311
- position: 'fixed',
312
- visibility: 'hidden',
234
+ position: "fixed",
235
+ visibility: "hidden",
313
236
  top: 0,
314
237
  left: 0
315
238
  }
316
239
  });
317
-
318
- _this.elements = {};
319
- _this.replacedLinks = [];
320
- return _this;
240
+ this.elements = {};
241
+ this.replacedLinks = [];
321
242
  }
322
-
323
- _createClass(Truncate, [{
324
- key: "componentDidMount",
325
- value: function componentDidMount() {
326
- var text = this.elements.text,
327
- calcTargetWidth = this.calcTargetWidth,
328
- onResize = this.onResize;
329
- var canvas = document.createElement('canvas');
330
- this.canvasContext = canvas.getContext('2d');
331
- calcTargetWidth(function () {
332
- // Node not needed in document tree to read its content
333
- if (text && text.parentNode) {
334
- text.parentNode.removeChild(text);
335
- }
336
- });
337
- window.addEventListener('resize', onResize);
338
- }
339
- }, {
340
- key: "componentDidUpdate",
341
- value: function componentDidUpdate(prevProps) {
342
- // Render was based on outdated refs and needs to be rerun
343
- if (this.props.children !== prevProps.children) {
344
- this.forceUpdate();
345
- } // If the width prop has changed, recalculate size of contents
346
-
347
-
348
- if (this.props.width !== prevProps.width) {
349
- this.calcTargetWidth();
243
+ componentDidMount() {
244
+ const {
245
+ elements: {
246
+ text
247
+ },
248
+ calcTargetWidth,
249
+ onResize
250
+ } = this;
251
+ const canvas = document.createElement("canvas");
252
+ this.canvasContext = canvas.getContext("2d");
253
+ calcTargetWidth(() => {
254
+ // Node not needed in document tree to read its content
255
+ if (text && text.parentNode) {
256
+ text.parentNode.removeChild(text);
350
257
  }
258
+ });
259
+ window.addEventListener("resize", onResize);
260
+ }
261
+ componentDidUpdate(prevProps) {
262
+ // Render was based on outdated refs and needs to be rerun
263
+ if (this.props.children !== prevProps.children) {
264
+ this.forceUpdate();
351
265
  }
352
- }, {
353
- key: "componentWillUnmount",
354
- value: function componentWillUnmount() {
355
- var ellipsis = this.elements.ellipsis,
356
- onResize = this.onResize,
357
- timeout = this.timeout;
358
266
 
359
- if (ellipsis.parentNode) {
360
- ellipsis.parentNode.removeChild(ellipsis);
361
- }
362
-
363
- window.removeEventListener('resize', onResize);
364
- window.cancelAnimationFrame(timeout);
267
+ // If the width prop has changed, recalculate size of contents
268
+ if (this.props.width !== prevProps.width) {
269
+ this.calcTargetWidth();
365
270
  }
366
- }, {
367
- key: "render",
368
- value: function render() {
369
- var _this2 = this;
370
-
371
- var target = this.elements.target,
372
- _this$props = this.props,
373
- children = _this$props.children,
374
- ellipsis = _this$props.ellipsis,
375
- lines = _this$props.lines,
376
- spanProps = _objectWithoutProperties(_this$props, _excluded),
377
- targetWidth = this.state.targetWidth,
378
- getLines = this.getLines,
379
- onTruncate = this.onTruncate;
380
-
381
- var text;
382
- var mounted = !!(target && targetWidth);
383
-
384
- if (typeof window !== 'undefined' && mounted) {
385
- if (lines > 0) {
386
- text = getLines();
387
- } else {
388
- text = children;
389
- onTruncate(false);
390
- }
271
+ }
272
+ componentWillUnmount() {
273
+ const {
274
+ elements: {
275
+ ellipsis
276
+ },
277
+ onResize,
278
+ timeout
279
+ } = this;
280
+ if (ellipsis.parentNode) {
281
+ ellipsis.parentNode.removeChild(ellipsis);
282
+ }
283
+ window.removeEventListener("resize", onResize);
284
+ window.cancelAnimationFrame(timeout);
285
+ }
286
+ render() {
287
+ const {
288
+ elements: {
289
+ target
290
+ },
291
+ props: {
292
+ children,
293
+ ellipsis,
294
+ lines,
295
+ ...spanProps
296
+ },
297
+ state: {
298
+ targetWidth
299
+ },
300
+ getLines,
301
+ onTruncate
302
+ } = this;
303
+ let text;
304
+ const mounted = !!(target && targetWidth);
305
+ if (typeof window !== "undefined" && mounted) {
306
+ if (lines > 0) {
307
+ text = getLines();
308
+ } else {
309
+ text = children;
310
+ onTruncate(false);
391
311
  }
392
-
393
- delete spanProps.onTruncate;
394
- delete spanProps.trimWhitespace;
395
- return /*#__PURE__*/_react.default.createElement("span", _extends({}, spanProps, {
396
- ref: function ref(targetEl) {
397
- _this2.elements.target = targetEl;
398
- }
399
- }), /*#__PURE__*/_react.default.createElement("span", null, text), /*#__PURE__*/_react.default.createElement("span", {
400
- ref: function ref(textEl) {
401
- _this2.elements.text = textEl;
402
- }
403
- }, children), /*#__PURE__*/_react.default.createElement("span", {
404
- ref: function ref(ellipsisEl) {
405
- _this2.elements.ellipsis = ellipsisEl;
406
- },
407
- style: this.styles.ellipsis
408
- }, ellipsis));
409
312
  }
410
- }]);
411
-
412
- return Truncate;
413
- }(_react.default.Component);
414
-
313
+ delete spanProps.onTruncate;
314
+ delete spanProps.trimWhitespace;
315
+ return /*#__PURE__*/_react.default.createElement("span", _extends({}, spanProps, {
316
+ ref: targetEl => {
317
+ this.elements.target = targetEl;
318
+ }
319
+ }), /*#__PURE__*/_react.default.createElement("span", {
320
+ style: {
321
+ display: "block",
322
+ maxWidth: "".concat(spanProps.width, "px")
323
+ }
324
+ }, text), /*#__PURE__*/_react.default.createElement("span", {
325
+ ref: textEl => {
326
+ this.elements.text = textEl;
327
+ }
328
+ }, children), /*#__PURE__*/_react.default.createElement("span", {
329
+ ref: ellipsisEl => {
330
+ this.elements.ellipsis = ellipsisEl;
331
+ },
332
+ style: this.styles.ellipsis
333
+ }, ellipsis));
334
+ }
335
+ }
415
336
  exports.default = Truncate;
416
-
417
337
  _defineProperty(Truncate, "propTypes", {
418
338
  children: _propTypes.default.node,
419
339
  ellipsis: _propTypes.default.node,
@@ -422,13 +342,10 @@ _defineProperty(Truncate, "propTypes", {
422
342
  width: _propTypes.default.number,
423
343
  onTruncate: _propTypes.default.func
424
344
  });
425
-
426
345
  _defineProperty(Truncate, "defaultProps", {
427
- children: '',
428
- ellipsis: '',
346
+ children: "",
347
+ ellipsis: "",
429
348
  lines: 1,
430
349
  trimWhitespace: false,
431
350
  width: 0
432
- });
433
-
434
- ;
351
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-show-more-text",
3
- "version": "1.6.1",
3
+ "version": "1.7.0",
4
4
  "description": "The text surrounded by the component will be truncated. Anything surrounded by the component could be evaluated as text. The component react-show-more-text/ShowMoreText is fork of react-show-more/ShowMore, applied improvements, works with React 16.x.x, added onClick event.",
5
5
  "main": "lib/ShowMoreText.js",
6
6
  "files": [
@@ -83,7 +83,6 @@
83
83
  "react-dom": "^16.14.0"
84
84
  },
85
85
  "dependencies": {
86
- "@babel/polyfill": "^7.12.1",
87
86
  "prop-types": "^15.7.2"
88
87
  },
89
88
  "browserslist": "> 0.25%, not dead"