index.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import React,{Component} from 'react';
  2. import {handleEnter,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. tmpDom:null
  26. };
  27. this.$cont = React.createRef();
  28. this.isIE = navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.split(";")[1].replace(/[ ]/g,"")=="MSIE8.0";
  29. this.handleSelect = this.handleSelect.bind(this);
  30. this.handleShow = this.handleShow.bind(this);
  31. this.handledbClick = this.handledbClick.bind(this);
  32. this.handleEditLabel = this.handleEditLabel.bind(this);
  33. }
  34. getClass(){
  35. const {value,hideTag,show,isImports,isExtBlue} = this.props;
  36. const ext = isExtBlue?style['ext']:'';
  37. const orgBorder = isImports?style['orange-border']:''; //橙色框高亮
  38. if(show){
  39. $(this.$cont.current).addClass(style['borderd']);
  40. }else{
  41. $(this.$cont.current).removeClass(style['borderd']);
  42. }
  43. if(hideTag){
  44. return classNames(style['no-tag'],ext);
  45. }
  46. if(value){
  47. return style['selected-tag'];
  48. }
  49. return classNames(style['tag'],orgBorder,ext);
  50. }
  51. handleSelect(item){
  52. const {handleSelect,ikey,mainSaveText,value} = this.props;
  53. if(!item){ //清空
  54. handleSelect&&handleSelect({ikey});
  55. return ;
  56. }
  57. let text = '';
  58. switch(true){
  59. case +item.controlType ===6:
  60. case +item.controlType===7:
  61. case +item.tagType===3:
  62. text = item;
  63. break;
  64. case 3:
  65. default:
  66. // text = item.labelPrefix+item.name+item.labelSuffix;
  67. text = item.name;
  68. // text = item.questionDetailList&&item.questionDetailList.length>0?item.questionDetailList[0].name: item.name;
  69. }
  70. handleSelect&&handleSelect({ikey,id:item.id,text,mainSaveText,value});
  71. }
  72. handleShow(e){
  73. // e.stopPropagation();
  74. const {handleShow,ikey,id,patId} = this.props;
  75. const that = this;
  76. const timer = setTimeout(()=>{
  77. if (that.state.editable) {//如果处于编辑状态点击不显示下拉框
  78. return
  79. }else {
  80. document.activeElement.blur()//chrome41有效,但是失去焦点的span仍能编辑
  81. $(e.target).parent().prev().attr({"contentEditable":false})
  82. this.setState({
  83. tmpDom:e.target
  84. })
  85. handleShow && handleShow({ikey,id:patId||id});
  86. }
  87. },300);
  88. this.setState({
  89. timer,
  90. });
  91. }
  92. componentDidMount(){ //默认值选中
  93. const {data,ikey,handleSelect,hideTag,mainSaveText,value,boxMark} = this.props;
  94. const selected = data.find((it)=>{
  95. return it.selected === undefined&&+it.defaultSelect===1;
  96. });
  97. if(boxMark!=1&&!hideTag&&selected){
  98. // const text = selected.labelPrefix+selected.name+selected.labelSuffix;
  99. const text = selected.name;
  100. handleSelect&&handleSelect({ikey,id:selected.id,text,mainSaveText,value});
  101. }
  102. }
  103. handleEditLabel(e){
  104. e.stopPropagation();
  105. if(!this.state.editable){ //ie8点开下拉未选值存值bug修改
  106. return;
  107. }
  108. const {ikey,boxMark,handleLabelEdit} = this.props;
  109. this.setState({
  110. editable:false
  111. });
  112. // 更改标签的value值
  113. const ev = e || window.event;
  114. let changeVal = ev.target.innerText || ev.target.innerHTML;
  115. //fireFox括号删完有<br>2270
  116. changeVal=changeVal=="<br>"?'':changeVal;
  117. if(!this.isIE){
  118. ev.target.innerText?(ev.target.innerText = ''):(ev.target.innerHTML = '');
  119. }
  120. handleLabelEdit && handleLabelEdit({ikey,changeVal,type:boxMark});
  121. }
  122. handledbClick(e){
  123. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  124. const {value,id,handleDbclick,patId} = this.props;
  125. clearTimeout(this.state.timer);//取消延时的单击事件
  126. //e.preventDefault();
  127. if(value&&value.trim()) {//有选中值的标签才能双击编辑
  128. this.setState({
  129. editable: true
  130. });
  131. };
  132. //失焦关闭编辑状态
  133. setTimeout(()=>{
  134. e.target.focus();
  135. })
  136. handleDbclick&&handleDbclick({id:patId||id});
  137. }
  138. render(){
  139. const {data,placeholder,show,value,hideTag,boxMark} = this.props;
  140. const {tmpDom} = this.state
  141. if(!show&&tmpDom){
  142. $(tmpDom).parent().prev().attr({"contentEditable":true})
  143. }
  144. return <div className={style['container']} ref = {this.$cont}>
  145. <div className={this.getClass()}
  146. onBlur={this.handleEditLabel}
  147. contentEditable={this.state.editable}
  148. onDoubleClick={hideTag?null:this.handledbClick}
  149. onFocus={(e)=>{e.stopPropagation()}}
  150. onClick={(e)=>this.handleShow(e,true)}
  151. onKeyDown={handleEnter}>
  152. {value||placeholder}
  153. </div>
  154. <DropList onSelect={this.handleSelect} boxMark={boxMark} data={data} show={show} hideTag={hideTag}/>
  155. </div>
  156. }
  157. }
  158. export default RadioDrop;