index.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React,{Component} from 'react';
  2. import {handleEnter,getPageCoordinate,windowEventHandler} from '@utils/tools.js';
  3. import {DropList} from '@commonComp';
  4. import classNames from 'classnames';
  5. import style from "./index.less";
  6. import $ from "jquery";
  7. /****
  8. * 单选下拉
  9. * author:zn@2018-11.13
  10. * 接收参数:
  11. * placeholder:灰显文字
  12. * data:下拉内容数据
  13. * hideTag:是否隐藏括号
  14. * handleSelect:选中事件
  15. * prefix:前缀
  16. * suffix:后缀
  17. *
  18. * ***/
  19. class RadioDrop extends Component{
  20. constructor(props){
  21. super(props);
  22. this.state={
  23. editable:false,
  24. timer:null,
  25. boxLeft:0,
  26. boxTop:0,
  27. tmpScroll:0,
  28. tmpTop:0,
  29. };
  30. this.$cont = React.createRef();
  31. this.isIE = navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.split(";")[1].replace(/[ ]/g,"")=="MSIE8.0";
  32. this.handleSelect = this.handleSelect.bind(this);
  33. this.handleShow = this.handleShow.bind(this);
  34. this.handledbClick = this.handledbClick.bind(this);
  35. this.handleEditLabel = this.handleEditLabel.bind(this);
  36. }
  37. getClass(){
  38. const {value,hideTag,placeholder,show,isImports} = this.props;
  39. const blueBorder = this.state.editable?style['blue-border']:'';
  40. const orgBorder = isImports?style['orange-border']:'';
  41. if(show){
  42. $(this.$cont.current).addClass(style['borderd']);
  43. }else{
  44. $(this.$cont.current).removeClass(style['borderd']);
  45. }
  46. if(hideTag){
  47. return style['no-tag'];
  48. }
  49. if(value){
  50. return blueBorder?classNames(style['selected-tag'],blueBorder):style['selected-tag'];
  51. }
  52. return classNames(style['tag'],orgBorder);
  53. }
  54. handleSelect(item){
  55. const {handleSelect,ikey,mainSaveText,value} = this.props;
  56. if(!item){ //清空
  57. handleSelect&&handleSelect({ikey});
  58. return ;
  59. }
  60. let text = '';
  61. switch(true){
  62. case +item.controlType ===6:
  63. case +item.controlType===7:
  64. case +item.tagType===3:
  65. text = item;
  66. break;
  67. case 3:
  68. default:
  69. // text = item.labelPrefix+item.name+item.labelSuffix;
  70. text = item.name;
  71. // text = item.questionDetailList&&item.questionDetailList.length>0?item.questionDetailList[0].name: item.name;
  72. }
  73. handleSelect&&handleSelect({ikey,id:item.id,text,mainSaveText,value});
  74. }
  75. handleShow(e){
  76. //e.stopPropagation();
  77. const {handleShow,ikey,id,patId} = this.props;
  78. const that = this;
  79. const timer = setTimeout(function(){
  80. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  81. return
  82. }else {
  83. handleShow && handleShow({ikey,id:patId||id});
  84. }
  85. },300);
  86. this.setState({
  87. timer,
  88. boxLeft:getPageCoordinate(e).boxLeft,
  89. boxTop:getPageCoordinate(e).boxTop,
  90. tmpScroll: $("#addScrollEvent")[0].scrollTop,
  91. tmpTop:getPageCoordinate(e).boxTop
  92. });
  93. windowEventHandler('scroll',()=>{ //弹窗跟随滚动条滚动或者关闭弹窗
  94. let scrollYs = $("#addScrollEvent")[0].scrollTop;
  95. this.setState({
  96. boxTop:this.state.tmpTop - scrollYs + this.state.tmpScroll
  97. })
  98. },$("#addScrollEvent")[0])
  99. }
  100. componentDidMount(){ //默认值选中
  101. const {data,ikey,handleSelect,hideTag,mainSaveText,value} = this.props;
  102. const selected = data.find((it)=>{
  103. return it.selected === undefined&&+it.defaultSelect===1;
  104. });
  105. if(!hideTag&&selected){
  106. // const text = selected.labelPrefix+selected.name+selected.labelSuffix;
  107. const text = selected.name;
  108. handleSelect&&handleSelect({ikey,id:selected.id,text,mainSaveText,value});
  109. }
  110. }
  111. handleEditLabel(e){
  112. e.stopPropagation();
  113. if(!this.state.editable){ //ie8点开下拉未选值存值bug修改
  114. return;
  115. }
  116. const {ikey,boxMark,handleLabelEdit} = this.props;
  117. this.setState({
  118. editable:false
  119. });
  120. // 更改标签的value值
  121. const ev = e || window.event;
  122. let changeVal = ev.target.innerText || ev.target.innerHTML;
  123. if(!this.isIE){
  124. ev.target.innerText?(ev.target.innerText = ''):(ev.target.innerHTML = '');
  125. }
  126. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  127. }
  128. handledbClick(e){
  129. const {value,id,handleDbclick,patId} = this.props;
  130. clearTimeout(this.state.timer);//取消延时的单击事件
  131. //e.preventDefault();
  132. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  133. this.setState({
  134. editable: true
  135. });
  136. };
  137. //失焦关闭编辑状态
  138. setTimeout(()=>{
  139. e.target.focus();
  140. })
  141. handleDbclick&&handleDbclick({id:patId||id});
  142. }
  143. render(){
  144. const {data,prefix,suffix,placeholder,show,value,hideTag} = this.props;
  145. const {boxLeft,boxTop} = this.state;
  146. return <div className={style['container']} ref = {this.$cont}>
  147. {prefix}
  148. <div className={this.getClass()}
  149. onBlur={this.handleEditLabel}
  150. contentEditable={this.state.editable}
  151. onDoubleClick={this.handledbClick}
  152. onClick={(e)=>this.handleShow(e,true)}
  153. onkeydown={handleEnter}>
  154. {value||placeholder}
  155. </div>
  156. {suffix}
  157. <DropList onSelect={this.handleSelect} data={data} left={boxLeft} top={boxTop} show={show} hideTag={hideTag}/>
  158. </div>
  159. }
  160. }
  161. export default RadioDrop;