AutoExpandingInput.js 917 Bytes
import React, {Component} from "react";
import {StyleSheet, TextInput} from "react-native";

export default class AutoExpandingInput extends Component {
  constructor(props) {
    super(props);
    this.state = {
      height: 20
    };
    this.onChange = this._onChange.bind(this);
  }

  onContentSizeChange(event) {
    this.setState({ height: event.nativeEvent.contentSize.height });
  }

  _onChange(changeText) {
    this.props.onTextChange(changeText);
  }

  render() {
    const{style} = this.props;
    return (
      <TextInput
        {...this.props}
        multiline={true}
        onChangeText={this.onChange}
        underlineColorAndroid="transparent"
        onContentSizeChange={this.onContentSizeChange.bind(this)}
        style={[style, { height: Math.max(20, this.state.height) }]}
      />
    );
  }
}

const styles = StyleSheet.create({
  inputStyle: {
     textAlignVertical: "top"
  }
});