index.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  88. //搜索后保持现在的值,继续输入时要用于对比
  89. that.setState({
  90. searchPre:newText
  91. });
  92. },config.delayTime);
  93. this.setState({
  94. timer
  95. });
  96. }
  97. handleBlur(e){//为了阻止冒泡事件
  98. const {boxMark,handleClear,handleChange,i} = this.props;
  99. e.stopPropagation();
  100. // 延时清空搜索结果,不延时会影响选中
  101. clearTimeout(this.state.clearTimer);
  102. const clearTimer = setTimeout(function(){
  103. handleClear && handleClear({boxMark})
  104. },config.delayTime);
  105. this.setState({
  106. clearTimer
  107. });
  108. }
  109. moveEnd(obj) {
  110. obj.focus();
  111. if($.support.msie)
  112. {
  113. var range = document.selection.createRange();
  114. this.last = range;
  115. range.moveToElementText(obj);
  116. range.select();
  117. document.selection.empty(); //取消选中
  118. }
  119. else
  120. {
  121. var range = document.createRange();
  122. range.selectNodeContents(obj);
  123. range.collapse(false);
  124. var sel = window.getSelection();
  125. sel.removeAllRanges();
  126. sel.addRange(range);
  127. }
  128. }
  129. handleKeydown(e){
  130. const ev = e||window.event;
  131. const target = ev.target||ev.srcElement;
  132. let innerVal = target.innerText;
  133. /*if(this.props.full){
  134. return false;
  135. }*/
  136. //禁止回车事件
  137. if(ev.keyCode==13){return false;}
  138. //backspace事件
  139. if(ev.keyCode==8){
  140. //用于对比backspace前后的值
  141. this.setState({
  142. preVal:innerVal
  143. })
  144. }
  145. }
  146. handleKeyup(e){
  147. const {boxMark,handleKeydown,i,value,removeId} = this.props;
  148. const {preVal,index} = this.state;
  149. const ev = e||window.event;
  150. const target = ev.target||ev.srcElement;
  151. let innerVal = target.innerText;
  152. if(ev.keyCode==8){
  153. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  154. let pattern1 = new RegExp(/^\,|^\,|^\.|^\。|^\、|^\;|^\;|^\:|^\:/);
  155. if(pattern1.test(preVal)){//以标点开头的情况-现病史
  156. if(preVal.trim().length==2){
  157. removeId && removeId({boxMark,i:index,text:""});
  158. }
  159. }else{
  160. if(preVal.trim().length==1){
  161. removeId && removeId({boxMark,i:index,text:""});
  162. }
  163. }
  164. if(innerVal !==preVal){return false}
  165. let data = innerVal.trim();
  166. //判断是否为空、中英文:, 。、;,且不是第一位
  167. let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$\s/);
  168. if(index!==0 && pattern.test(data)){
  169. let preObj = $(this.$span.current).prev();
  170. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  171. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  172. this.moveEnd(obj[0]);
  173. this.setState({
  174. index: null
  175. })
  176. }
  177. }
  178. }
  179. /*shouldComponentUpdate(next){
  180. if(JSON.stringify(next) == JSON.stringify(this.props)){
  181. return false;
  182. }
  183. return true;
  184. }*/
  185. componentWillReceiveProps(next){
  186. const isRead = this.props.isRead;
  187. if(next.isRead != isRead){
  188. this.$span.current.innerText = next.value||'';
  189. }
  190. }
  191. componentDidMount(){
  192. const {value} = this.props;
  193. if(value){
  194. this.$span.current.innerText = value||'';
  195. }
  196. if(isIE()){
  197. $(this.$span.current).onIe8Input(function(e){
  198. this.onChange(e)
  199. },this);
  200. }
  201. }
  202. render() {
  203. return <span className={style['editable-span']+(this.props.full?' '+style['full']:'')}
  204. contentEditable='true'
  205. ref={this.$span}
  206. onInput={this.onChange}
  207. onFocus={this.handleFocus}
  208. onBlur={this.handleBlur}
  209. onkeydown={this.handleKeydown}
  210. onkeyup={this.handleKeyup}></span>;
  211. }
  212. }
  213. export default EditableSpan;