diff options
author | Francesco Cioria <firefelix@gmail.com> | 2017-04-20 15:11:41 +0200 |
---|---|---|
committer | Francesco Cioria <firefelix@gmail.com> | 2017-04-20 15:32:30 +0200 |
commit | a4ec821d9ce862bc6f5a5b3b742d9155e38f1eef (patch) | |
tree | f2907c32b96d685add83b036fc218952fe7e871a | |
parent | f3f7f1f130652726706b539bbf07414c6c37fd3e (diff) | |
download | react-autosize-textarea-a4ec821d9ce862bc6f5a5b3b742d9155e38f1eef.zip react-autosize-textarea-a4ec821d9ce862bc6f5a5b3b742d9155e38f1eef.tar.gz react-autosize-textarea-a4ec821d9ce862bc6f5a5b3b742d9155e38f1eef.tar.bz2 |
add prop "innerRef" called with the ref to the DOM node
-rw-r--r-- | src/TextareaAutosize.js | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/TextareaAutosize.js b/src/TextareaAutosize.js index 1cd2377..8c097c7 100644 --- a/src/TextareaAutosize.js +++ b/src/TextareaAutosize.js @@ -12,6 +12,7 @@ const UPDATE = 'autosize:update', * @param onResize - called whenever the textarea resizes * @param rows - minimum number of visible rows * @param maxRows - maximum number of visible rows + * @param innerRef - called with the ref to the DOM node */ export default class TextareaAutosize extends React.Component { @@ -105,23 +106,35 @@ export default class TextareaAutosize extends React.Component { this.props.onChange && this.props.onChange(e); } + saveDOMNodeRef = ref => { + const { innerRef } = this.props; + + if (innerRef) { + innerRef(ref); + } + + this.textarea = ref; + } + getLocals = () => { const { props: { onResize, maxRows, onChange, style, ...props }, // eslint-disable-line no-unused-vars - state: { maxHeight } + state: { maxHeight }, + saveDOMNodeRef } = this; return { ...props, + saveDOMNodeRef, style: maxHeight ? { ...style, maxHeight } : style, onChange: this.onChange }; } render() { - const { children, ...locals } = this.getLocals(); + const { children, saveDOMNodeRef, ...locals } = this.getLocals(); return ( - <textarea {...locals} ref={(ref) => { this.textarea = ref; }}> + <textarea {...locals} ref={saveDOMNodeRef}> {children} </textarea> ); @@ -138,5 +151,6 @@ export default class TextareaAutosize extends React.Component { TextareaAutosize.propTypes = { rows: PropTypes.number, maxRows: PropTypes.number, - onResize: PropTypes.func + onResize: PropTypes.func, + innerRef: PropTypes.func }; |