index.jsx 5.0 KB

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