index.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { Component } from "react";
  2. import style from "./index.less";
  3. import Notify from '../Notify';
  4. import config from '@config/index';
  5. import {setLastPosition} from '@common/js/util';
  6. import {isIE} from '@utils/tools.js';
  7. import $ from "jquery";
  8. class Textarea extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. timer:null,
  13. inpText:'',
  14. overFlag:false
  15. };
  16. this.$dom = React.createRef();
  17. this.handleInput = this.handleInput.bind(this);
  18. this.handleFocus = this.handleFocus.bind(this);
  19. //this.handleBlur = this.handleBlur.bind(this);
  20. this.handleKeydown = this.handleKeydown.bind(this);
  21. }
  22. handleFocus(){ //初始显示props中的值,focus已经显示输入的值,避免值更新闪烁
  23. const {handleFocus,fuzhen,handleInput,isChronic} = this.props;
  24. handleFocus&&handleFocus(); //其他史、查体获取数据的方法
  25. if(fuzhen&&!isChronic&&!(this.$dom.current.innerText?this.$dom.current.innerText:this.$dom.current.innerHTML)){
  26. const text = config.currentText.replace("(**)",fuzhen);
  27. this.$dom.current.innerText?(this.$dom.current.innerText = text):(this.$dom.current.innerHTML = text);
  28. handleInput&&handleInput({text});
  29. }
  30. }
  31. handleInput(e){
  32. const {handleInput,boxMark,handlePush,hasMain} = this.props;
  33. const {inpText,overFlag} = this.state;
  34. const text = e.target.innerText || e.target.innerHTML;
  35. const stimer = this.state.timer;
  36. if(boxMark=='1'&&text.length>config.limited){ //主诉字符数限制
  37. e.target.innerText?(e.target.innerText = text.substr(0,config.limited)):(e.target.innerHTML = text.substr(0,config.limited));
  38. e.target.blur();
  39. Notify.error(config.limitText);
  40. if(overFlag){
  41. e.target.innerText?(e.target.innerText = inpText):(e.target.innerHTML = inpText);
  42. return
  43. }
  44. this.setState({
  45. inpText:text.substr(0,config.limited),
  46. overFlag:true
  47. })
  48. return;
  49. }
  50. /*if(boxMark=='3'&&!hasMain){
  51. e.target.innerText = '';
  52. return;
  53. }*/
  54. this.setState({
  55. inpText:text,
  56. overFlag:false
  57. })
  58. //存值到store
  59. handleInput&&handleInput({text});
  60. //右侧推送--延时推送
  61. clearTimeout(stimer);
  62. let timer = setTimeout(function(){
  63. handlePush&&handlePush();
  64. clearTimeout(stimer);
  65. },config.delayPushTime);
  66. this.setState({
  67. timer
  68. });
  69. }
  70. handleKeydown(e){
  71. const {boxMark} = this.props;
  72. const ev = e||window.event;
  73. if(+boxMark==1){
  74. //禁止回车事件
  75. if(ev.keyCode==13){return false;}
  76. }
  77. }
  78. shouldComponentUpdate(next){
  79. if(JSON.stringify(next) == JSON.stringify(this.props)){
  80. return false;
  81. }
  82. return true;
  83. }
  84. componentWillReceiveProps(next){
  85. const isRead = this.props.isRead;
  86. if(next.isRead != isRead||(next.value!=this.props.value&&next.value&&next.value.indexOf("复诊")!=-1)){ //value对比解决复诊不显示bug,复诊对比解决关标跳到前面bug
  87. this.$dom.current.innerText?(this.$dom.current.innerText = next.value||''):(this.$dom.current.innerHTML = next.value||'');
  88. }
  89. }
  90. componentDidMount(){
  91. const {value} = this.props;
  92. if(value){
  93. this.$dom.current.innerText?(this.$dom.current.innerText = value||''):(this.$dom.current.innerText=value||'');
  94. }
  95. if(isIE()){
  96. $(this.$dom.current).onIe8Input(function(e){
  97. this.handleInput(e)
  98. },this);
  99. }
  100. }
  101. render() {
  102. const { title } = this.props;
  103. return (
  104. <div className={style["box"]}>
  105. <div className={style["title"]}>{title}</div>
  106. {/*{isRead?<div className={style["content"]+" "+'11'} contentEditable={true} onFocus={this.handleFocus}>{value}</div>:''}*/}
  107. <div className={style["content"]}
  108. onFocus={this.handleFocus}
  109. ref={this.$dom}
  110. contentEditable={true}
  111. onInput={this.handleInput}
  112. onkeydown={this.handleKeydown}>
  113. </div>
  114. </div>
  115. );
  116. }
  117. }
  118. export default Textarea;