@konforti/react-truncate
Version:
Long text truncation
2 lines (1 loc) • 3.1 kB
JavaScript
import React from"react";import truncate from"@konforti/truncate";var _extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();function _asyncToGenerator(e){return function(){var t=e.apply(this,arguments);return new Promise(function(e,n){return function r(o,i){try{var a=t[o](i),s=a.value}catch(e){return void n(e)}if(!a.done)return Promise.resolve(s).then(function(e){r("next",e)},function(e){r("throw",e)});e(s)}("next")})}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var ReactTruncate=function(e){function t(){var e,n,r;_classCallCheck(this,t);for(var o=arguments.length,i=Array(o),a=0;a<o;a++)i[a]=arguments[a];return n=r=_possibleConstructorReturn(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(i))),r.onResize=function(){r.ticking=r.ticking||requestAnimationFrame(r.handleResize)},r.handleResize=function(){r.ticking=!1,r.handleLongText()},r.ticking=!1,r.longTextWrapper=null,_possibleConstructorReturn(r,n)}return _inherits(t,React.Component),_createClass(t,[{key:"componentDidMount",value:function(){this.handleLongText(),this.longTextWrapper&&window.addEventListener("resize",this.onResize,!1)}},{key:"componentDidUpdate",value:function(e){e.children!==this.props.children&&this.handleLongText()}},{key:"componentWillUnmount",value:function(){window.removeEventListener("resize",this.onResize,!1)}},{key:"handleLongText",value:function(){var e=_asyncToGenerator(regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:this.longTextWrapper&&(t=truncate(this.longTextWrapper,this.props.children,this.props.lines,this.props.truncFrom,this.props.ellipsis,this.props.wrapperOffset),this.props.onTruncate&&this.props.onTruncate(t));case 1:case"end":return e.stop()}},e,this)}));return function(){return e.apply(this,arguments)}}()},{key:"render",value:function(){var e=this,t=_extends({wordBreak:"break-all",whiteSpace:"normal",wordWrap:"break-word",display:"inline-block"},this.props.style);return React.createElement("span",{className:"truncate-wrapper",style:t,ref:function(t){e.longTextWrapper=t}},this.props.children)}}]),t}();export default ReactTruncate;