react-native 自動(dòng)計(jì)算行高textInput組件

react-native 根據(jù)textInput輸入文本內(nèi)容自動(dòng)計(jì)算輸入框高度

import React from 'react';
import {
  TextInput,
} from 'react-native';

import BaseComponent from '../../components/common/baseComponent';

class AutoExpandingTextInput extends BaseComponent {
  constructor(props) {
    super(props);
    this.displayName = 'AutoExpandingTextInput';

    this.state = {
      text: '',
      height: 0,
    };
    this.onChange = this.onChange.bind(this);
  }

  onChange(event) {
    // console.log('======onChange========', event.nativeEvent);
    this.setState({
      text: event.nativeEvent.text,
      height: event.nativeEvent.contentSize.height,
    });
  }

  render() {
    const { style, textHeightMin, textHeightMax } = this.props;
    let textHeight = Math.max(textHeightMin, this.state.height);
    if (textHeight >= textHeightMax) {
      textHeight = textHeightMax;
    }
    return (
      <TextInput
        {...this.props}
        multiline={true}
        onChange={this.onChange}
        underlineColorAndroid="transparent"
        style={[style, { height: textHeight }]}
        value={this.state.text}
      />
    );
  }
}

AutoExpandingTextInput.propTypes = {
  textHeightMin: React.PropTypes.number.isRequired,
  textHeightMax: React.PropTypes.number.isRequired,
};

export default AutoExpandingTextInput;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容