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

class Textarea extends Component {
  constructor(props) {
    super(props);
    this.state = {
      timer:null
    };
    this.$dom = React.createRef();
    this.handleInput = this.handleInput.bind(this);
  }
  handleInput(e){
    const {handleChangeAssistValue,idx,handlePush} = this.props;
    const text = e.target.innerText;
    const stimer = this.state.timer;
    handleChangeAssistValue&&handleChangeAssistValue(text,idx);
    //右侧推送--延时推送
    clearTimeout(stimer);
    let timer = setTimeout(function(){
      handlePush&&handlePush();
      clearTimeout(stimer);
    },config.delayPushTime);
    this.setState({
      timer
    });
  }
  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}
          onPropertyChange={this.handleInput}
      ></div>
    );
  }
}

export default Textarea;
