index.jsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import React,{Component} from 'react';
  2. import className from 'classnames';
  3. import {NumberPan,Notify} from '@commonComp';
  4. import style from './index.less';
  5. /***
  6. * author:zn@2018-11-19
  7. * 接收参数:
  8. * value: 默认选中的值
  9. * placeholder:灰显文字
  10. * handleSelect: 选中事件
  11. * show: 是否显示下拉
  12. * allClick:是否前后缀也可唤出数字键盘
  13. *
  14. * ***/
  15. class NumberDrop extends Component{
  16. constructor(props){
  17. super(props);
  18. this.state={
  19. /*editable:false, //标签是否可输入*/
  20. value:props.value||'',
  21. timer:null,
  22. sltTimer:null,
  23. blurTimer:null,
  24. hasSelect:false, //是否点过下拉键盘
  25. placeholder:props.placeholder
  26. };
  27. this.$span = React.createRef();
  28. this.numInpBlur = this.numInpBlur.bind(this);
  29. this.handleSpanInp = this.handleSpanInp.bind(this);
  30. this.handleNumClick = this.handleNumClick.bind(this);
  31. this.handleNumFocus = this.handleNumFocus.bind(this);
  32. this.handleKeyDowm = this.handleKeyDowm.bind(this);
  33. this.beyondArea = this.beyondArea.bind(this);
  34. }
  35. select(text){ //选中键盘上数字事件
  36. //placeholder修改后,第一次点击键盘触发blur后onClick不触发,原因未知,改为onMouseup可触发
  37. let timer = null;
  38. clearTimeout(this.state.sltTimer);
  39. clearTimeout(this.state.blurTimer);
  40. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max} = this.props;
  41. const needCompare=min!=undefined&&max!=undefined;
  42. if(!text){
  43. this.setState({
  44. placeholder:this.props.placeholder
  45. });
  46. }else{
  47. if(needCompare){
  48. const that = this;
  49. const isFine = this.validSymbols(text,min,max); //有~或/时是否合理
  50. const hasSymbol = /[\/|\~]/g.test(text); //是否有~或/
  51. const singleFine = !isNaN(+text)&&parseFloat(min)<=parseFloat(text)&&parseFloat(text)<=parseFloat(max); //无~或/时是否合理
  52. timer = setTimeout(function(){
  53. clearTimeout(that.state.sltTimer);
  54. if(text!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  55. that.beyondArea();
  56. return;
  57. }
  58. },1500);
  59. }
  60. this.setState({
  61. hasSelect:true,
  62. sltTimer:timer
  63. });
  64. }
  65. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
  66. }
  67. beyondArea(){
  68. const {handleSelect,ikey,suffix,prefix,mainSaveText} = this.props;
  69. Notify.info("输入数值不符合规范,请重新输入!");
  70. handleSelect&&handleSelect({ikey,text:'',suffix,prefix,mainSaveText});
  71. this.setState({
  72. placeholder:this.props.placeholder,
  73. hasSelect:false
  74. });
  75. }
  76. handleNumFocus(e){
  77. /*const {placeholder} = this.state;
  78. const val = e.target.innerText.trim();
  79. //console.log(33,e.target.innerText,placeholder,e.target.innerText.trim() == placeholder)
  80. if(val!=''&&val == placeholder){
  81. this.setState({
  82. placeholder:''
  83. });
  84. }*/
  85. e.stopPropagation();
  86. }
  87. handleKeyDowm(e){
  88. if(e.keyCode==13){
  89. const {reFocus,num,handleHide} = this.props;
  90. reFocus&&reFocus(num);
  91. handleHide && handleHide();
  92. }
  93. }
  94. handleNumClick(e){ //数字框不可编辑的状态时点击事件,点击将数字框变为可输入且下拉不再显示直到失焦后再次聚集
  95. e.stopPropagation();
  96. const {show,handleShow,ikey,id,patId,handleHide} = this.props;
  97. if(show) {
  98. handleHide && handleHide();
  99. return;
  100. }else{
  101. this.$span.current.focus();
  102. this.setState({
  103. hasSelect:false,
  104. placeholder:'' //火狐26placeholder点击不隐藏bug修改
  105. });
  106. handleShow&&handleShow({ikey,id:patId||id});
  107. }
  108. }
  109. validSymbols(txt,min,max){
  110. //输入只有一个~或/时判断两边是否为合理数字,有多个为不合理
  111. const index1 = txt.indexOf('~');
  112. const index2 = txt.indexOf('/');
  113. const needCompare = min!=undefined&&max!=undefined;
  114. let arr1=[],arr2=[];
  115. if(index1!=-1&&index1==txt.lastIndexOf('~')&&index1!=txt.length-1){ //有且只有一个~,且不在最后
  116. arr1 = txt.split('~');
  117. //~的范围在合理范围内为合理值
  118. 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)))){
  119. return true
  120. }
  121. return false;
  122. }
  123. if(index2!=-1&&index2==txt.lastIndexOf('/')&&index2!=txt.length-1){ //有且只有一个~,且不在最后
  124. arr2 = txt.split('/');
  125. // /两边的数字分别在合理范围内为合理值
  126. 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)))){
  127. return true
  128. }
  129. return false;
  130. }
  131. return false;
  132. }
  133. numInpBlur(e){ //数字框失焦,保存值到store中
  134. e.stopPropagation();
  135. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max,show} = this.props;
  136. /*if(show){ //修改清空后第一次点击键盘不触发click事件bug--失焦placehoder消失,弃用
  137. return;
  138. }*/
  139. //输入超出合理范围或输入不是数字提示且清空
  140. const needCompare=min!=undefined&&max!=undefined;
  141. const txt = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');//e.target.innerText.trim();
  142. const isFine = this.validSymbols(txt,min,max); //有~或/时是否合理
  143. const hasSymbol = /[\/|\~]/g.test(txt); //是否有~或/
  144. const singleFine = (!isNaN(+txt)&&!needCompare)||(!isNaN(+txt)&&needCompare&&parseFloat(min)<=parseFloat(txt)&&parseFloat(txt)<=parseFloat(max)); //无~或/时是否合理
  145. if(txt!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  146. this.beyondArea();
  147. return;
  148. }
  149. //输入为空时显示placeholder
  150. const timer = setTimeout(()=>{
  151. if(!e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'')){
  152. this.setState({
  153. placeholder:this.props.placeholder
  154. });
  155. }
  156. },200);
  157. this.setState({
  158. blurTimer:timer
  159. });
  160. const val = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');//e.target.innerText.trim();
  161. const {placeholder} = this.state;
  162. let text = val===placeholder?'':val;
  163. //e.target.innerText = ''; //避免出现重复输入值
  164. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText});
  165. }
  166. handleSpanInp(e){ //数字框输入事件
  167. e.stopPropagation();
  168. const txt = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');
  169. this.setState({
  170. value:txt
  171. });
  172. const {handleHide} = this.props;
  173. handleHide&&handleHide();
  174. }
  175. getClasses(){ //整个标签是否有值的状态
  176. const {hideTag,placeholder,isImports} = this.props;
  177. const val = this.state.value;
  178. const isSelected = val&&val!=placeholder?style['selected']:style['container'];
  179. const orgBorder = isImports&&!(val&&val!=placeholder)?style['orange-border']:'';
  180. const noTag = hideTag?style['no-tag']:'';
  181. return className(isSelected,noTag,orgBorder);
  182. }
  183. getSpanClass(){ //将被替换的文字选中状态显示
  184. const cls = this.props.show?style['blued']:'';
  185. return cls;
  186. }
  187. stopBubble(e){
  188. e.stopPropagation();
  189. }
  190. componentDidMount(){
  191. //设置最小宽度避免输入后宽度跳动
  192. const spanWidth = window.getComputedStyle(this.$span.current).width;
  193. this.$span.current.style.minWidth=spanWidth;
  194. //保存输入框dom以便聚焦
  195. const that = this;
  196. setTimeout(function(){ //多个其他史/现病史bug修改
  197. const {saveDoms} = that.props;
  198. saveDoms&&saveDoms(that.$span);
  199. })
  200. }
  201. componentWillReceiveProps(nextProps){
  202. if((nextProps.placeholder == this.props.placeholder)&&(nextProps.value == this.props.value)){
  203. return
  204. }
  205. this.setState({
  206. placeholder:nextProps.placeholder,
  207. value:nextProps.value
  208. })
  209. }
  210. render(){
  211. const {prefix,suffix,show,value,handleHide,allClick} = this.props;
  212. const {placeholder,hasSelect} = this.state;
  213. return <div className={this.getClasses()}
  214. style={{position:'relative'}}
  215. onClick={allClick?this.handleNumClick:null}>
  216. <span>{prefix}</span>
  217. <span onFocus={this.handleNumFocus}
  218. onClick={allClick?null:this.handleNumClick}
  219. contentEditable={true}
  220. style={{minWidth:'10px',display:'inline-block',textAlign:'center'}}
  221. ref = {this.$span}
  222. onkeyup={this.handleKeyDowm}
  223. onBlur={this.numInpBlur}
  224. onInput={this.handleSpanInp}
  225. className={this.getSpanClass()}
  226. >&nbsp;{value||placeholder}</span>
  227. <span>{suffix}</span>
  228. <NumberPan handleSelect={this.select.bind(this)}
  229. onClose={handleHide}
  230. show={show}
  231. toClear={!hasSelect}/>
  232. </div>
  233. }
  234. }
  235. export default NumberDrop;