index.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,handleEnter} from '@utils/tools.js';
  5. import Notify from '../Notify/index.js';
  6. /*****
  7. * author:zn@2018-12-10
  8. * 自由文本输入组件
  9. * 接收参数:
  10. * value:默认显示文字
  11. * handleChange:输入变化时触发函数,接收以下参数:
  12. * * boxMark:病例项标识
  13. * * i:标签index
  14. * * text:标签内文字
  15. *
  16. * ****/
  17. class EditableSpan extends Component{
  18. constructor(props){
  19. super(props);
  20. this.state={
  21. timer:null,
  22. clearTimer:null,
  23. oldText:props.value,
  24. labelVal:'', //存放标签原有的值--主诉字数限制用
  25. preVal:''
  26. };
  27. this.$span = React.createRef();
  28. this.handleFocus = this.handleFocus.bind(this);
  29. this.onChange = this.onChange.bind(this);
  30. this.handleBlur = this.handleBlur.bind(this);
  31. this.handleKeydown = this.handleKeydown.bind(this);
  32. this.handleKeyup = this.handleKeyup.bind(this);
  33. }
  34. handleFocus(e){
  35. e.stopPropagation();
  36. const {setFocusIndex,i,boxMark}= this.props;
  37. let text = e.target.innerText;
  38. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  39. this.setState({
  40. labelVal:text
  41. });
  42. }
  43. onChange(e){
  44. e.stopPropagation();
  45. const {handleChange,boxMark,i,handleSearch,value,mainSaveText,mainIds} = this.props;
  46. const {labelVal,oldText} = this.state;
  47. const text1 =e.target.innerText;
  48. let mainText = filterArr(mainSaveText);//主诉字数
  49. if(+boxMark==1){
  50. if(mainText.length >= config.limited){
  51. if(text1.length > labelVal.length){
  52. e.target.innerText = labelVal;
  53. Notify.info(config.limitText);
  54. return
  55. }else if(text1.length == labelVal.length){
  56. this.setState({
  57. labelVal:text1
  58. });
  59. }else{
  60. handleChange&&handleChange({text1,boxMark,i});
  61. }
  62. return
  63. }
  64. }
  65. this.setState({
  66. labelVal:text1
  67. });
  68. const that = this;
  69. handleChange&&handleChange({text1,boxMark,i});
  70. //延迟搜索
  71. clearTimeout(this.state.timer);
  72. const timer = setTimeout(function(){
  73. let newText = e.target.innerText;
  74. let temp = '';
  75. let search='';
  76. clearTimeout(that.state.timer);
  77. // temp = newText.replace(oldText.replace(/(^\s*)|(\s*$)/g,''),'');
  78. temp = newText.replace(labelVal.replace(/(^\s*)|(\s*$)/g,''),'');
  79. search = temp.replace(/(^\s*)|(\s*$)/g,'');
  80. // console.log(111,labelVal,333,newText,444,search);
  81. handleSearch&&handleSearch({text:search,boxMark,mainIds});
  82. /*that.setState({
  83. oldText:newText.replace(search,'')
  84. })*/
  85. },config.delayTime);
  86. this.setState({
  87. timer
  88. });
  89. }
  90. handleBlur(e){//为了阻止冒泡事件
  91. const {boxMark,handleClear,handleChange,i} = this.props;
  92. e.stopPropagation();
  93. // 延时清空搜索结果,不延时会影响选中
  94. clearTimeout(this.state.clearTimer);
  95. const clearTimer = setTimeout(function(){
  96. handleClear && handleClear({boxMark})
  97. },config.delayTime);
  98. this.setState({
  99. clearTimer
  100. });
  101. }
  102. handleKeydown(e){
  103. let innerVal = e.target.innerText;
  104. //禁止回车事件
  105. if(e.keyCode==13){return false;}
  106. //backspace事件
  107. if(e.keyCode==8){
  108. //用于对比backspace前后的值
  109. this.setState({
  110. preVal:innerVal
  111. })
  112. }
  113. }
  114. handleKeyup(e){
  115. const {boxMark,handleKeydown,i,value} = this.props;
  116. const {preVal} = this.state;
  117. let innerVal = e.target.innerText;
  118. if(e.keyCode==8){
  119. if(innerVal !==preVal){return}
  120. let data = innerVal.trim();
  121. //判断是否为空、中英文:, 。、;,且不是第一位
  122. if(i!==0 &&data==""||data==","||data==","||data==":"||data==":"||data=="."||data=="。"||data=="、"||data==";"||data==";"){
  123. handleKeydown&&handleKeydown({boxMark,i});
  124. }
  125. }
  126. }
  127. /*shouldComponentUpdate(next){
  128. if(JSON.stringify(next) == JSON.stringify(this.props)){
  129. return false;
  130. }
  131. return true;
  132. }*/
  133. componentWillReceiveProps(next){
  134. const isRead = this.props.isRead;
  135. if(next.isRead != isRead){
  136. this.$span.current.innerText = next.value||'';
  137. }
  138. }
  139. componentDidMount(){
  140. const {value} = this.props;
  141. if(value){
  142. this.$span.current.innerText = value||'';
  143. }
  144. }
  145. render() {
  146. return <span className={style['editable-span']}
  147. contentEditable='true'
  148. ref={this.$span}
  149. onInput={this.onChange}
  150. onFocus={this.handleFocus}
  151. onBlur={this.handleBlur}
  152. onkeydown={this.handleKeydown}
  153. onkeyup={this.handleKeyup}></span>;
  154. }
  155. }
  156. export default EditableSpan;