diff --git a/components/input/Input.js b/components/input/Input.js index 0a1155b8..fa7da3f3 100644 --- a/components/input/Input.js +++ b/components/input/Input.js @@ -54,10 +54,44 @@ const factory = (FontIcon) => { type: 'text' }; + componentDidMount () { + window.addEventListener('resize', this.handleAutoresize); + } + + componentWillReceiveProps (nextProps) { + if (!this.props.multiline && nextProps.multiline) { + window.addEventListener('resize', this.handleAutoresize); + } else if (this.props.multiline && !nextProps.multiline) { + window.removeEventListener('resize', this.handleAutoresize); + } + } + + componentWillUnmount () { + window.removeEventListener('resize', this.handleAutoresize); + } + handleChange = (event) => { + if (this.props.multiline) { + this.handleAutoresize(); + } if (this.props.onChange) this.props.onChange(event.target.value, event); }; + handleAutoresize = () => { + const element = this.refs.input; + // compute the height difference between inner height and outer height + const style = getComputedStyle(element, null); + let heightOffset = 0; + if (style.boxSizing === 'content-box') { + heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)); + } else { + heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth); + } + // resize the input to its content size + element.style.height = 'auto'; + element.style.height = `${element.scrollHeight + heightOffset}px`; + } + blur () { this.refs.input.blur(); } diff --git a/docs/app/components/layout/main/modules/examples/input_example_1.txt b/docs/app/components/layout/main/modules/examples/input_example_1.txt index 9cc017dc..f1801959 100644 --- a/docs/app/components/layout/main/modules/examples/input_example_1.txt +++ b/docs/app/components/layout/main/modules/examples/input_example_1.txt @@ -1,5 +1,5 @@ class InputTest extends React.Component { - state = { name: '', phone: '', email: '', hint: '' }; + state = { name: '', phone: '', multiline: '', email: '', hint: '' }; handleChange = (name, value) => { this.setState({...this.state, [name]: value}); @@ -10,6 +10,7 @@ class InputTest extends React.Component {
+ J} />