react-native-auto-expanding-textinput 根据输入的内容自增长高度

直接导入这个react-native-auto-expanding-textinput文件  就可以引用



/**
 * Created by xupy on 2016/12/30.
 */
import React, {
    Component,
    PropTypes
} from 'react';
import {
    View,
    StyleSheet,
    TextInput,
} from 'react-native';

var PureRenderMixin = require('react/lib/ReactComponentWithPureRenderMixin');

export default class AutoExpandingTextInput extends Component {

    constructor(props) {
        super(props);
        // initial state
        this.state = {
            height: this.props.minHeight,
            maxHeight: this.props.maxHeight || this.props.minHeight * 3
        };
        this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this);
    }

    static propTypes = {
        onChangeHeight: PropTypes.func.isRequired,
        minHeight: PropTypes.number.isRequired,
        maxHeight: PropTypes.number
    };

    static defaultProps = {};

    componentWillReceiveProps(nextProps) {
        if (nextProps.value === '') {
            this.setState({
                height: this.props.minHeight
            });
        }
    }

    _onChange = (event) => {
        let curHeight = event.nativeEvent.contentSize.height;
        if (curHeight < this.props.minHeight || curHeight > this.state.maxHeight) return;

        if (this.state.height !== curHeight) {
            if (this.props.onChangeHeight) {
                this.props.onChangeHeight(this.state.height, curHeight);
            }
        }

        this.setState({
            height: curHeight
        });
    }
    onContentSizeChange = (params) =>{
        this.setState({
            height: params.nativeEvent.contentSize.height
        });
    }	
    render() {
        let tmpHeight = Math.min(this.state.maxHeight, this.state.height);
        return (
            <TextInput
                {...this.props}
                multiline={true}
                onChange={this._onChange}
                onContentSizeChange={this.onContentSizeChange}
                style={[styles.default, this.props.style, {height: tmpHeight}]}
            />
        );
    }
}

const styles = StyleSheet.create({
    default: {
        paddingHorizontal: 10,
        paddingVertical: 5,
        justifyContent: 'center',
        textAlign: 'left'
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值