index.jsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if(window.getSelection){//ie11 10 9 ff safari
  111. obj.focus(); //解决ff不获取焦点无法定位问题
  112. var range = window.getSelection();//创建range
  113. range.selectAllChildren(obj);//range 选择obj下所有子内容
  114. range.collapseToEnd();//光标移至最后
  115. }
  116. else if (document.selection) {//ie10 9 8 7 6 5
  117. var range = document.selection.createRange();//创建选择对象
  118. range.moveToElementText(obj);//range定位到obj
  119. range.collapse(false);//光标移至最后
  120. range.select();
  121. }
  122. }
  123. handleKeydown(e){
  124. const ev = e||window.event;
  125. const target = ev.target||ev.srcElement;
  126. let innerVal = target.innerText;
  127. /*if(this.props.full){
  128. return false;
  129. }*/
  130. //禁止回车事件
  131. if(ev.keyCode==13){return false;}
  132. //backspace事件
  133. if(ev.keyCode==8){
  134. //用于对比backspace前后的值
  135. this.setState({
  136. preVal:innerVal
  137. })
  138. }
  139. }
  140. handleKeyup(e){
  141. const {boxMark,handleKeydown,i,value,removeId} = this.props;
  142. const {preVal,index} = this.state;
  143. const ev = e||window.event;
  144. const target = ev.target||ev.srcElement;
  145. let innerVal = target.innerText;
  146. if(ev.keyCode==8){
  147. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  148. if(preVal.trim().length==1){
  149. removeId && removeId({boxMark,i:index,text:""});
  150. // this.moveEnd(obj[0]);
  151. }
  152. if(innerVal !==preVal){return false}
  153. let data = innerVal.trim();
  154. //判断是否为空、中英文:, 。、;,且不是第一位
  155. let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$\s/);
  156. if(index!==0 && pattern.test(data)){
  157. let preObj = $(this.$span.current).prev();
  158. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  159. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  160. this.moveEnd(obj[0]);
  161. this.setState({
  162. index: null
  163. })
  164. }
  165. }
  166. }
  167. /*shouldComponentUpdate(next){
  168. if(JSON.stringify(next) == JSON.stringify(this.props)){
  169. return false;
  170. }
  171. return true;
  172. }*/
  173. componentWillReceiveProps(next){
  174. const isRead = this.props.isRead;
  175. if(next.isRead != isRead){
  176. this.$span.current.innerText = next.value||'';
  177. }
  178. }
  179. componentDidMount(){
  180. const {value} = this.props;
  181. if(value){
  182. this.$span.current.innerText = value||'';
  183. }
  184. if(isIE()){
  185. $(this.$span.current).onIe8Input(function(e){
  186. this.onChange(e)
  187. },this);
  188. }
  189. }
  190. render() {
  191. return <span className={style['editable-span']+(this.props.full?' '+style['full']:'')}
  192. contentEditable='true'
  193. ref={this.$span}
  194. onInput={this.onChange}
  195. onFocus={this.handleFocus}
  196. onBlur={this.handleBlur}
  197. onkeydown={this.handleKeydown}
  198. onkeyup={this.handleKeyup}></span>;
  199. }
  200. }
  201. export default EditableSpan;