index.jsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,isIE} from '@utils/tools.js';
  5. import Notify from '../Notify/index.js';
  6. import $ from 'jquery';
  7. /*****
  8. * author:zn@2018-12-10
  9. * 自由文本输入组件
  10. * 接收参数:
  11. * value:默认显示文字
  12. * handleChange:输入变化时触发函数,接收以下参数:
  13. * * boxMark:病例项标识
  14. * * i:标签index
  15. * * text:标签内文字
  16. *
  17. * ****/
  18. class EditableSpan extends Component{
  19. constructor(props){
  20. super(props);
  21. this.state={
  22. timer:null,
  23. clearTimer:null,
  24. oldText:props.value,
  25. labelVal:'', //存放标签原有的值--主诉字数限制用
  26. preVal:'',
  27. index:null,
  28. searchPre:''
  29. };
  30. this.$span = React.createRef();
  31. this.handleFocus = this.handleFocus.bind(this);
  32. this.onChange = this.onChange.bind(this);
  33. this.handleBlur = this.handleBlur.bind(this);
  34. this.handleKeydown = this.handleKeydown.bind(this);
  35. this.handleKeyup = this.handleKeyup.bind(this);
  36. this.moveEnd = this.moveEnd.bind(this);
  37. }
  38. handleFocus(e){
  39. e.stopPropagation();
  40. const {setFocusIndex,i,boxMark}= this.props;
  41. let text = e.target.innerText;
  42. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  43. this.setState({
  44. labelVal:text,
  45. index:i,
  46. searchPre:text
  47. });
  48. }
  49. onChange(e){
  50. e.stopPropagation();
  51. const {handleChange,boxMark,i,handleSearch,value,mainSaveText,mainIds} = this.props;
  52. const {labelVal,searchPre} = this.state;
  53. const text1 =e.target.innerText;
  54. let mainText = filterArr(mainSaveText);//主诉字数
  55. if(+boxMark==1){
  56. if(mainText.length >= config.limited){
  57. if(text1.length > labelVal.length){
  58. e.target.innerText = labelVal;
  59. Notify.info(config.limitText);
  60. return
  61. }else if(text1.length == labelVal.length){
  62. this.setState({
  63. labelVal:text1
  64. });
  65. }else{
  66. handleChange&&handleChange({text1,boxMark,i});
  67. }
  68. return
  69. }
  70. }
  71. this.setState({
  72. labelVal:text1
  73. });
  74. const that = this;
  75. handleChange&&handleChange({text1,boxMark,i});
  76. //延迟搜索
  77. clearTimeout(this.state.timer);
  78. const timer = setTimeout(function(){
  79. let newText = e.target.innerText;
  80. let temp = '',isEnd=false;
  81. let search='';
  82. clearTimeout(that.state.timer);
  83. temp = newText.replace(searchPre,'');
  84. isEnd = !(newText.indexOf(searchPre)>0);
  85. search = temp.replace(/[(^\s*)|(\s*$)|(^\,*)|(\,*$)]/g,'');
  86. console.log(labelVal,'旧:',searchPre,'新:',newText,'搜索:',search);
  87. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  88. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  89. }
  90. //搜索后保持现在的值,继续输入时要用于对比
  91. that.setState({
  92. searchPre:newText
  93. });
  94. },config.delayTime);
  95. this.setState({
  96. timer
  97. });
  98. }
  99. handleBlur(e){//为了阻止冒泡事件
  100. const {boxMark,handleClear,handleChange,i} = this.props;
  101. e.stopPropagation();
  102. // 延时清空搜索结果,不延时会影响选中
  103. clearTimeout(this.state.clearTimer);
  104. const clearTimer = setTimeout(function(){
  105. handleClear && handleClear({boxMark})
  106. },config.delayTime);
  107. this.setState({
  108. clearTimer
  109. });
  110. }
  111. moveEnd(obj) {
  112. if(window.getSelection){//ie11 10 9 ff safari
  113. obj.focus(); //解决ff不获取焦点无法定位问题
  114. var range = window.getSelection();//创建range
  115. range.selectAllChildren(obj);//range 选择obj下所有子内容
  116. range.collapseToEnd();//光标移至最后
  117. }
  118. else if (document.selection) {//ie10 9 8 7 6 5
  119. var range = document.selection.createRange();//创建选择对象
  120. range.moveToElementText(obj);//range定位到obj
  121. range.collapse(false);//光标移至最后
  122. range.select();
  123. }
  124. }
  125. handleKeydown(e){
  126. const ev = e||window.event;
  127. const target = ev.target||ev.srcElement;
  128. let innerVal = target.innerText;
  129. /*if(this.props.full){
  130. return false;
  131. }*/
  132. //禁止回车事件
  133. if(ev.keyCode==13){return false;}
  134. //backspace事件
  135. if(ev.keyCode==8){
  136. //用于对比backspace前后的值
  137. this.setState({
  138. preVal:innerVal
  139. })
  140. }
  141. }
  142. handleKeyup(e){
  143. const {boxMark,handleKeydown,i,value,removeId} = this.props;
  144. const {preVal,index} = this.state;
  145. const ev = e||window.event;
  146. const target = ev.target||ev.srcElement;
  147. let innerVal = target.innerText;
  148. if(ev.keyCode==8){
  149. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  150. if(preVal.trim().length==1){
  151. removeId && removeId({boxMark,i:index,text:""});
  152. // this.moveEnd(obj[0]);
  153. }
  154. if(innerVal !==preVal){return false}
  155. let data = innerVal.trim();
  156. //判断是否为空、中英文:, 。、;,且不是第一位
  157. let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$\s/);
  158. if(index!==0 && pattern.test(data)){
  159. let preObj = $(this.$span.current).prev();
  160. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  161. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  162. this.moveEnd(obj[0]);
  163. this.setState({
  164. index: null
  165. })
  166. }
  167. }
  168. }
  169. /*shouldComponentUpdate(next){
  170. if(JSON.stringify(next) == JSON.stringify(this.props)){
  171. return false;
  172. }
  173. return true;
  174. }*/
  175. componentWillReceiveProps(next){
  176. const isRead = this.props.isRead;
  177. if(next.isRead != isRead){
  178. this.$span.current.innerText = next.value||'';
  179. }
  180. }
  181. componentDidMount(){
  182. const {value} = this.props;
  183. if(value){
  184. this.$span.current.innerText = value||'';
  185. }
  186. if(isIE()){
  187. $(this.$span.current).onIe8Input(function(e){
  188. this.onChange(e)
  189. },this);
  190. }
  191. }
  192. render() {
  193. return <span className={style['editable-span']+(this.props.full?' '+style['full']:'')}
  194. contentEditable='true'
  195. ref={this.$span}
  196. onInput={this.onChange}
  197. onFocus={this.handleFocus}
  198. onBlur={this.handleBlur}
  199. onkeydown={this.handleKeydown}
  200. onkeyup={this.handleKeyup}></span>;
  201. }
  202. }
  203. export default EditableSpan;