index.jsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React,{Component} from 'react';
  2. import className from 'classnames';
  3. import {NumberUnitPan} from '@commonComp';
  4. import style from './index.less';
  5. import config from '@config/index.js';
  6. import {filterArr,handleEnter,isIE,getPageCoordinate,filterDataArr} from '@utils/tools.js';
  7. import {Notify} from '@commonComp';
  8. import $ from 'jquery';
  9. /***
  10. * 带单位数字组件
  11. * 接收参数:
  12. * value: 默认选中的值
  13. * placeholder:灰显文字
  14. * handleSelect: 选中事件
  15. * show: 是否显示下拉
  16. *
  17. * ***/
  18. class NumberUnitDrop extends Component{
  19. constructor(props){
  20. super(props);
  21. this.state={
  22. editable:false, //标签是否可输入
  23. numEditable:true, //数字框是否可输入
  24. timer:null,
  25. hasSelect:false, //是否点过下拉键盘
  26. isClosed:false,
  27. value:props.value,
  28. placeholderFlag:false,
  29. labelVal:'',
  30. boxLeft:0,
  31. boxTop:0,
  32. tmpTop:0,
  33. tmpScroll:0
  34. };
  35. this.$span = React.createRef();
  36. this.$cont = React.createRef();
  37. this.select = this.select.bind(this);
  38. this.numInpBlur = this.numInpBlur.bind(this);
  39. this.handleSpanInp = this.handleSpanInp.bind(this);
  40. this.handleNumClick = this.handleNumClick.bind(this);
  41. this.changeToEdit = this.changeToEdit.bind(this);
  42. }
  43. select(text){ //选中键盘上数字事件
  44. const {handleSelect,ikey,suffix,prefix,mainSaveText,formulaCode} = this.props;
  45. this.setState({
  46. hasSelect:true
  47. });
  48. if(!text){
  49. this.setState({
  50. value:''
  51. });
  52. }
  53. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText,formulaCode});
  54. }
  55. handleNumClick(e){
  56. e.stopPropagation();
  57. const {show,handleShow,ikey,id,patId,handleHide,value} = this.props;
  58. const {hasSelect} = this.state;
  59. const that = this;
  60. //双击时不显示下拉
  61. clearTimeout(this.state.timer);
  62. const timer = setTimeout(function(){
  63. const {hasSelect,editable,isClosed} = that.state;
  64. if(editable||isClosed){
  65. return;
  66. }
  67. handleShow&&handleShow({ikey,id:patId||id});
  68. },300);
  69. this.setState({
  70. timer,
  71. boxLeft:getPageCoordinate(e).boxLeft,
  72. boxTop:getPageCoordinate(e).boxTop,
  73. tmpScroll: $("#addScrollEvent")[0].scrollTop,
  74. tmpTop:getPageCoordinate(e).boxTop
  75. });
  76. $("#addScrollEvent").scroll(()=>{
  77. let scrollYs = $("#addScrollEvent")[0].scrollTop;
  78. this.setState({
  79. boxTop:this.state.tmpTop - scrollYs + this.state.tmpScroll
  80. })
  81. })
  82. handleHide&&handleHide();
  83. }
  84. //数字框失焦,保存值到store中
  85. numInpBlur(e){
  86. e.stopPropagation();
  87. const {handleSelect,ikey,suffix,prefix,mainSaveText,handleLabelChange,boxMark,formulaCode} = this.props;
  88. const {editable} = this.state;
  89. if(editable){
  90. const text = e.target.innerText;
  91. // this.$span.current.innerText=''; //修改生成文字变成输入的2倍bug
  92. // handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
  93. handleLabelChange&&handleLabelChange({ikey,changeVal:text,suffix,prefix,mainSaveText,type:boxMark,formulaCode});
  94. }
  95. this.setState({
  96. isClosed:false,
  97. numEditable:true,
  98. hasSelect:false,
  99. editable:false
  100. });
  101. }
  102. handleSpanInp(e){ //数字框输入事件
  103. e.stopPropagation();
  104. // 主诉字数达到上限时不允许输入
  105. const {mainSaveText,ikey,type,handleSelect,suffix,prefix,boxMark,formulaCode} = this.props;
  106. const {labelVal,editable} = this.state;
  107. let mainText = filterDataArr(mainSaveText);//主诉字数
  108. if(editable){//避免IE中点击标签也会触发
  109. let val = e.target.innerText;
  110. if(+boxMark==1){
  111. if(mainText.length >= config.limited){
  112. if(val.length > labelVal.length){
  113. e.target.innerText = labelVal;
  114. Notify.info(config.limitText);
  115. return
  116. }else if(val.length == labelVal.length){
  117. this.setState({
  118. labelVal:val
  119. });
  120. }else{
  121. handleSelect&&handleSelect({ikey,text:val,suffix,prefix,mainSaveText,formulaCode});
  122. }
  123. }
  124. }
  125. }
  126. }
  127. getClasses(){ //整个标签是否有值的状态
  128. const {value,hideTag,show} = this.props;
  129. const inpValue = this.state.value;
  130. const blueBorder = this.state.editable?style['blue-border']:'';
  131. const isSelected = value||inpValue?style['selected']:style['container'];
  132. const noTag = hideTag?style['no-tag']:'';
  133. if(show){
  134. $(this.$cont.current).addClass(style['borderd']);
  135. }else{
  136. $(this.$cont.current).removeClass(style['borderd']);
  137. }
  138. return className(isSelected,noTag,blueBorder);
  139. }
  140. changeToEdit(e){ //整个标签双击编辑状态
  141. e.stopPropagation();
  142. const {value,id,handleDbclick,patId} = this.props;
  143. const text = e.target.innerText;
  144. // clearTimeout(this.state.timer);//取消延时的单击事件
  145. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  146. this.setState({
  147. editable: true,
  148. labelVal:text
  149. });
  150. //失焦关闭编辑状态
  151. setTimeout(()=>{
  152. e.target.focus();
  153. })
  154. //双击埋点记录
  155. handleDbclick && handleDbclick({id:patId||id});
  156. }
  157. }
  158. componentDidMount(){
  159. if(isIE()){
  160. $(this.$span.current).onIe8Input(function(e){
  161. this.handleSpanInp(e)
  162. },this);
  163. }
  164. }
  165. render(){
  166. const {placeholder,prefix,suffix,show,value,handleHide} = this.props;
  167. const {numEditable,editable,hasSelect,placeholderFlag,boxLeft,boxTop} = this.state;
  168. return <div className={this.getClasses()} ref={this.$cont}>
  169. <span>{prefix}</span>
  170. <span onClick={this.handleNumClick}
  171. contentEditable={editable}
  172. ref = {this.$span}
  173. onBlur={this.numInpBlur}
  174. onInput={this.handleSpanInp}
  175. onDoubleClick={this.changeToEdit}
  176. onkeydown={handleEnter}
  177. style={{cursor:editable?'text':'pointer'}}>{value||placeholder}</span>
  178. <span>{suffix}</span>
  179. <NumberUnitPan handleSelect={(text)=>this.select(text)}
  180. onClose={handleHide}
  181. top={boxTop}
  182. left={boxLeft}
  183. show={show} toClear={!hasSelect} value={value}/>
  184. </div>
  185. }
  186. }
  187. export default NumberUnitDrop;