index.jsx 11 KB

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