diff options
author | Francesco Cioria <firefelix@gmail.com> | 2017-01-21 12:33:50 +0100 |
---|---|---|
committer | Francesco Cioria <firefelix@gmail.com> | 2017-01-22 19:02:12 +0100 |
commit | 3f1ae217d545421b827115c1a2212928a7dc7649 (patch) | |
tree | 4f1fde330264a4d10fb2cc335ed0c9f5018e4e0d /src | |
parent | 65bda438031a7ffb92e28b4b715e33851fc4a617 (diff) | |
download | react-autosize-textarea-3f1ae217d545421b827115c1a2212928a7dc7649.zip react-autosize-textarea-3f1ae217d545421b827115c1a2212928a7dc7649.tar.gz react-autosize-textarea-3f1ae217d545421b827115c1a2212928a7dc7649.tar.bz2 |
move maxHeight/Rows logic in separate function; fix bugs;
Diffstat (limited to 'src')
-rw-r--r-- | src/TextareaAutosize.js | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/TextareaAutosize.js b/src/TextareaAutosize.js index 44b0bef..c51648c 100644 --- a/src/TextareaAutosize.js +++ b/src/TextareaAutosize.js @@ -53,15 +53,24 @@ export default class TextareaAutosize extends React.Component { getValue = ({ valueLink, value }) => valueLink ? valueLink.value : value; - onChange = e => { + hasReachedMaxRows = (value) => { + const { maxRows } = this.props; + + const numberOfRows = (value || '').split('\n').length; + + return t.Number.is(maxRows) && numberOfRows >= maxRows; + } + + updateMaxHeight = (value) => { const { - props: { maxRows, onChange }, + props: { maxRows }, state: { maxHeight } } = this; - const numberOfRows = e.target.value.split('\n').length; + const hasReachedMaxRows = this.hasReachedMaxRows(value); - if (!maxHeight && numberOfRows >= maxRows) { + if (!maxHeight && hasReachedMaxRows) { + const numberOfRows = (value || '').split('\n').length; const computedStyle = window.getComputedStyle(this.textarea); const paddingTop = parseFloat(computedStyle.getPropertyValue('padding-top'), 10); @@ -72,14 +81,24 @@ export default class TextareaAutosize extends React.Component { const borderBottomWidth = parseInt(computedStyle.getPropertyValue('border-bottom-width'), 10); const verticalBorderWidth = (borderTopWidth || 0) + (borderBottomWidth || 0); + const height = this.textarea.offsetHeight - verticalPadding - verticalBorderWidth; + this.setState({ - maxHeight: this.textarea.offsetHeight - verticalPadding - verticalBorderWidth + maxHeight: height / numberOfRows * maxRows }); - } else if (maxHeight && numberOfRows < maxRows) { + + return true; + } else if (maxHeight && !hasReachedMaxRows) { this.setState({ maxHeight: null }); + + return false; } - onChange && onChange(e); + } + + onChange = e => { + this.updateMaxHeight(e.target.value); + this.props.onChange && this.props.onChange(e); } getLocals = () => { |