index.jsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { Component } from "react";
  2. import style from "./index.less";
  3. import config from '@config/index';
  4. import $ from 'jquery';
  5. class Textarea extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. timer:null,
  10. val:'报告描述或意见'
  11. };
  12. this.textInput = React.createRef();
  13. this.$dom = React.createRef();
  14. this.$domW = React.createRef();
  15. this.handleInput = this.handleInput.bind(this);
  16. this.handleFocus = this.handleFocus.bind(this);
  17. }
  18. handleInput(e){
  19. const {handleChangeAssistValue,idx,handlePush} = this.props;
  20. const text = e.target.innerText || e.target.innerHTML;
  21. const stimer = this.state.timer;
  22. handleChangeAssistValue&&handleChangeAssistValue(text,idx);
  23. //右侧推送--延时推送
  24. clearTimeout(stimer);
  25. let timer = setTimeout(function(){
  26. handlePush&&handlePush();
  27. clearTimeout(stimer);
  28. },config.delayPushTime);
  29. if(text.trim() != '' && text != '<br>'){
  30. console.log(e.target.nextSibling)
  31. e.target.nextSibling.innerText?(e.target.nextSibling.innerText = ''):(e.target.nextSibling.innerHTML = '')
  32. }else{
  33. e.target.nextSibling.innerText?(e.target.nextSibling.innerText = '报告描述或意见'):(e.target.nextSibling.innerHTML = '报告描述或意见')
  34. }
  35. this.setState({
  36. timer
  37. });
  38. }
  39. shouldComponentUpdate(next){
  40. if(JSON.stringify(next) == JSON.stringify(this.props)){
  41. return false;
  42. }
  43. return true;
  44. }
  45. componentWillReceiveProps(next){
  46. const isRead = this.props.isRead;
  47. if(next.isRead != isRead){
  48. this.$dom.current.innerText?(this.$dom.current.innerText = next.value||'') : (this.$dom.current.innerHTML = next.value||'')
  49. }
  50. }
  51. componentDidMount(){
  52. const {value} = this.props;
  53. if(value && value.trim()){
  54. this.$dom.current.innerText?(this.$dom.current.innerText = value) : (this.$dom.current.innerHTML = value)
  55. this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = ''):(this.$dom.current.nextSibling.innerHTML = '')
  56. }else{
  57. this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = '报告描述或意见'):(this.$dom.current.nextSibling.innerHTML = '报告描述或意见')
  58. }
  59. }
  60. handleFocus(){ //ie8下提示语挡住输入框,导致输入框无法聚焦
  61. this.textInput.current.previousSibling.focus();
  62. }
  63. render() {
  64. const {idx} = this.props;
  65. return (
  66. <div className={style.textWap}>
  67. <div className={style.divTextarea}
  68. contenteditable={true}
  69. ref={this.$dom}
  70. onInput={this.handleInput}
  71. onKeyUp={this.handleInput}
  72. ></div>
  73. <p ref={this.textInput} onClick={this.handleFocus} className={style.textareaWarring}></p>
  74. </div>
  75. );
  76. }
  77. }
  78. export default Textarea;