index.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. this.emitBMIData = this.emitBMIData.bind(this);
  35. }
  36. select(text){ //选中键盘上数字事件
  37. //placeholder修改后,第一次点击键盘触发blur后onClick不触发,原因未知,改为onMouseup可触发
  38. let timer = null;
  39. clearTimeout(this.state.sltTimer);
  40. clearTimeout(this.state.blurTimer);
  41. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max,formulaCode} = this.props;
  42. const needCompare=min!=undefined&&max!=undefined;
  43. if(!text){
  44. this.setState({
  45. placeholder:this.props.placeholder
  46. });
  47. }else{
  48. if(needCompare){
  49. const that = this;
  50. const isFine = this.validSymbols(text,min,max); //有~或/时是否合理
  51. const hasSymbol = /[\/|\~]/g.test(text); //是否有~或/
  52. const singleFine = !isNaN(+text)&&parseFloat(min)<=parseFloat(text)&&parseFloat(text)<=parseFloat(max); //无~或/时是否合理
  53. timer = setTimeout(function(){
  54. clearTimeout(that.state.sltTimer);
  55. if(text!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  56. that.beyondArea();
  57. return;
  58. }
  59. },1500);
  60. }
  61. this.setState({
  62. hasSelect:true,
  63. sltTimer:timer
  64. });
  65. }
  66. handleSelect&&handleSelect({ikey,text,suffix,prefix,mainSaveText,formulaCode});
  67. //BMI相关数据上传
  68. this.emitBMIData(text);
  69. }
  70. emitBMIData(text){
  71. //手动修改BMI相关值时数据上传
  72. const {setBMIParam,formulaCode,setBMI} = this.props;
  73. if(formulaCode==="BMI_RES"){
  74. setBMI&&setBMI(text);
  75. }
  76. if(text!==0&&(formulaCode==="BMI_SG"||formulaCode==="BMI_TZ")){
  77. const map={
  78. BMI_SG:'tall',
  79. BMI_TZ:'weight'
  80. };
  81. setTimeout(function(){
  82. setBMIParam&&setBMIParam(map[formulaCode],text);
  83. });
  84. }
  85. }
  86. beyondArea(){
  87. const {handleSelect,ikey,suffix,prefix,mainSaveText,formulaCode} = this.props;
  88. Notify.info("输入数值不符合规范,请重新输入!");
  89. handleSelect&&handleSelect({ikey,text:'',suffix,prefix,mainSaveText,formulaCode});
  90. this.emitBMIData('');
  91. this.setState({
  92. placeholder:this.props.placeholder,
  93. hasSelect:false
  94. });
  95. }
  96. handleNumFocus(e){
  97. /*const {placeholder} = this.state;
  98. const val = e.target.innerText.trim();
  99. //console.log(33,e.target.innerText,placeholder,e.target.innerText.trim() == placeholder)
  100. if(val!=''&&val == placeholder){
  101. this.setState({
  102. placeholder:''
  103. });
  104. }*/
  105. e.stopPropagation();
  106. }
  107. handleKeyDowm(e){
  108. if(e.keyCode==13){
  109. const {reFocus,num,handleHide} = this.props;
  110. reFocus&&reFocus(num);
  111. handleHide && handleHide();
  112. }
  113. }
  114. handleNumClick(e){ //数字框不可编辑的状态时点击事件,点击将数字框变为可输入且下拉不再显示直到失焦后再次聚集
  115. e.stopPropagation();
  116. const {show,handleShow,ikey,id,patId,handleHide} = this.props;
  117. if(show) {
  118. handleHide && handleHide();
  119. return;
  120. }else{
  121. this.$span.current.focus();
  122. this.setState({
  123. hasSelect:false,
  124. placeholder:'' //火狐26placeholder点击不隐藏bug修改
  125. });
  126. handleShow&&handleShow({ikey,id:patId||id});
  127. }
  128. }
  129. validSymbols(txt,min,max){
  130. //输入只有一个~或/时判断两边是否为合理数字,有多个为不合理
  131. const index1 = txt.indexOf('~');
  132. const index2 = txt.indexOf('/');
  133. const needCompare = min!=undefined&&max!=undefined;
  134. let arr1=[],arr2=[];
  135. if(index1!=-1&&index1==txt.lastIndexOf('~')&&index1!=txt.length-1){ //有且只有一个~,且不在最后
  136. arr1 = txt.split('~');
  137. //~的范围在合理范围内为合理值
  138. 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)))){
  139. return true
  140. }
  141. return false;
  142. }
  143. if(index2!=-1&&index2==txt.lastIndexOf('/')&&index2!=txt.length-1){ //有且只有一个~,且不在最后
  144. arr2 = txt.split('/');
  145. // /两边的数字分别在合理范围内为合理值
  146. 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)))){
  147. return true
  148. }
  149. return false;
  150. }
  151. return false;
  152. }
  153. numInpBlur(e){ //数字框失焦,保存值到store中
  154. e.stopPropagation();
  155. const {handleSelect,ikey,suffix,prefix,mainSaveText,min,max,show,formulaCode} = this.props;
  156. /*if(show){ //修改清空后第一次点击键盘不触发click事件bug--失焦placehoder消失,弃用
  157. return;
  158. }*/
  159. //输入超出合理范围或输入不是数字提示且清空
  160. const needCompare=min!=undefined&&max!=undefined;
  161. const txt = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');//e.target.innerText.trim();
  162. const isFine = this.validSymbols(txt,min,max); //有~或/时是否合理
  163. const hasSymbol = /[\/|\~]/g.test(txt); //是否有~或/
  164. const singleFine = (!isNaN(+txt)&&!needCompare)||(!isNaN(+txt)&&needCompare&&parseFloat(min)<=parseFloat(txt)&&parseFloat(txt)<=parseFloat(max)); //无~或/时是否合理
  165. if(txt!=''&&(!hasSymbol&&!singleFine)||(hasSymbol&&!isFine)){
  166. this.beyondArea();
  167. return;
  168. }
  169. //输入为空时显示placeholder
  170. const timer = setTimeout(()=>{
  171. if(!e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'')){
  172. this.setState({
  173. placeholder:this.props.placeholder
  174. });
  175. }
  176. },200);
  177. this.setState({
  178. blurTimer:timer
  179. });
  180. const val = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');//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,formulaCode});
  185. this.emitBMIData(text);
  186. }
  187. handleSpanInp(e){ //数字框输入事件
  188. e.stopPropagation();
  189. const txt = e.target.innerHTML.replace(/&nbsp;$|^&nbsp;/,'');
  190. this.setState({
  191. value:txt
  192. });
  193. const {handleHide} = this.props;
  194. handleHide&&handleHide();
  195. }
  196. getClasses(){ //整个标签是否有值的状态
  197. const {hideTag,placeholder,isImports,isExtBlue} = this.props;
  198. const val = this.state.value;
  199. const isSelected = val&&val!=placeholder?style['selected']:style['container'];
  200. const orgBorder = isImports&&!(val&&val!=placeholder)?style['orange-border']:'';
  201. const ext = isExtBlue?style['ext']:'';
  202. const noTag = hideTag?style['no-tag']:'';
  203. return className(isSelected,noTag,orgBorder,ext);
  204. }
  205. getSpanClass(){ //将被替换的文字选中状态显示
  206. const cls = this.props.show?style['blued']:style['nol'];
  207. return cls;
  208. }
  209. stopBubble(e){
  210. e.stopPropagation();
  211. }
  212. componentDidMount(){
  213. //设置最小宽度避免输入后宽度跳动
  214. const spanWidth = window.getComputedStyle(this.$span.current).width;
  215. this.$span.current.style.minWidth=spanWidth;
  216. //保存输入框dom以便聚焦
  217. const that = this;
  218. setTimeout(function(){ //多个其他史/现病史bug修改
  219. const {saveDoms} = that.props;
  220. saveDoms&&saveDoms(that.$span);
  221. })
  222. }
  223. componentWillReceiveProps(nextProps){
  224. //BMI存值
  225. const {formulaCode,bmi,suffix,prefix,ikey,handleSelect,mainSaveText,wrBmi} = this.props;
  226. if(formulaCode==="BMI_RES"&&nextProps.bmi!==bmi){
  227. handleSelect&&handleSelect({ikey,text:nextProps.bmi,suffix,prefix,mainSaveText,formulaCode});
  228. }
  229. //手动修改BMI时身高体重清空
  230. if(wrBmi!==nextProps.wrBmi&&(formulaCode==="BMI_SG"||formulaCode==="BMI_TZ")){
  231. handleSelect&&handleSelect({ikey,text:'',suffix,prefix,mainSaveText,formulaCode});
  232. }
  233. if((nextProps.placeholder == this.props.placeholder)&&(nextProps.value == this.props.value)){
  234. return
  235. }
  236. this.setState({
  237. placeholder:nextProps.placeholder,
  238. value:nextProps.value
  239. });
  240. }
  241. render(){
  242. const {prefix,suffix,show,value,handleHide,allClick,bmi,formulaCode} = this.props;
  243. const val = formulaCode==="BMI_RES"?(+bmi===Infinity||isNaN(bmi)?'':bmi):value;
  244. const {placeholder,hasSelect} = this.state;
  245. return <div className={this.getClasses()}
  246. style={{position:'relative'}}
  247. onClick={allClick?this.handleNumClick:null}>
  248. <span>{prefix}</span>
  249. <span onFocus={this.handleNumFocus}
  250. onClick={allClick?null:this.handleNumClick}
  251. contentEditable={true}
  252. style={{minWidth:'10px',display:'inline-block',textAlign:'center'}}
  253. ref = {this.$span}
  254. onkeyup={this.handleKeyDowm}
  255. onBlur={this.numInpBlur}
  256. onInput={this.handleSpanInp}
  257. className={this.getSpanClass()}
  258. >&nbsp;{val||placeholder}</span>
  259. <span>{suffix}</span>
  260. <NumberPan handleSelect={this.select.bind(this)}
  261. noString={formulaCode?true:false}
  262. onClose={handleHide}
  263. show={show}
  264. toClear={!hasSelect}/>
  265. </div>
  266. }
  267. }
  268. export default NumberDrop;