index.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import React,{Component} from 'react';
  2. import className from 'classnames';
  3. import {NumberPan,Notify} from '@commonComp';
  4. import style from './index.less';
  5. import $ from "jquery";
  6. import {handleEnter,getPageCoordinate} from '@utils/tools.js';
  7. /***
  8. * author:zn@2018-11-19
  9. * 接收参数:
  10. * value: 默认选中的值
  11. * placeholder:灰显文字
  12. * handleSelect: 选中事件
  13. * show: 是否显示下拉
  14. * allClick:是否前后缀也可唤出数字键盘
  15. *
  16. * ***/
  17. class NumberDrop extends Component{
  18. constructor(props){
  19. super(props);
  20. this.state={
  21. editable:false, //标签是否可输入
  22. timer:null,
  23. sltTimer:null,
  24. hasSelect:false, //是否点过下拉键盘
  25. boxLeft:0,
  26. boxTop:0,
  27. tmpTop:0,
  28. tmpScroll:0,
  29. placeholder:props.placeholder
  30. };
  31. this.$span = React.createRef();
  32. this.$pre = React.createRef();
  33. this.$suf = React.createRef();
  34. this.$cont = React.createRef();
  35. this.select = this.select.bind(this);
  36. this.numInpBlur = this.numInpBlur.bind(this);
  37. this.handleSpanInp = this.handleSpanInp.bind(this);
  38. this.handleNumClick = this.handleNumClick.bind(this);
  39. this.handleNumFocus = this.handleNumFocus.bind(this);
  40. this.handleBlur = this.handleBlur.bind(this);
  41. this.changeToEdit = this.changeToEdit.bind(this);
  42. //this.handleKeyDowm = this.handleKeyDowm.bind(this);
  43. this.beyondArea = this.beyondArea.bind(this);
  44. }
  45. select(text){ //选中键盘上数字事件
  46. let timer = null;
  47. clearTimeout(this.state.sltTimer);
  48. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max} = this.props;
  49. const needCompare=min!=undefined&&max!=undefined;
  50. if(!text){
  51. this.setState({
  52. placeholder:this.props.placeholder
  53. });
  54. }else{
  55. if(needCompare){
  56. const that = this;
  57. const isFine = this.validSymbols(text,min,max); //有~或/时是否合理
  58. const hasSymbol = /[\/|\~]/g.test(text); //是否有~或/
  59. const singleFine = !isNaN(+text)&&min<text&&text<max; //无~或/时是否合理
  60. timer = setTimeout(function(){
  61. clearTimeout(that.state.sltTimer);
  62. if(text!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  63. that.beyondArea();
  64. return;
  65. }
  66. },1500);
  67. }
  68. this.setState({
  69. hasSelect:true,
  70. sltTimer:timer
  71. });
  72. }
  73. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
  74. }
  75. beyondArea(){
  76. const {handleSelect,ikey,suffix,prefix,mainSaveText} = this.props;
  77. Notify.info("输入数值不符合规范,请重新输入!");
  78. handleSelect&&handleSelect({ikey,text:'',suffix,prefix,mainSaveText});
  79. this.setState({
  80. placeholder:this.props.placeholder,
  81. hasSelect:false
  82. });
  83. }
  84. handleNumFocus(e){ //数字框可编辑状态下聚集事件,处理是否显示下拉等
  85. const {placeholder} = this.state;
  86. const val = e.target.innerText.trim();
  87. //console.log(33,e.target.innerText,placeholder,e.target.innerText.trim() == placeholder)
  88. if(val!=''&&val == placeholder){
  89. this.setState({
  90. placeholder:''
  91. });
  92. }
  93. e.stopPropagation();
  94. }
  95. handleNumClick(e){ //数字框不可编辑的状态时点击事件,点击将数字框变为可输入且下拉不再显示直到失焦后再次聚集
  96. const {show,handleShow,ikey,id,patId,handleHide,value} = this.props;
  97. if(show) {
  98. handleHide && handleHide();
  99. return;
  100. }else{
  101. const {editable} = this.state;
  102. if(editable){
  103. return;
  104. }
  105. const that = this;
  106. //双击时不显示下拉
  107. clearTimeout(that.state.timer);
  108. const timer = setTimeout(function(){
  109. //只有弹窗关闭则点击数字键盘会清空当前数据
  110. that.$span.current.focus();
  111. that.setState({
  112. hasSelect:false
  113. });
  114. handleShow&&handleShow({ikey,id:patId||id});
  115. },300);
  116. this.setState({
  117. timer,
  118. boxLeft:getPageCoordinate(e).boxLeft,
  119. boxTop:getPageCoordinate(e).boxTop,
  120. tmpScroll: $("#addScrollEvent")[0].scrollTop,
  121. tmpTop:getPageCoordinate(e).boxTop
  122. });
  123. $("#addScrollEvent").scroll(()=>{
  124. let scrollYs = $("#addScrollEvent")[0].scrollTop;
  125. this.setState({
  126. boxTop:this.state.tmpTop - scrollYs + this.state.tmpScroll
  127. })
  128. })
  129. }
  130. e.stopPropagation();
  131. }
  132. validSymbols(txt,min,max){
  133. //输入只有一个~或/时判断两边是否为合理数字,有多个为不合理
  134. const index1 = txt.indexOf('~');
  135. const index2 = txt.indexOf('/');
  136. const needCompare = min!=undefined&&max!=undefined;
  137. let arr1=[],arr2=[];
  138. if(index1!=-1&&index1==txt.lastIndexOf('~')&&index1!=txt.length-1){ //有且只有一个~,且不在最后
  139. arr1 = txt.split('~');
  140. //~的范围在合理范围内为合理值
  141. if(!isNaN(+arr1[0])&&!isNaN(+arr1[1])&&((!needCompare)||(needCompare&&min<arr1[0]&&arr1[0]<max&&min<arr1[1]&&arr1[1]<max))){
  142. return true
  143. }
  144. return false;
  145. }
  146. if(index2!=-1&&index2==txt.lastIndexOf('/')&&index2!=txt.length-1){ //有且只有一个~,且不在最后
  147. arr2 = txt.split('/');
  148. // /两边的数字分别在合理范围内为合理值
  149. if(!isNaN(+arr2[0])&&!isNaN(+arr2[1])&&((!needCompare)||(needCompare&&min<arr2[0]&&arr2[0]<max&&min<arr2[1]&&arr2[1]<max))){
  150. return true
  151. }
  152. return false;
  153. }
  154. return false;
  155. }
  156. numInpBlur(e){ //数字框失焦,保存值到store中
  157. e.stopPropagation();
  158. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max,show} = this.props;
  159. if(show){ //修改清空后第一次点击键盘不触发click事件bug
  160. return;
  161. }
  162. //输入超出合理范围或输入不是数字提示且清空
  163. const needCompare=min!=undefined&&max!=undefined;
  164. //if(needCompare){
  165. const txt = e.target.innerText.trim();
  166. const isFine = this.validSymbols(txt,min,max); //有~或/时是否合理
  167. const hasSymbol = /[\/|\~]/g.test(txt); //是否有~或/
  168. const singleFine = (!isNaN(+txt)&&!needCompare)||(!isNaN(+txt)&&needCompare&&min<txt&&txt<max); //无~或/时是否合理
  169. if(txt!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  170. this.beyondArea();
  171. return;
  172. }
  173. //}
  174. //输入为空时显示placeholder
  175. if(!e.target.innerText.trim()){
  176. this.setState({
  177. placeholder:this.props.placeholder
  178. });
  179. }
  180. const val = e.target.innerText.trim();
  181. const {placeholder} = this.state;
  182. let text = val===placeholder?'':val;
  183. //e.target.innerText = ''; //避免出现重复输入值
  184. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
  185. }
  186. handleSpanInp(e){ //数字框输入事件
  187. e.stopPropagation();
  188. const {handleHide} = this.props;
  189. handleHide&&handleHide();
  190. }
  191. /*handleKeyDowm(e){
  192. handleEnter();
  193. //只能输入数字
  194. const key = e.key;
  195. const ctrlOn = e.ctrlKey;
  196. const isCopyPaste = ctrlOn&&(key=='v'||key=='c');
  197. if((!/[0-9|.|~|\/]/.test(key)&&key.length==1&&!isCopyPaste)){
  198. e.preventDefault();
  199. return false;
  200. }
  201. }*/
  202. getClasses(){ //整个标签是否有值的状态
  203. const {hideTag,placeholder,value} = this.props;
  204. const $span = this.$span.current;
  205. const val = value;//$span&&$span.innerText.trim()||value;
  206. const blueBorder = this.state.editable?style['blue-border']:'';
  207. const isSelected = val&&val!=placeholder?style['selected']:style['container'];
  208. const noTag = hideTag?style['no-tag']:'';
  209. return className(isSelected,noTag,blueBorder);
  210. }
  211. changeToEdit(e){ //整个标签双击编辑状态
  212. const {value,id,handleDbclick,patId,handleHide,show} = this.props;
  213. clearTimeout(this.state.timer);//取消延时的单击事件
  214. e.preventDefault();
  215. if(show){
  216. handleHide&&handleHide();
  217. }
  218. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  219. this.setState({
  220. editable: true
  221. });
  222. setTimeout(()=>{
  223. this.$cont.current.focus();
  224. })
  225. //双击埋点记录
  226. handleDbclick && handleDbclick({id:patId||id});
  227. }
  228. }
  229. handleBlur(e){ //双击编辑blur
  230. const {handleLabelChange,ikey,boxMark,value} = this.props;
  231. //if(!this.state.editable) return;
  232. this.setState({
  233. editable: false
  234. });
  235. let totalVal = e.target.innerText.trim();
  236. let changeVal = this.$span.current.innerText.trim();//数字框值-修改后;去掉前空格避免多空格叠加
  237. let prefix = this.$pre.current.innerText.trim(); //前缀值-修改后
  238. let suffix = this.$suf.current.innerText.trim(); //后缀值-修改后
  239. //console.log('数字框:'+changeVal,";全部:"+totalVal,";前缀:"+prefix+";后缀:"+suffix);
  240. handleLabelChange && handleLabelChange({ikey,changeVal,type:boxMark,totalVal,prefix,suffix});
  241. }
  242. getSpanClass(){ //将被替换的文字选中状态显示
  243. //const {hasSelect} = this.state;
  244. const cls = this.props.show?style['blued']:'';
  245. return cls;
  246. }
  247. componentDidMount(){
  248. //设置最小宽度避免输入后宽度跳动
  249. const spanWidth = window.getComputedStyle(this.$span.current).width;
  250. this.$span.current.style.minWidth=spanWidth;
  251. }
  252. render(){
  253. const {prefix,suffix,show,value,handleHide,allClick} = this.props;
  254. const {numEditable,placeholder,editable,hasSelect,boxTop,boxLeft} = this.state;
  255. return <div className={this.getClasses()}
  256. ref={this.$cont}
  257. onDoubleClick={this.changeToEdit}
  258. onClick={allClick?this.handleNumClick:null}
  259. contentEditable={editable}
  260. onBlur={this.handleBlur}
  261. onkeydown={handleEnter}>
  262. <span ref = {this.$pre}>&nbsp;{prefix}</span>
  263. <span onFocus={this.handleNumFocus}
  264. onClick={allClick?null:this.handleNumClick}
  265. contentEditable={true}
  266. style={{minWidth:'10px',display:'inline-block',textAlign:'center'}}
  267. ref = {this.$span}
  268. onBlur={this.numInpBlur}
  269. onInput={this.handleSpanInp}
  270. className={this.getSpanClass()}
  271. >&nbsp;{value||placeholder}</span>
  272. <span ref = {this.$suf}>&nbsp;{suffix}</span>
  273. <NumberPan handleSelect={(text)=>this.select(text)}
  274. onClose={handleHide}
  275. show={show}
  276. toClear={!hasSelect}
  277. left={boxLeft}
  278. top={boxTop}/>
  279. </div>
  280. }
  281. }
  282. export default NumberDrop;