index.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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,filterDataArr,setFontColorSize,handleMouseUp} 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. * 双击编辑效果去掉2019-8-13
  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. };
  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.handleNumClick = this.handleNumClick.bind(this);
  37. }
  38. select(obj){ //选中键盘上数字事件
  39. const {handleSelect,ikey,suffix,prefix,mainSaveText,formulaCode,mainData} = this.props;
  40. this.setState({
  41. hasSelect:true
  42. });
  43. if(!obj.text){
  44. this.setState({
  45. value:''
  46. });
  47. }
  48. handleSelect&&handleSelect({ikey,text:obj.text,suffix,prefix,mainSaveText,mark:obj.mark,formulaCode,mainData});
  49. }
  50. handleNumClick(e){
  51. e.stopPropagation();
  52. const {show,handleShow,ikey,id,patId,handleHide,value} = this.props;
  53. const {hasSelect} = this.state;
  54. const that = this;
  55. //双击时不显示下拉
  56. clearTimeout(this.state.timer);
  57. const timer = setTimeout(function(){
  58. const {hasSelect,editable,isClosed} = that.state;
  59. if(editable||isClosed){
  60. return;
  61. }
  62. handleShow&&handleShow({ikey,id:patId||id});
  63. },300);
  64. this.setState({
  65. timer,
  66. });
  67. handleHide&&handleHide();
  68. }
  69. handleMouseDown(){
  70. const {i,setSelectArea,boxMark}= this.props;
  71. setSelectArea({i,boxMark,dir:'start'});
  72. }
  73. getClasses(){ //整个标签是否有值的状态
  74. const {value,hideTag,show,isImports,isExtBlue,mouseSelect} = this.props;
  75. const inpValue = this.state.value;
  76. const blueBorder = this.state.editable?style['blue-border']:'';
  77. const isSelected = value||inpValue?style['selected']:style['container'];
  78. const orgBorder = isImports&&!(value||inpValue)?style['orange-border']:'';
  79. const noTag = hideTag?style['no-tag']:'';
  80. const selectedArea = mouseSelect?style['selected-area']:'';
  81. if(show){
  82. $(this.$cont.current).addClass(style['borderd']);
  83. }else{
  84. $(this.$cont.current).removeClass(style['borderd']);
  85. }
  86. if(value){
  87. return className(isSelected,noTag,blueBorder,orgBorder,setFontColorSize(isExtBlue?2:''),selectedArea);
  88. }else{
  89. if(isExtBlue){
  90. return classNames(isSelected,noTag,blueBorder,orgBorder, setFontColorSize(2),'prefixUnsetColor',selectedArea);
  91. }else{
  92. return className(isSelected,noTag,blueBorder,orgBorder,setFontColorSize(1),selectedArea);
  93. }
  94. }
  95. }
  96. render(){
  97. const {placeholder,prefix,suffix,show,value,handleHide,select_start,i,boxMark} = this.props;
  98. const {editable,hasSelect} = this.state;
  99. return <div className={this.getClasses()}
  100. ref={this.$cont}
  101. onMouseUp={()=>handleMouseUp({select_start,i,boxMark})}
  102. onMouseDown={this.handleMouseDown.bind(this)}
  103. onClick={this.handleNumClick}>
  104. <span ref = {this.$pre} className="prefixUnset">{prefix?prefix+' ':prefix}</span>
  105. <span ref = {this.$span}
  106. onKeyDown={handleEnter}
  107. className="prefixUnset"
  108. style={{cursor:editable?'text':'pointer',wordBreak:'break-all'}}>{value||placeholder}</span>
  109. <span ref = {this.$suf} className="prefixUnset">{suffix?' '+suffix:suffix}</span>
  110. <NumberUnitPan handleSelect={(obj)=>this.select(obj)}
  111. onClose={handleHide}
  112. show={show} toClear={!hasSelect} value={value}/>
  113. </div>
  114. }
  115. }
  116. export default NumberUnitDrop;