import React, { Component } from "react";
import style from "./index.less";

class Textarea extends Component {
  constructor(props) {
    super(props);
    this.$dom = React.createRef();
    this.handleInput = this.handleInput.bind(this);
  }
  handleInput(e){
    const {handleChangeAssistValue,idx} = this.props;
    const text = e.target.innerText;
    handleChangeAssistValue&&handleChangeAssistValue(text,idx);
  }
  shouldComponentUpdate(next){
    if(JSON.stringify(next) == JSON.stringify(this.props)){
      return false;
    }
    return true;
  }
  componentWillReceiveProps(next){
    const isRead = this.props.isRead;
    if(next.isRead != isRead){
      this.$dom.current.innerText = next.value||'';
    }
  }
  componentDidMount(){
    const {value} = this.props;
    if(value){
      this.$dom.current.innerText = value||'';
    }
  }
  render() {
    const {value} = this.props;
    return (
      <div className={style.divTextarea}
          contenteditable={true}
          ref={this.$dom}
          onInput={this.handleInput}
      ></div>
    );
  }
}

export default Textarea;
