index.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. search = temp.replace(/(^\s*)|(\s*$)/g,'');
  79. //console.log(111,oldText,333,newText,444,search);
  80. handleSearch&&handleSearch({text:search,boxMark,mainIds});
  81. that.setState({
  82. oldText:newText
  83. })
  84. },config.delayTime);
  85. this.setState({
  86. timer
  87. });
  88. }
  89. handleBlur(e){//为了阻止冒泡事件
  90. const {boxMark,handleClear,handleChange,i} = this.props;
  91. e.stopPropagation();
  92. // 延时清空搜索结果,不延时会影响选中
  93. clearTimeout(this.state.clearTimer);
  94. const clearTimer = setTimeout(function(){
  95. handleClear && handleClear({boxMark})
  96. },config.delayTime);
  97. this.setState({
  98. clearTimer
  99. });
  100. }
  101. handleKeydown(e){
  102. let innerVal = e.target.innerText;
  103. //禁止回车事件
  104. if(e.keyCode==13){return false;}
  105. //backspace事件
  106. if(e.keyCode==8){
  107. //用于对比backspace前后的值
  108. this.setState({
  109. preVal:innerVal
  110. })
  111. }
  112. }
  113. handleKeyup(e){
  114. const {boxMark,handleKeydown,i,value} = this.props;
  115. const {preVal} = this.state;
  116. let innerVal = e.target.innerText;
  117. if(e.keyCode==8){
  118. if(innerVal !==preVal){return}
  119. let data = innerVal.trim();
  120. //判断是否为空、中英文:, 。、;,且不是第一位
  121. if(i!==0 &&data==""||data==","||data==","||data==":"||data==":"||data=="."||data=="。"||data=="、"||data==";"||data==";"){
  122. handleKeydown&&handleKeydown({boxMark,i});
  123. }
  124. }
  125. }
  126. /*shouldComponentUpdate(next){
  127. if(JSON.stringify(next) == JSON.stringify(this.props)){
  128. return false;
  129. }
  130. return true;
  131. }*/
  132. componentWillReceiveProps(next){
  133. const isRead = this.props.isRead;
  134. if(next.isRead != isRead){
  135. this.$span.current.innerText = next.value||'';
  136. }
  137. }
  138. componentDidMount(){
  139. const {value} = this.props;
  140. if(value){
  141. this.$span.current.innerText = value||'';
  142. }
  143. }
  144. render() {
  145. return <span className={style['editable-span']}
  146. contentEditable='true'
  147. ref={this.$span}
  148. onInput={this.onChange}
  149. onFocus={this.handleFocus}
  150. onBlur={this.handleBlur}
  151. onkeydown={this.handleKeydown}
  152. onkeyup={this.handleKeyup}></span>;
  153. }
  154. }
  155. export default EditableSpan;