diff --git a/dist/react-input-autosize.es.js b/dist/react-input-autosize.es.js index 505efee..cb92774 100644 --- a/dist/react-input-autosize.es.js +++ b/dist/react-input-autosize.es.js @@ -281,18 +281,12 @@ var AutosizeInput = function (_Component) { this.copyInputStyles(); this.updateInputWidth(); } - }, { - key: 'componentWillReceiveProps', - value: function componentWillReceiveProps(nextProps) { - var id = nextProps.id; - - if (id !== this.props.id) { - this.setState({ inputId: id || generateId() }); - } - } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { + if (prevProps.id !== this.props.id) { + this.setState({ inputId: this.props.id || generateId() }); + } if (prevState.inputWidth !== this.state.inputWidth) { if (typeof this.props.onAutosize === 'function') { this.props.onAutosize(this.state.inputWidth); diff --git a/dist/react-input-autosize.js b/dist/react-input-autosize.js index b66e2d2..99f9dcd 100644 --- a/dist/react-input-autosize.js +++ b/dist/react-input-autosize.js @@ -287,18 +287,12 @@ var AutosizeInput = function (_Component) { this.copyInputStyles(); this.updateInputWidth(); } - }, { - key: 'componentWillReceiveProps', - value: function componentWillReceiveProps(nextProps) { - var id = nextProps.id; - - if (id !== this.props.id) { - this.setState({ inputId: id || generateId() }); - } - } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { + if (prevProps.id !== this.props.id) { + this.setState({ inputId: this.props.id || generateId() }); + } if (prevState.inputWidth !== this.state.inputWidth) { if (typeof this.props.onAutosize === 'function') { this.props.onAutosize(this.state.inputWidth); diff --git a/dist/react-input-autosize.min.js b/dist/react-input-autosize.min.js index 0d904aa..c611a00 100644 --- a/dist/react-input-autosize.min.js +++ b/dist/react-input-autosize.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],e):t.AutosizeInput=e(t.React,t.PropTypes)}(this,function(t,e){"use strict";var n="default"in t?t.default:t;e=e&&e.hasOwnProperty("default")?e.default:e;!function(){function t(t){this.value=t}function e(e){function n(o,r){try{var s=e[o](r),u=s.value;u instanceof t?Promise.resolve(u.value).then(function(t){n("next",t)},function(t){n("throw",t)}):i(s.done?"return":"normal",s.value)}catch(t){i("throw",t)}}function i(t,e){switch(t){case"return":o.resolve({value:e,done:!0});break;case"throw":o.reject(e);break;default:o.resolve({value:e,done:!1})}(o=o.next)?n(o.key,o.arg):r=null}var o,r;this._invoke=function(t,e){return new Promise(function(i,s){var u={key:t,arg:e,resolve:i,reject:s,next:null};r?r=r.next=u:(o=r=u,n(t,e))})},"function"!=typeof e.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(e.prototype[Symbol.asyncIterator]=function(){return this}),e.prototype.next=function(t){return this._invoke("next",t)},e.prototype.throw=function(t){return this._invoke("throw",t)},e.prototype.return=function(t){return this._invoke("return",t)}}();var i=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},o=function(){function t(t,e){for(var n=0;n=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n},p=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},l={position:"absolute",top:0,left:0,visibility:"hidden",height:0,overflow:"scroll",whiteSpace:"pre"},a=["extraWidth","injectStyles","inputClassName","inputRef","inputStyle","minWidth","onAutosize","placeholderIsMinWidth"],c=function(t){return a.forEach(function(e){return delete t[e]}),t},h=function(t,e){e.style.fontSize=t.fontSize,e.style.fontFamily=t.fontFamily,e.style.fontWeight=t.fontWeight,e.style.fontStyle=t.fontStyle,e.style.letterSpacing=t.letterSpacing,e.style.textTransform=t.textTransform},f=!("undefined"==typeof window||!window.navigator)&&/MSIE |Trident\/|Edge\//.test(window.navigator.userAgent),d=function(){return f?"_"+Math.random().toString(36).substr(2,12):void 0},y=function(t){function e(t){i(this,e);var n=p(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return n.inputRef=function(t){n.input=t,"function"==typeof n.props.inputRef&&n.props.inputRef(t)},n.placeHolderSizerRef=function(t){n.placeHolderSizer=t},n.sizerRef=function(t){n.sizer=t},n.state={inputWidth:t.minWidth,inputId:t.id||d()},n}return s(e,t),o(e,[{key:"componentDidMount",value:function(){this.mounted=!0,this.copyInputStyles(),this.updateInputWidth()}},{key:"componentWillReceiveProps",value:function(t){var e=t.id;e!==this.props.id&&this.setState({inputId:e||d()})}},{key:"componentDidUpdate",value:function(t,e){e.inputWidth!==this.state.inputWidth&&"function"==typeof this.props.onAutosize&&this.props.onAutosize(this.state.inputWidth),this.updateInputWidth()}},{key:"componentWillUnmount",value:function(){this.mounted=!1}},{key:"copyInputStyles",value:function(){if(this.mounted&&window.getComputedStyle){var t=this.input&&window.getComputedStyle(this.input);t&&(h(t,this.sizer),this.placeHolderSizer&&h(t,this.placeHolderSizer))}}},{key:"updateInputWidth",value:function(){if(this.mounted&&this.sizer&&void 0!==this.sizer.scrollWidth){var t=void 0;t=this.props.placeholder&&(!this.props.value||this.props.value&&this.props.placeholderIsMinWidth)?Math.max(this.sizer.scrollWidth,this.placeHolderSizer.scrollWidth)+2:this.sizer.scrollWidth+2,(t+="number"===this.props.type&&void 0===this.props.extraWidth?16:parseInt(this.props.extraWidth)||0)=0||Object.prototype.hasOwnProperty.call(t,i)&&(n[i]=t[i]);return n}(this.props,[]);return function(t){p.forEach(function(e){return delete t[e]})}(o),o.className=this.props.inputClassName,o.id=this.state.inputId,o.style=i,n.createElement("div",{className:this.props.className,style:e},this.renderStyles(),n.createElement("input",r({},o,{ref:this.inputRef})),n.createElement("div",{ref:this.sizerRef,style:u},t),this.props.placeholder?n.createElement("div",{ref:this.placeHolderSizerRef,style:u},this.props.placeholder):null)}}]),h}();return h.defaultProps={minWidth:1,injectStyles:!0},h}); diff --git a/lib/AutosizeInput.js b/lib/AutosizeInput.js index 50b20f6..f97aa4b 100644 --- a/lib/AutosizeInput.js +++ b/lib/AutosizeInput.js @@ -99,18 +99,12 @@ var AutosizeInput = function (_Component) { this.copyInputStyles(); this.updateInputWidth(); } - }, { - key: 'componentWillReceiveProps', - value: function componentWillReceiveProps(nextProps) { - var id = nextProps.id; - - if (id !== this.props.id) { - this.setState({ inputId: id || generateId() }); - } - } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { + if (prevProps.id !== this.props.id) { + this.setState({ inputId: this.props.id || generateId() }); + } if (prevState.inputWidth !== this.state.inputWidth) { if (typeof this.props.onAutosize === 'function') { this.props.onAutosize(this.state.inputWidth); diff --git a/src/AutosizeInput.js b/src/AutosizeInput.js index 423d999..7875c57 100644 --- a/src/AutosizeInput.js +++ b/src/AutosizeInput.js @@ -57,13 +57,10 @@ class AutosizeInput extends Component { this.copyInputStyles(); this.updateInputWidth(); } - componentWillReceiveProps (nextProps) { - const { id } = nextProps; - if (id !== this.props.id) { - this.setState({ inputId: id || generateId() }); - } - } componentDidUpdate (prevProps, prevState) { + if (prevProps.id !== this.props.id) { + this.setState({ inputId: this.props.id || generateId() }); + } if (prevState.inputWidth !== this.state.inputWidth) { if (typeof this.props.onAutosize === 'function') { this.props.onAutosize(this.state.inputWidth); diff --git a/src/__tests__/AutosizeInput-test.js b/src/__tests__/AutosizeInput-test.js index 3cf0db7..ad8d31f 100644 --- a/src/__tests__/AutosizeInput-test.js +++ b/src/__tests__/AutosizeInput-test.js @@ -9,13 +9,16 @@ describe('AutosizeInput test', function () { } // Render an instance of the component var instance = TestUtils.renderIntoDocument( - + ); var inputElement = TestUtils.findRenderedDOMComponentWithTag(instance, 'input'); it('should assign the given inputClassName', function () { - expect(true).toEqual(true); expect(inputElement.className).toEqual('testClass'); }); + it('should use the id passed as prop', function () { + expect(inputElement.id).toEqual('abcId'); + }); + });