diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..72ac191 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,38 @@ +## Changelog + +- 0.14.0 Fix console warnings +- 0.14.0 Added new event hook: `onToggled` +- 0.13.1 Fix react warning of `maxCalculateTimes` +- 0.13.0 Shows all of the text when `line` is falsely, add `maxCalculateTimes` to prevent infinite loop +- 0.12.1 Update peerDependencies to support React 16 +- 0.12.0 Added new prop: `textElement` for customize text render +- 0.11.2 Fix truncate incorrect in small size of width +- 0.11.0 Added two event hooks: `onTruncated` and `onCalculated` +- 0.10.4 Support inline style +- 0.10.3 Downgrade peerDependencies to `^15.4.1` +- 0.10.2 Fix truncated wrong when font property changed +- 0.10.1 Fix unknown prop `element` +- 0.10.0 Allow pass `element` to specify root element type +- 0.9.3 Fix SSR window not found (removed raf polyfill) +- 0.9.2 Fix styles prop is not working when the text is not truncated +- 0.9.1 Fix async this.forceUpdate() issue +- 0.9.0 Fix infinity loop bug, upgrade react to 15.5.4 and support yarn +- 0.8.3 IE 11 compatibility +- 0.8.2 Fix wrong truncating when a container has long words without spaces +- 0.8.1 Fix `textTruncateChild` bug +- 0.8.0 Drop `showTitle`, put `textTruncateChild` in same line if `tagName` is `A` or `SPAN` +- 0.7.2 Server render will display all text +- 0.7.1 Add new prop `containerClassName` +- 0.7.0 Fix infinite update bug, drop raf +- 0.6.2 Fix infinite loop bug +- 0.6.1 Fix lib issue for browser +- 0.6.0 Support React 15 +- 0.5.2 Fix server render bug +- 0.5.1 Hide `textTruncateChild` when nothing truncated +- 0.5.0 Add request animation frame +- 0.4.0 New property `textTruncateChild` for show more or others +- 0.3.7 Support CommonJS and UMD module loader +- 0.3.5 Fix window resize issue +- 0.3.4 supports Babel6 +- 0.2.0 supports React 0.14 +- 0.1.5 supports React 0.13.3 and below diff --git a/README.md b/README.md index 47521ce..9df20cc 100644 --- a/README.md +++ b/README.md @@ -35,44 +35,3 @@ var TextTruncate = require('react-text-truncate'); // CommonJS or UMD 2. Will TextTruncate support IE10 or below? No! IE 10 and below are dead according to [Microsoft post](https://www.microsoft.com/en-us/WindowsForBusiness/End-of-IE-support). - -3. Uncaught ReferenceError: requestAnimationFrame is not defined - - Please polyfill `requestAnimationFrame` before you called `` - -## Changelog - -* 0.13.1 Fix react warning of `maxCalculateTimes` -* 0.13.0 Shows all of the text when `line` is falsely, add `maxCalculateTimes` to prevent infinite loop -* 0.12.1 Update peerDependencies to support React 16 -* 0.12.0 Added new prop: `textElement` for customize text render -* 0.11.2 Fix truncate incorrect in small size of width -* 0.11.0 Added two event hooks: `onTruncated` and `onCalculated` -* 0.10.4 Support inline style -* 0.10.3 Downgrade peerDependencies to `^15.4.1` -* 0.10.2 Fix truncated wrong when font property changed -* 0.10.1 Fix unknown prop `element` -* 0.10.0 Allow pass `element` to specify root element type -* 0.9.3 Fix SSR window not found (removed raf polyfill) -* 0.9.2 Fix styles prop is not working when the text is not truncated -* 0.9.1 Fix async this.forceUpdate() issue -* 0.9.0 Fix infinity loop bug, upgrade react to 15.5.4 and support yarn -* 0.8.3 IE 11 compatibility -* 0.8.2 Fix wrong truncating when a container has long words without spaces -* 0.8.1 Fix `textTruncateChild` bug -* 0.8.0 Drop `showTitle`, put `textTruncateChild` in same line if `tagName` is `A` or `SPAN` -* 0.7.2 Server render will display all text -* 0.7.1 Add new prop `containerClassName` -* 0.7.0 Fix infinite update bug, drop raf -* 0.6.2 Fix infinite loop bug -* 0.6.1 Fix lib issue for browser -* 0.6.0 Support React 15 -* 0.5.2 Fix server render bug -* 0.5.1 Hide `textTruncateChild` when nothing truncated -* 0.5.0 Add request animation frame -* 0.4.0 New property `textTruncateChild` for show more or others -* 0.3.7 Support CommonJS and UMD module loader -* 0.3.5 Fix window resize issue -* 0.3.4 supports Babel6 -* 0.2.0 supports React 0.14 -* 0.1.5 supports React 0.13.3 and below diff --git a/lib/index.js b/lib/index.js index 93b1d63..52251c1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -106,6 +106,10 @@ window.cancelAnimationFrame(_this.rafId); } _this.rafId = window.requestAnimationFrame(_this.update.bind(_this)); + }, _this.onToggled = function (truncated) { + typeof _this.props.onToggled === 'function' && setTimeout(function () { + return _this.props.onToggled(truncated); + }, 0); }, _this.onTruncated = function () { typeof _this.props.onTruncated === 'function' && setTimeout(function () { return _this.props.onTruncated(); @@ -158,12 +162,13 @@ line = _props.line, onCalculated = _props.onCalculated, onTruncated = _props.onTruncated, + onToggled = _props.onToggled, text = _props.text, textElement = _props.textElement, textTruncateChild = _props.textTruncateChild, truncateText = _props.truncateText, maxCalculateTimes = _props.maxCalculateTimes, - props = _objectWithoutProperties(_props, ['containerClassName', 'element', 'line', 'onCalculated', 'onTruncated', 'text', 'textElement', 'textTruncateChild', 'truncateText', 'maxCalculateTimes']); + props = _objectWithoutProperties(_props, ['containerClassName', 'element', 'line', 'onCalculated', 'onTruncated', 'onToggled', 'text', 'textElement', 'textTruncateChild', 'truncateText', 'maxCalculateTimes']); var scopeWidth = this.scope.getBoundingClientRect().width; @@ -174,6 +179,7 @@ // return if all of text can be displayed if (scopeWidth >= this.measureWidth(text)) { + this.onToggled(false); return (0, _react.createElement)(textElement, props, text); } @@ -261,10 +267,12 @@ } if (startPos === maxTextLength) { + this.onToggled(false); return (0, _react.createElement)(textElement, props, text); } this.onTruncated(); + this.onToggled(true); return _react2.default.createElement( 'div', props, @@ -286,11 +294,12 @@ line = _props2.line, onCalculated = _props2.onCalculated, onTruncated = _props2.onTruncated, + onToggled = _props2.onToggled, textElement = _props2.textElement, textTruncateChild = _props2.textTruncateChild, truncateText = _props2.truncateText, maxCalculateTimes = _props2.maxCalculateTimes, - props = _objectWithoutProperties(_props2, ['element', 'text', 'style', 'containerClassName', 'line', 'onCalculated', 'onTruncated', 'textElement', 'textTruncateChild', 'truncateText', 'maxCalculateTimes']); + props = _objectWithoutProperties(_props2, ['element', 'text', 'style', 'containerClassName', 'line', 'onCalculated', 'onTruncated', 'onToggled', 'textElement', 'textTruncateChild', 'truncateText', 'maxCalculateTimes']); var fontWeight = style.fontWeight, fontStyle = style.fontStyle, @@ -321,6 +330,7 @@ line: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.bool]), onCalculated: _propTypes2.default.func, onTruncated: _propTypes2.default.func, + onToggled: _propTypes2.default.func, text: _propTypes2.default.string, textElement: _propTypes2.default.node, textTruncateChild: _propTypes2.default.node, diff --git a/package.json b/package.json index e07effc..8e398a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-text-truncate", - "version": "0.13.1", + "version": "0.14.1", "description": "Truncate text for React.js", "main": "lib/index.js", "scripts": { diff --git a/src/App.js b/src/App.js index 805be0b..1a18158 100644 --- a/src/App.js +++ b/src/App.js @@ -1,18 +1,19 @@ -import React, {Component} from 'react'; -import TextTruncate from './TextTruncate'; +import React, { Component } from "react"; +import TextTruncate from "./TextTruncate"; export class App extends Component { constructor(props) { super(props); this.state = { - text: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.', + text: + "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", line: 2, - truncateText: '…', + truncateText: "…", appendTextTruncateChild: true }; } - handleChange = (e) => { + handleChange = e => { this.setState({ line: this.refs.line.value << 0, text: this.refs.text.value, @@ -21,117 +22,193 @@ export class App extends Component { }); }; - onToggle = (e) => { + onToggle = e => { var display = this.refs.invisibleBlock.style.display; - this.refs.invisibleBlock.style.display = display === 'none' ? 'block' : 'none'; + this.refs.invisibleBlock.style.display = + display === "none" ? "block" : "none"; this.refs.invisibleTextTruncate.update(); }; render() { - const { - appendTextTruncateChild, - ...props - } = this.state; + const { appendTextTruncateChild, ...props } = this.state; if (appendTextTruncateChild) { - props.textTruncateChild = ( - Read On - ); + props.textTruncateChild = Read On; } return ( -
-
-
- - +
+
+
+ +
-
- - +
+ +