index.jsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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} 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. getClasses(){ //整个标签是否有值的状态
  70. const {value,hideTag,show,isImports} = this.props;
  71. const inpValue = this.state.value;
  72. const blueBorder = this.state.editable?style['blue-border']:'';
  73. const isSelected = value||inpValue?style['selected']:style['container'];
  74. const orgBorder = isImports&&!(value||inpValue)?style['orange-border']:'';
  75. const noTag = hideTag?style['no-tag']:'';
  76. if(show){
  77. $(this.$cont.current).addClass(style['borderd']);
  78. }else{
  79. $(this.$cont.current).removeClass(style['borderd']);
  80. }
  81. return className(isSelected,noTag,blueBorder,orgBorder);
  82. }
  83. render(){
  84. const {placeholder,prefix,suffix,show,value,handleHide,hideTag} = this.props;
  85. const {editable,hasSelect} = this.state;
  86. return <div className={this.getClasses()}
  87. ref={this.$cont}
  88. onClick={this.handleNumClick}>
  89. <span ref = {this.$pre}>{prefix?prefix+' ':prefix}</span>
  90. <span ref = {this.$span}
  91. onKeyDown={handleEnter}
  92. style={{cursor:editable?'text':'pointer'}}>{value||placeholder}</span>
  93. <span ref = {this.$suf}>{suffix?' '+suffix:suffix}</span>
  94. <NumberUnitPan handleSelect={(obj)=>this.select(obj)}
  95. onClose={handleHide}
  96. show={show} toClear={!hasSelect} value={value}/>
  97. </div>
  98. }
  99. }
  100. export default NumberUnitDrop;